Business Rule

Mark Wood
Tera Contributor

 

Hello Experts,

I'm currently working on the following requirements:

"When a change record is created for implementing a problem resolution, the problem ticket cannot be closed until the change is in a Closed-Complete state."

To achieve this requirement, I've written a before-update business rule with the condition that the state changes to resolved. However, it seems that my code is not functioning as expected. Could anyone please provide guidance on this?

Thank you.

(function executeRule(current, previous /*null when async*/) {
 
    var cr=new GlideRecord('change_request');
    cr.addQuery('parent',current.sys_id);
    cr.query();
    while(cr.next())
    {
if(cr.state!=3)//not closed
{

gs.addErrorMessage("Associated Change is not resolved, Please resolve change to proceed with problem resolution");

current.state=104;
current.update();
current.setAbortAction(true);
 
}
else{
gs.addInfoMessage("You can Close Problem");
 
}
 
    }
 
})(current, previous);
8 REPLIES 8

Harish KM
Kilo Patron
Kilo Patron

Hi @Mark Wood how are you associating the change? Are you creating change request from Problem via UI Action? How are you linking the change request to Problem? Which field holds the relationship?

your BR will look like this on problem table

   var chg = new GlideRecord('change_request');
    chg.addQuery('parent', current.sys_id);// Make sure parent field has problem record
    chg.addEncodedQuery('stateIN-5,-4,-3,-2,-1,0'); //not closed/cancelled
    chg.query();
    while (chg.next()) {
        gs.addErrorMessage("Change Request is active" + chg.getValue('number'));
        current.setAbortAction(true);
    }
Regards
Harish

Aman Kumar S
Kilo Patron

Hi @Mark Wood 

You can try below script,

Hope you are using Before Update BR on the problem table.

var cr=new GlideRecord('change_request');
    cr.addQuery('parent',current.sys_id);
   cr.addQuery("state", "!=", "3");
    cr.query();
    if(cr.next()){
           current.setAbortAction(true);
gs.addErrorMessage("Associated Change is not resolved, Please resolve change to proceed with problem resolution");
}
else{
      gs.addInfoMessage("You can Close Problem");
 }
 
 
Best Regards
Aman Kumar

Pratiksha2
Mega Sage

Hello @Mark Wood 

Create an Before Update Business rule on Problem table and try using below script: add when to run condition as State is Closed

 

(function executeRule(current, previous /*previous is not used in this example*/) {
 
    if (current.getTableName() === 'problem') {
        var changeGR = new GlideRecord('change_request');
        changeGR.addQuery('parent', current.sys_id); // Assuming 'problem_id' is the reference field to problem table in change_request table
        changeGR.query();
        var allChangesClosedComplete = true;
        while (changeGR.next()) {
            if (changeGR.state != 3 /*assuming 3 is the Closed-Complete state*/) {
                // Set the flag to false and break out of the loop
                allChangesClosedComplete = false;
                break;
            }
        }
        
        // Check if all associated change requests are in "Closed-Complete" state
        if (allChangesClosedComplete) {
            // Allow closing the problem ticket
            return;
        } else {
            // Prevent closing the problem ticket and display an error message
            current.setAbortAction(true);
            gs.addErrorMessage('Cannot close the problem ticket until all associated changes are in a "Closed-Complete" state.');
            return;
        }
    }
})(current, previous);

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Pratiksha

Pratiksha2
Mega Sage

Hello @Mark Wood -

I noticed that you marked my answer as helpful.
So if resolves your query then please Accept my solution ✔️ as well and close the thread to help future readers to understand.

Thank You!