When a Change Request is completed, the related Incident record should be updated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 02:34 AM
- When a Change Request is completed, the related Incident record should be updated.
- If the Change Request fails:
- If an Incident is already related, it should be updated.
- If no Incident exists, a new Incident should be created. For this Requirement i have done this script part but its not working , kindly any one please help me on this.
script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 02:41 AM
Hello @keerthana10
You can try below mentioned script.If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
(function executeRule(current, previous /*null when async*/) {
var changeRequest = current;
// Check if the Change Request is completed (state '3')
if (changeRequest.state == '3') { // '3' is typically 'Completed' state, adjust as needed
gs.addInfoMessage('State check: Completed');
// Query for related incident
var incident = new GlideRecord('incident');
incident.addQuery('change_request', current.sys_id);
incident.query();
if (incident.next()) {
// If related incident exists, update it
incident.short_description = 'Change Request ' + current.number + ' completed';
incident.update();
gs.addInfoMessage('Incident updated for completed Change Request');
} else {
// If no related incident, create a new one
var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'New Incident related to Change Request ' + current.number;
newIncident.change_request = current.sys_id;
newIncident.insert();
gs.addInfoMessage('New Incident created for completed Change Request');
}
}
// Check if the Change Request failed (state '4')
if (changeRequest.state == '4') { // '4' is typically 'Failed' state, adjust as needed
gs.addInfoMessage('State check: Failed');
// Query for related incident
var incident = new GlideRecord('incident');
incident.addQuery('change_request', current.sys_id);
incident.query();
if (incident.next()) {
// If related incident exists, update it
incident.short_description = 'Change Request ' + current.number + ' failed';
incident.update();
gs.addInfoMessage('Incident updated for failed Change Request');
} else {
// If no related incident, create a new one
var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'Incident created due to failed Change Request ' + current.number;
newIncident.change_request = current.sys_id;
newIncident.insert();
gs.addInfoMessage('New Incident created for failed Change Request');
}
}
})(current, previous);
Regards,
Simran

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 02:48 AM
Hello @keerthana10 ,
Please use the below script and check
(function executeRule(current, previous /*null when async*/) {
var changeRequest = current;
// Check if Change Request is completed
if (changeRequest.state == 3) { // Assuming 3 means 'Completed'
gs.log('Change Request is completed');
var incident = new GlideRecord('incident');
incident.addQuery('change_request', current.sys_id);
incident.query();
if (incident.next()) {
gs.log('Updating existing related Incident');
incident.short_description = 'Change Request ' + current.number + ' completed';
incident.update();
} else {
gs.log('No existing Incident found, creating a new one');
var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'New Incident related to Change Request ' + current.number;
newIncident.setValue('change_request', current.sys_id); // Ensure this field exists
newIncident.insert();
}
}
// Check if Change Request failed
if (changeRequest.state == 4) { // Assuming 4 means 'Failed'
gs.log('Change Request failed');
var incident = new GlideRecord('incident');
incident.addQuery('change_request', current.sys_id);
incident.query();
if (incident.next()) {
gs.log('Updating existing Incident due to failure');
incident.short_description = 'Change Request ' + current.number + ' failed';
incident.update();
} else {
gs.log('No existing Incident found, creating a new one for failure');
var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'Incident created due to failed Change Request ' + current.number;
newIncident.setValue('change_request', current.sys_id);
newIncident.insert();
}
}
})(current, previous);
if this helps you kindly mark it as helpful/correct.
Regards,
Debasis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:09 AM
When to run condition?