D365 Finance & Operation

Customizing D365 Finance and Operations user alerts.

Requirement:

When ever a field value is changed D365 Finance and operation has to send alert to the respective user over the email with old values to new values changed.

Solution:

We used changed based alert functionality available out of the box in D365 finance and operation with customizing a small part of the message which can includes the field value changed from old value to new value in the alert message.

below extension code of class “EventActionAlert” method “execute”, works to include the old and new value in the alert message.

[ExtensionOf(classStr(EventActionAlert))]
final class EventActionAlert_Model_Extension
{
    public void execute(EventRule eventRule,EventType eventType,Common buffer,EventInbox inbox,EventAlertCreatedDateTime alertCreatedDateTime) 
    {
        EventCUD eventCud;
        EventRuleField ruleField;
        fieldId     fieldId;
        EventMessage message;

        select eventCud
            where eventCud.CudRecId == buffer.RecId && eventCud.CudTableId == eventRule.AlertTableId;

        select forupdate ruleField
            where ruleField.RuleId == eventRule.RuleId && ruleField.uTableId == eventRule.AlertTableId
                && (ruleField.uFieldId == eventRule.AlertFieldId);

        if (ruleField && eventCud)
        {
            var dbLogManager = Microsoft.Dynamics.Ax.Xpp.SysDatabaseLog.SysDatabaseLogManager::CreateInstance();
            var parser = dbLogManager.GetParser(tableId2Name(eventCud.CudTableId), eventCud.NewData);
            System.Object[] fieldValues;
            container newValue;
            container oldValue;
            System.String fieldName;
            System.String oldString, newString;
            fieldValues = parser.GetNextField();
            while (fieldValues != null)
            {
                fieldName = fieldValues.GetValue(0);
                fieldName = fieldName.Trim();
                fieldId = fieldName2Id(eventCud.CudTableId, fieldName);
                
                if (fieldId == ruleField.uFieldId)
                {
                    var dictField = new SysDictField(eventCud.CudTableId, fieldId);
                    if (dictField.rightAligned())
                    {
                        newString = fieldValues.GetValue(1);
                        oldString = fieldValues.GetValue(2);
                        newValue = [newString.TrimStart()];
                        oldValue = (eventCud.Event == EventLogType::Insert) ? conNull() : [oldString.TrimStart()];
                    }
                    else
                    {
                        newValue = [fieldValues.GetValue(1)];
                        oldValue = (eventCud.Event == EventLogType::Insert) ? conNull() : [fieldValues.GetValue(2)];
                    }
                    if (fieldId != 0)
                    {
                        if (newValue != oldValue && (fieldName != 'RecVersion'))
                        {
                            message = strFmt(strFmt('%1 changed from %2 to %3',eventRule.AlertFieldLabel,conPeek(oldValue, 1).ToString(),conPeek(newValue,1).ToString()));
                        }
                    }
                }
                fieldValues = parser.GetNextField();
            }
        }
        
        eventRule.Message = message;

        next execute(eventRule,eventType,buffer,inbox,alertCreatedDateTime);
    }

}

Result:

Thanks,

Happy Daxing with Rizz 🙂

Leave a comment