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
3 hours ago
Hi @Omkar Jori
Any particular reason you r opting for onChange script for this use case?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @Mohammed8 ,
I have a custom conflict status configuration based on the service using different client script and script includes,
On every form load conflict is getting checked as per the requirements, and the requirement is to trigger the notification at Authorize state even the conflict is detected earlier.
I tried adding the conditions directly in notification but not working, the code is not properly setting the values for conflict status fields, only setting up every time form loads.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Since there are multiple client scripts at play on same field, it may be getting triggered multiple time. Maybe try to use oldValue !=3 and newValue=='3'.
I would recommend to prefer server-side business rule (After) for this use case.
Do let know if you find solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
Hi @Omkar Jori
For your use case- can you try this:
- Register an event
Navigate to System Policy > Events > Registry > Click New.
Event name: change.authorize.conflict
Table: change_request
Description: Triggered when a change moves to Authorize and a conflict is detected.
2.Trigger a Notification
- Navigate to System Notification > Email > Notifications.
- Click New.
- Set When to send to Event is fired.
- Select your Event name (change.authorize.conflict).
- Choose the specific users or groups to receive the alert (e.g., Change Managers) under the Who to receive tab.
3. Create the After – Insert/update Business Rule
- Navigate to System Definition > Business Rules>Click New.
- Name: Fire Event on Authorize Conflict
- Table: Change Request
- When: after
- Insert/Update: Check both
- Conditions
- State is Authorize
- Conflict status is Conflict
(function executeRule(current, previous /*null when async*/ ) {
if (current.state == 'authorize' && current.conflict_status == 'conflict') {
gs.eventQueue('change.authorize.conflict', current, current.number, current.assigned_to);
}
})(current, previous);
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti