(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 + C
and click Entity - Set
Name
to: Log - Set the
Display Icon
to: File spreadsheet - Mark the
Create Display Attribute
check 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
Name
to: Logs - Set
Type
to: Composition - Set
Referenced
to: 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 Tab
magic 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
Logs
tab - Click
Edit Layout
- Click
Data Grid of Log
- The default setting shows the
Columns
tab. Log is shown by default. Add Ticket - Click
OK
— - Go to the
General
tab - Verify that
Entity
is set to: Log - Verify that
Data Source
is 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 + C
and in the Business section, select Command - Set
Name
to: Log Title Change - Set
Type
to: Entity Command - Set
Entity
to: 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 + C
and in the Business section, select Business Events. - Set
Command
to: LogTitleChange (Ticket) - Click
Finish
—
On the Event Triggers page, select the appropriate allocations. - Set
Entity Ticket
to Created
In the Attribute Changes section,
- Mark
Title
as Changed - Save:
Ctrl + S