Trigger email/event on field change using client script (without saving form)

Nilesh2
Tera Expert

Hello all ,

We can not directly trigger event using client script but we can do this by making Ajax call .

I'm sharing here a simple scenario to help us understand this concept easily.

 

Use case :

On an incident form, when a user changes the "Assigned To" field of an existing incident record, send an immediate alert email to the assignment group manager about the changes before saving the form.

This method can be used to send alert notifications when sensitive data is changed on a form during interactive user sessions.

 

Answer:

Step 1 : Create event for incident table. 

Navigate to -  All>System policy>Events>Registry.

Refer following screen shot for field values. 

 

Nilesh2_0-1673200754103.png

 

Step 2 : Create email notification for incident table . 

Navigate to -  All>System notification>Email>Notifications

Refer following screen shot for field values. 

2.1-when to send

Nilesh2_3-1673201514950.png

 

2.2-Who will receive

Nilesh2_2-1673201368753.png

2.3 -What it will contain

Nilesh2_4-1673202216433.png

 

Step 3 : Create  a client callable script include. 

Navigate to -  All>System definition>Script include

Refer following screen shot for field values. 

 

Nilesh2_5-1673202713039.png

//Script include script

var email_to_assignment_group_manager = Class.create();
email_to_assignment_group_manager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
triggeremail: function() {
var sysid = this.getParameter('sysparm_recordsys_id');
var incdata = new GlideRecord('incident');
incdata.get(sysid);
gs.eventQueue('emailtomanager',incdata);
return;
},
type: 'email_to_assignment_group_manager'
});

 

Step 4: Write a on change client script and make ajax call send notification . 

Navigate to -  All>System definition>Client script.

Refer following screen shot for field values. 

 

Nilesh2_8-1673204977966.png

// on change client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (!g_form.isNewRecord()) {
var eventcall = new GlideAjax('email_to_assignment_group_manager');
eventcall.addParam('sysparm_name', 'triggeremail');
eventcall.addParam('sysparm_recordsys_id', g_form.getUniqueValue());
eventcall.getXML();
}
else
return false;
}

 

Step 5: Now Open an existing incident record and change the  value of assigned to user .

 

Step 6: Validate that email is triggered to Assignment group manager.

Navigate to -  All>System logs>Emails.

Check that your email is triggered/send to assignment group manager by refreshing the list . it is an asynchronous call so it will take some time to trigger event.

Nilesh2_7-1673204849439.png

 

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.

 

Regards
Nilesh

 

1 ACCEPTED SOLUTION

Hi sekhar,

In ServiceNow, business rules are used to define server-side logic .If you want to trigger an event after a record is saved (here if value is changed), you would typically use an "after" business rule but Best practice is "async" BR.

On the other hand, Client scripts can be used to perform actions or validations before or after the record is saved. If you want to trigger an event just after changing a value and before saving the record, you might use an "onChange" client script (As shown in Blog).

View solution in original post

3 REPLIES 3

RESHMA GADKAR
Tera Contributor

Hi Nilesh, it is very helpful 

Thank you

sekharksnow
Tera Contributor

Hi Nilesh,

I want to know that, the event will trigger after saving the record, or before saving and just after the change of value.

As of my knowledge it will trigger just after changing the value and before saving the record. I know you just give an example, but what is the best way to call the event for the above scenario.

Hi sekhar,

In ServiceNow, business rules are used to define server-side logic .If you want to trigger an event after a record is saved (here if value is changed), you would typically use an "after" business rule but Best practice is "async" BR.

On the other hand, Client scripts can be used to perform actions or validations before or after the record is saved. If you want to trigger an event just after changing a value and before saving the record, you might use an "onChange" client script (As shown in Blog).