Need help with Change Request - Change Management form

Harsh3842
Tera Contributor

Hi All,

I need help with a requirement - the requirement is on the change request form when we are moving from implement to review state if that change request has any associated change task auto or manual if the task is not either closed or cancelled the change request state should not move from implement to review state Now to fix it 
I wrote a Business Rule on change_request table, before update with condition state changes to review
This is the script that I have written

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

     var implementState = -1; // 'Implement'
     var reviewState = 0;     // 'Review'

    // Only block if trying to move from Implement to Review
   if (current.state == reviewState && current.state.changesFrom(implementState)) {

         var taskGR = new GlideRecord('change_task');
         taskGR.addQuery('change_request', current.sys_id);
         taskGR.addQuery('state', 'NOT IN', '3,4'); // Allow only Closed (3) and Cancelled (4)
         taskGR.query();

         if (taskGR.hasNext()) {
             gs.addErrorMessage("All Change Tasks must be closed or cancelled before moving to the Review state.");
             current.state = previous.state; // Revert to previous state
             gs.setAbortAction(true); // Block the update
         }
     }

 })(current, previous);
this is working fine but There is a flow attached to change request by name change normal implement due to it I am getting an error message on change task table.  if I am closing either of the change task which are auto created (post implementation testing or implementation)
How can I avoid it so that irrespective whether there is auto task or manual task the change request does not  move to review state unless until all the change task are closed
And the flow is OOTB




1 ACCEPTED SOLUTION

Ehab Pilloor
Mega Sage

Hi @Harsh3842,

If you want the Flow to not be activated when BR is running then add 

 if (taskGR.hasNext()) {
             taskGR.setWorkflow(false);// Disables the Flow on Change Task
             gs.addErrorMessage("All Change Tasks must be closed or cancelled before moving to the Review state.");
             current.state = previous.state; // Revert to previous state
             gs.setAbortAction(true); // Block the update
         }

However, you need to check if disabling the Flow doesnt hamper critical processes before moving forward.

 

Regards,

Ehab Pilloor

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Harsh3842 

ensure flow is not having a race condition with your business rule

Also if your flow is trying to close the change request before the last task is fully closed, you may need to adjust the flow logic or use a short delay.

try this updated script

(function executeRule(current, previous) {
    var implementState = -1; // Update with your actual "Implement" state value
    var reviewState = 0;     // Update with your actual "Review" state value

    // Only block if moving from Implement to Review
    if (current.state == reviewState && previous.state == implementState) {
        // Use GlideAggregate for efficiency
        var agg = new GlideAggregate('change_task');
        agg.addQuery('change_request', current.sys_id);
        agg.addQuery('state', 'NOT IN', '3,4'); // Only count open tasks
        agg.addAggregate('COUNT');
        agg.query();
        if (agg.next() && agg.getAggregate('COUNT') > 0) {
            gs.addErrorMessage("All Change Tasks must be closed or cancelled before moving to the Review state.");
            gs.setAbortAction(true); // Block the update
        }
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar , thanks for your suggestion but this code is not working the change request is not moving to other states without checking the change task
any other solution.
I am thinking let's not touch the BR I created on change request table and create a new BR on change request table and try to put the post implementation test task to be closed last something like it

Vasu20
Tera Contributor

Hi @Harsh3842 ,

If the change tasks are active it should not allow the change request to be closed.This is the problem statement. 
The business rule you have written does the required action. 
The problem is that there is a flow designer in the background that is doing it?
Is my understanding right?

Thanks and Regards,
Vasudev S V

Harsh3842
Tera Contributor

Hi @Vasu20, Yes your understanding is correct The BR that I have wrote it is working great but when any of the auto generated task I am cancelling or closed on the change task table I am seeing the error message because i have written gliderecord on change task table which is correct but the BR is not triggering there are still manual and one auto task which is still in open or any other state and I have not moved the change request state from implement to review where I should see the error message. Hope you are understanding my issue