OnChange Client Script Triggering Multiple Times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi,
I have written the OnChange Client script that is triggering multiple times (3 times).
Requirement:
I want to trigger and event on Change request state is changed to "Authorize" and Conflict status is "Conflict".
My code ->
Client script:
Type- OnChange
Field- State
Code-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if(isLoading || newValue === ' ') {
return;
}
if (newValue == -3 && g_form.getValue("conflict_status" == "Conflict") {
var a = new GlideAjax('conflictCheckBasedOnService');
a.addParam('sysparm_name', 'triggerConflictNotification');
a.addParam('change_request', g_form.getUniqueValue());
a.getXMLAnswer(function(response) {
alert("Notification sent");
});
}
}
Script Include:
triggerConflictNotification: function() {
var change = this.getParameter("change_request");
var gr = new GlideRecord("change_request");
gr.addQuery("sys_id", change);
gr.query();
if(gr.next()) {
gs.eventQueue("conflict_detected", gr, "", "");
return;
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @Omkar Jori,
Since your requirement is to trigger a notification only when the Change Request is updated to the Authorize state, an onChange Client Script is not the ideal approach.
The State field can change multiple times during the client-side lifecycle (form load, client scripts, UI Policies, etc.), which can cause the script to execute multiple times.
A better approach is to use an After Update Business Rule with conditions
- State changes to Authorize
- Conflict Status = Conflict
Then call gs.eventQueue() from the Business Rule. This ensures the event is queued once, after the record is successfully updated in the database.
You Use:
g_form.getValue('conflict_status') == 'Conflict'instead of:
g_form.getValue("conflict_status" == "Conflict")