Link multiple change requests to an incident via rfc field

diana_tat
Tera Contributor

I want to link multiple change requests to an incident. I tried modifying the rfc field and make it list instead of reference, but it saves just one change. I know about the option to add a related list, but I still want the functionality between incident and change. Right now, there is a business rule that sets the state of the incident "work in progress" when the change is closed. So, i want to keep this and once all changes are close, put the state in "work in progress". Any idea how to do this and how to modify the field?

2 REPLIES 2

Juhi Poddar
Kilo Patron

Hello @diana_tat 

  • Modify the dictionary and change the type to List instead of reference.
  • This will allow to select multiple change records.JuhiPoddar_1-1732614524550.png
  • Modify the existing business rule as:

 

(function executeRule(current, previous /*null when async*/) {
    // Ensure the 'rfc' field is populated (which holds multiple change request sys_ids)
    if (!current.rfc) {
        return;
    }

    // Get all the Change Requests in the 'rfc' field
    var grChange = new GlideRecord('change_request');
    grChange.addQuery('sys_id', 'IN', current.rfc);  // Assuming 'rfc' stores sys_ids of related change requests
    grChange.query();

    var allClosed = true;
    
    // Check if all Change Requests are closed
    while (grChange.next()) {
        if (grChange.state != 'Closed') {  // Check if the state is not 'Closed'
            allClosed = false;
            break;
        }
    }

    // If all Change Requests are closed, set the Incident state to 'Work in Progress'
    if (allClosed) {
        current.state = 'Work in Progress';  // Update this to the desired state
        current.update();
    }

})(current, previous);
​

 

This will check if all the change records attached are closed and then only update the state to 'Work in Progress'.

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

Hi, Thanks for the reply. I deactivated the ootb rfc field and i have created a new one and made it list type. It works, i am able to save multiple changes now. The only problem is that the BR is not working. I have this code

 

(function executeRule(current, previous /*null when async*/) {

// Query the incidents linked to this change request
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('u_change_request', current.sys_id);
incidentGR.addQuery('state', 3); // On Hold
incidentGR.addQuery('hold_reason', 5); // Awaiting Change
incidentGR.query();

while (incidentGR.next()) {
// Check if all related Change Requests are closed
var grChange = new GlideRecord('change_request');
grChange.addQuery('sys_id', 'IN', incidentGR.u_change_request); // Ensure the related Change Requests are checked
grChange.query();

var allClosed = true;
while (grChange.next()) {
if (grChange.state != 'Closed') { // If any Change Request is not closed
allClosed = false;
break;
}
}

// If all Change Requests are closed, update the Incident
if (allClosed) {
incidentGR.state = 2; // Set state to 'Work In Progress'
incidentGR.work_notes = gs.getMessage(
'All related Change Requests have been closed. Incident state updated to Work In Progress.'
);
incidentGR.update(); // Save changes to the related incident
}
}
})(current, previous);
 
and it wont change the status to wip