The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Notification to a group when field changes in particular table

Priyanka Chaud1
Tera Contributor

Hi Servicenow Experts,

I have a requirement for my cmdb table...When 4 fields on the form has been changed they want to send notification to a group with the old and new value of that field that has been changed on the record.

For this i created a event, there BR after update on cmdb table here is the BR script...not sure what to put in eventqueue syntax is correct? Also after this need to create email script so need help on that as i am not getting what to code in email script to get that data to create notification. Can someone please help me on this req it would be great help. Thanks @Ankur Bawiskar @sohail_mohamad @HrishabhKumar @Sandeep Rajput @Anil Lande 

Attaching br so far what i have done:-

var arr=[];
    if(current.name.changes()){
      var temp1={};
     temp1['field']='name';
    temp1['oldvalue']= previous.name;
    temp1['newvalue']= current.name;
    arr.push(temp1);
    }
    if(current.install_status.changes()){
      var temp2={};
     temp2['field']='install_status';
    temp2['oldvalue']= previous.install_status;
    temp2['newvalue']= current.install_status;
    arr.push(temp2);
    }

    if(current.support_group.changes()){
      var temp3={};
     temp3['field']='support_group';
    temp3['oldvalue']= previous.support_group;
    temp3['newvalue']= current.support_group;
    arr.push(temp3);
    }
    if(current.u_sla_definition.changes()){
      var temp4={};
     temp4['field']='u_sla_definition';
    temp4['oldvalue']= previous.u_sla_definition;
    temp4['newvalue']= current.u_sla_definition;
    arr.push(temp4);
    }
    gs.eventQueue('ba.notification.group', current, arr.push());

 

1 ACCEPTED SOLUTION

@Priyanka Chaud1 

try this

I have used .toString() whenever you are getting the value from current and previous object

Also note support_group is reference so it will give sysId so if you want to show group name then use getDisplayValue()

Same goes with install_status which is a choice type; if you simply get the value you will get choice value and not choice label; so I used getDisplayValue() there as well

var arr = [];
if (current.name.changes()) {
    var temp1 = {};
    temp1['field'] = 'name';
    temp1['oldvalue'] = previous.name.toString();
    temp1['newvalue'] = current.name.toString();
    arr.push(temp1);
}
if (current.install_status.changes()) {
    var temp2 = {};
    temp2['field'] = 'install_status';
    temp2['oldvalue'] = previous.install_status.getDisplayValue();
    temp2['newvalue'] = current.install_status.getDisplayValue();
    arr.push(temp2);
}
if (current.support_group.changes()) {
    var temp3 = {};
    temp3['field'] = 'support_group';
    temp3['oldvalue'] = previous.support_group.getDisplayValue();
    temp3['newvalue'] = current.support_group.getDisplayValue();
    arr.push(temp3);
}
if (current.u_sla_definition.changes()) {
    var temp4 = {};
    temp4['field'] = 'u_sla_definition';
    temp4['oldvalue'] = previous.u_sla_definition.toString();
    temp4['newvalue'] = current.u_sla_definition.toString();
    arr.push(temp4);
}

gs.eventQueue('ba.notification.group', current, JSON.stringify(arr));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

HIROSHI SATOH
Mega Sage

Please use this as a reference. Operation has not been confirmed.

 

Business Rule Configuration:

  • Ensure your Business Rule (BR) is configured to run after the update on the CMDB table. You've correctly set up the script to detect changes in the fields.
  • However, the gs.eventQueue line needs some modification. You're trying to send the array arr to the event, but using arr.push() inside the gs.eventQueue is incorrect. Instead, y

 

var arr = [];
if (current.name.changes()) {
    var temp1 = {};
    temp1['field'] = 'name';
    temp1['oldvalue'] = previous.name;
    temp1['newvalue'] = current.name;
    arr.push(temp1);
}
if (current.install_status.changes()) {
    var temp2 = {};
    temp2['field'] = 'install_status';
    temp2['oldvalue'] = previous.install_status;
    temp2['newvalue'] = current.install_status;
    arr.push(temp2);
}
if (current.support_group.changes()) {
    var temp3 = {};
    temp3['field'] = 'support_group';
    temp3['oldvalue'] = previous.support_group;
    temp3['newvalue'] = current.support_group;
    arr.push(temp3);
}
if (current.u_sla_definition.changes()) {
    var temp4 = {};
    temp4['field'] = 'u_sla_definition';
    temp4['oldvalue'] = previous.u_sla_definition;
    temp4['newvalue'] = current.u_sla_definition;
    arr.push(temp4);
}

gs.eventQueue('ba.notification.group', current, JSON.stringify(arr));

 

Event Configuration:

  • Go to System Policy > Events > Event Registry and create an event called ba.notification.group.
  • In the event's Description field, you can describe what this event does.

Email Notification:

  • Create an Email Notification triggered by the ba.notification.group event.
  • Inside the email, you can use an Email Script to extract the old and new values from the event's data.

Here's a sample Email Script to extract the data from the event's parameter:

 

(function() {
    var changeDetails = JSON.parse(event.parm2);
    var message = '';

    for (var i = 0; i < changeDetails.length; i++) {
        message += 'Field: ' + changeDetails[i].field + '\n';
        message += 'Old Value: ' + changeDetails[i].oldvalue + '\n';
        message += 'New Value: ' + changeDetails[i].newvalue + '\n\n';
    }

    template.print(message);
})();

 

This script will generate a message with details of all the changed fields and their old and new values. You can place this script in the email's body.

Hi @HIROSHI SATOH tysm for reponse. As you said i created a event with name and than created a email script and notification in notification i am calling the script through this ${mail_script:notify_ba_change_field} and also made that change in BR..Attaching email script as well. Notification is getting triggered but its coming empty. Can you check if i did everything right or any mistake??

Hey @HIROSHI SATOH @Ankur Bawiskar is it possible to display this data in table format in email??The data present in email script?

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka Chaud1 

in the email script are you parsing the JSON correctly?

what came in logs?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader