Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue with the email script

Prithvi Ramesh1
Mega Sage

Business rule - 

(function executeRule(current, previous /*null when async*/ ) {

    var fields = current.getFields();
    for (var i = 0; i < fields.size(); i++) {
        var field = fields.get(i);
        if (field.changes()) {
            var name = field.getName();
            var fieldLabel = field.getLabel(); // Get the field's label
            var prev = previous.getDisplayValue(name); // Get the previous value of the field
            var value = field.getElementValue(name); // Returns the unformatted value of the element
            var disValue = field.getDisplayValue();
            gs.addInfoMessage('Field Name: ' + fieldLabel + 
                              ' | Previous Value: ' + prev + 
                              ' | Current Value: ' + disValue);
            gs.eventQueue("field.changes", current, prev, disValue);
        }
    }
})(current, previous);

PrithviRamesh1_0-1747250053070.png

Notifications - 

PrithviRamesh1_1-1747250084951.pngPrithviRamesh1_2-1747250101881.png

 

Notification Email script - 

(function runMailScript(current, template, email, email_action, event) {
    // Add your code here
    var fields = current.getFields();
    var fieldLabel = ""; // Initialize fieldLabel variable
    for (var i = 0; i < fields.size(); i++) {
        var field = fields.get(i);
        if (field.changes()) {
            fieldLabel = field.getLabel(); // Get the field's label
            break; // Exit loop after finding the changed field
        }
    }
    
    var emailBody = "Hello,\n\n" +
                    "This is to inform you that the value of the field " + fieldLabel + " has changed.\n\n" +
                    "Previous Value: " + event.parm1 + "\n" +
                    "New Value: " + event.parm2 + "\n\n" +
                    "Please review the changes at your earliest convenience.\n\n" +
                    "Best regards,\n" +
                    "[Your Company Name]";
    
    template.print(emailBody);
})(current, template, email, email_action, event);

 Name - field_value_changed 

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Prithvi Ramesh1 ,

 

The correct syntax for including your Email Script would be:

 

${mail_script:field_value_changed}

 

Regards,

Robert

View solution in original post

2 REPLIES 2

Prithvi Ramesh1
Mega Sage

When the notification is triggered, then the body returns as empty.

Robert H
Mega Sage

Hello @Prithvi Ramesh1 ,

 

The correct syntax for including your Email Script would be:

 

${mail_script:field_value_changed}

 

Regards,

Robert