Line of code in a Business Rule which to automatically refresh the record page after the Business Rule finishes its job?

georgimavrodiev
Mega Guru

Hello all,

I have a custom choice of "Implement" in the Change Task State field.
I had a requirement to ensure that once a user opens a Change Request record, which is in "Scheduled" State and goes to the Change Tasks Related List and changes the states of all the Change Tasks presented there to "Implement" -> then, automatically the Change Request itself will be moved to "Implement" State.
I was able to cover this goal by creating a Business Rule against Change Task (change_task) table. It works perfectly, but it does not refresh the page once it finishes its work.
In other words I see the following once the States of all related Change Tasks were moved to "Implement", and I need to reload the form of the Change Request in order for the change in the State field to be updated:
2017-11-27 17_31_37-CHG0030351 _ ServiceNow.png

I already tried couple of approaches and the result continues to be negative.

So, could you please provide me with a line of code which can be executed at the end of the Business Rule and to refresh the page? Thank you!

Regards, Georgi

1 ACCEPTED SOLUTION

It was my understanding that a Change Manager would be manually setting the Tasks associated with the change to have the Implement State. This is why I proposed using the Wait condition within the workflow. It appears that once all of the related tasks are in Implementation State, the Change Request should then be in Implement state. If this is your business process, then we should be able to leverage a Business Rule and a Wait condition.



1.   Business rule: Set Change when State is Implement


      -     Create an onAfter business rule that runs when the state changes to Implement


      -     This will 'touch' the Change record so that the Wait condition will re-check.


      -     I will use the state value of 10 in my example since I do not know what your Implement state is.


      -     Here is a sample code:


      Condition: current.state.changesTo(10)


      Script:


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


      var notesTxt = 'Change Task ' + current.number + ' set to: ' + curent.state.getDisplayValue();


      var chgObj = new GlideRecord('change_request');


      chgObj.get(current.change_request);


      chgObj.work_notes = notesTxt;


      chgObj.update();


})(current, previous);



2.   Script Include: ChangeUtils


      -     As of Geneva, there is an existing Script Include that can be used with Change Request: ChangeUtils


      -     Adding additional methods to this Script Include will allow you to call it from within any workflow.


      -     Having your script outside of the workflow allows you to edit the function without having to check out the workflow.


      -     Below are two methods you can add to this Script Include:


      closedTasks: function(chgObj) {


              var cTask = new GlideRecord('change_task');


              cTask.addQuery('change_request', chgObj.sys_id);


              cTask.query();


              var rowCount = cTask.getRowCount();


              var cCount = 0;


              while (cTask.next()) {


                      if (cTask.active == false) {


                              cCount++;


                      }


              }


              var answer = false;


              if (rowCount == cCount && rowCount > 0) {


                      answer = true;


              }


              return answer;


      },



      implementTasks: function(chgObj) {


              var cTask = new GlideRecord('change_task');


              cTask.addQuery('change_request', chgObj.sys_id);


              cTask.addQuery('active', true);


              cTask.query();


              var rowCount = cTask.getRowCount();


              var iCount = 0;


              while (cTask.next()) {


                      if (cTask.state == 10) {


                              iCount++;


                      }


              }


              var answer = false;


              if (rowCount == iCount && rowCount > 0) {


                      answer = true;


              }


              return answer;


      },



3.   Wait Workflow Activities:


      -     If you choose to use the methods from the Script Include, you can call them from the Wait Workflow Activity.


      -     For checking if all Change Tasks are in a state of Implement, use the following syntax:


var sInc = new ChangeUtils();


answer = sInc.implementTasks(current);



      -     It is unclear if your records are set to be inactive when set to be Implement.


      -     You may also want to have an additional wait that waits for all tasks to be closed.


      -     For checking if all Change Tasks are closed, use the following syntax:


var sInc = new ChangeUtils();


answer = sInc.closedTasks(current);



4.   Set Values Workflow Activity:


      -     If the desire is to automatically set the state of the Change record after all related tasks are set to Implement, then you can do so with a Set Values Workflow Activity.




Hopefully this will give you enough of a sample to work with.


View solution in original post

5 REPLIES 5

Hello Christopher,





I found out an interesting article about a known issue:
ServiceNow KB: gs.setRedirect() in combination with current.setAbortAction(true) not executed (KB053... .




Anyway, I will mark your latest reply as the "Correct answer" of this thread, as it was really useful to me.
Thank you very much for taking your time to share your thoughts in details with me!




Regards, Georgi