(10) Entity Events and Business Commands
- Support Ticket Tutorial -
Create a Change Log
Business commands are very useful when you want to interact with your data. You can define Entity events to set conditions for when to trigger your commands. This section will create a simple change log to show all of the changes made to the Title of the Support Tickets by users.
1. Create Log Entity
First, we need to create the entity where we will store all the logs.
- Type:
Ctrl + Shift + Cand click Entity - Set
Nameto: Log - Set the
Display Iconto: File spreadsheet - Mark the
Create Display Attributecheck circle. Type Title Change - Click
Next - Remove the Create page and Detail Page. Keep the List Page
- Click
Finish
— - Go to Data > Entities and click Ticket
- Click
Add Reference - Set
Nameto: Logs - Set
Typeto: Composition - Set
Referencedto: Log - Click
Save
2. Update Entity pages
Next, all logs should show for each entity.
- Go to UI > Entity Pages and click Ticket Detail
- Go the Layout Editor, and, near the bottom of the page, click the
Add Tabmagic wand - Select Create Child List Tab. (Make sure that all files are saved so that the proper options will be available.)
- Select Logs
- In the lower section of the Layout Editor, click the
Logstab - Click
Edit Layout - Click
Data Grid of Log - The default setting shows the
Columnstab. Log is shown by default. Add Ticket - Click
OK
— - Go to the
Generaltab - Verify that
Entityis set to: Log - Verify that
Data Sourceis set to:
(ticket, db, ctx) => db.LogSet.Where(x => x.Ticket == ticket)
- Click
OK - Click
OK - Save:
Ctrl + S
3. Create Business command
A command needs to be established to pull the log into the list.
- Type:
Ctrl + Shift + Cand in the Business section, select Command - Set
Nameto: Log Title Change - Set
Typeto: Entity Command - Set
Entityto: Ticket - Click
Finish - Copy and paste the following code:
(ticket, db, ctx) =>
{
ticket.Logs.Add(new Log()
{
TitleChange = "Title changed at: " + DateTime.Now.ToString() + " - New Title: " + (string.IsNullOrEmpty(ticket.Title) ? "no title defined!" : ticket.Title)
});
}
- Save:
Ctrl + S
4. Create Event Entity
The trigger for data to be added to the Change Log is when an end user alters the title of a Support Ticket.
- Type:
Ctrl + Shift + Cand in the Business section, select Business Events. - Set
Commandto: LogTitleChange (Ticket) - Click
Finish
—
On the Event Triggers page, select the appropriate allocations. - Set
Entity Ticketto Created
In the Attribute Changes section,
- Mark
Titleas Changed - Save:
Ctrl + S