How to: Add Custom Actions for Alerts

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Add a custom action by using an action menu item and a static execute method. An example of an existing action occurs when an alert is inserted in the EventAlertInbox form.

Add a Custom Action

  1. Implement your custom action class. The custom logic for processing your event should be added to the existing code in the execute method of your action class. The execute method has the following parameters.
        public void execute(
            EventRule   eventRule,
            EventType   eventType,
            Common      buffer,
            EventInbox inbox,
            EventAlertCreatedDate   eventCreatedDate = systemdateget(),
            EventAlertCreatedTime   eventCreatedTime = timenow()
            )
The parameters of the execute method are defined as follows:

  - eventRule - The rule that has generated the alert.

  - eventType - An object of one of the EventType classes, which represents the event that generated the alert.

  - buffer - The record in which the event occurred (the one in which, for example, the value of a field has changed).

  - inbox - The alert generated by the event processor (by applying the rule found in eventRule to the event found in eventType).

  - eventCreatedDate and eventCreatedTime - Shows the date and time, respectively, that indicates when the alert was created.
  1. Modify the code to add your class as a custom action to the action list in the initEventRule method of the EventCreateRule Class. The list of custom action IDs can also be updated anywhere in the code in the area of rule creation, for example, in the insert method on the EventRule table. The ActionIds field of the EventRule table contains the packed list of action class identifiers. The order in which the class IDs are added to the created list determines the order in which your actions are executed when a match occurs.

Note

In the default implementation, the identifiers of the EventActionAlert Class and the EventActionEmail Class are packed for each alert rule.

Example of a Custom Action

Alerts can be customized so that they are generated on certain tables, and they are recorded in a dedicated alert log table.

The following example is a step-by-step demonstration of how a custom action can be added. It does not provide a real-world example of an alerts customization in Microsoft Dynamics AX. A more realistic example of a custom action would be to send alerts as instant messages or into a Really Simple Syndication (RSS) feed.

  1. Create your custom action class by using the following code.
        class EventActionLogging extends EventAction
        {
        }
  1. Implement the execute method by using the following code.
        public void execute(
            EventRule   eventRule,
            EventType   eventType,
            Common      buffer,
            EventInbox alertInbox,
            EventAlertCreatedDate alertCreatedDate = systemdateget(),
            EventAlertCreatedTime alertCreatedTime = timenow()
            )
        {
        
            EventAlertLog alertLog;
        
            if (eventRule.isLogged)
            {
        
                ttsbegin;
                    alertLog.RuleId = eventRule.RuleId;
                    alertLog.InboxId = alertInbox.InboxId;
                    alertLog.EventSubject = alertInbox.Subject;
                    alertLog.EventMessage = alertInbox.Message;
                    alertLog.insert();
                ttscommit;
        
            }
        
        }
  1. Modify the logic that adds action IDs to rules in the EventCreateRule class by using the following code.
        protected boolean initEventRule()
        {
        
        List actions = new List(Types::Integer);
        
            // The order that actions are executed in is determined
            // by the order in which your classIds are added to the list.
            actions.addEnd(classnum(EventActionAlert));
            actions.addEnd(classnum(EventActionEmail));
        
            // Your custom actions are executed only if a rule is created
            // for only one of the following tables.
            if (callerTableId == tableNum(LedgerTable))
            // || callerTableId == tableNum(Table2)
            // || callerTableId == tableNum(Table3)
                actions.addEnd(classNum(EventActionLogging));
        
            
    
        }

Note

If you also want to send your alerts as instant messages, implement a Real Time Communication client DLL that communicates with your Live Communication Server. By using either .NET or the COM interoperability in Microsoft Dynamics AX, call the methods in your client DLL from the execute method.

See also

How to: Add Custom Actions UI for Alerts

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.