When a Change Request is completed, the related Incident record should be updated.

keerthana10
Tera Contributor
  1. When a Change Request is completed, the related Incident record should be updated.
  2. 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:

 

(function executeRule(current, previous /*null when async*/)
{
    var changeRequest = current;
  if (changeRequest.state == '3') // || changeRequest.state == 'Closed')
    gs.addInfoMessage('state check');
    {
    var incident = new GlideRecord('incident');
    incident.addQuery('change_request', current.sys_id);
    incident.query();
   gs.addInfoMessage('incident check');
    if (incident.next())
    {
    incident.short_description = 'Change Request ' + current.number + ' completed';
    incident.update();
    }
    else
    {
    var newIncident = new GlideRecord('incident');
    newIncident.initialize();
    newIncident.short_description = 'New Incident related to Change Request ' + current.number;
    newIncident.change_request = current.sys_id; // Associate the Change Request
    newIncident.insert();
    }
    gs.addInfoMessage("else cgecj");
    }
    if (current.state == '4') //|| changeRequest.state == 'Rejected')
   {

    var incident = new GlideRecord('incident');
    incident.addQuery('change_request', current.sys_id);
    incident.query();
    if (incident.next())
    {
    incident.short_description = 'Change Request ' + current.number + ' failed';
    incident.update();
    }
    else
    {
    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();
    }
   
}
})(current, previous);
3 REPLIES 3

Simran Gadodiya
Mega Sage

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

Debasis Pati
Tera Guru

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

When to run condition?