Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

multiple records update on setWorkflow(false)

Mohammed Lais1
Mega Guru

Hi

i am trying to update multiple ritm records through workflow.

i am using the below code.

var req = current.request;

  var gr = new GlideRecord('sc_req_item');

  gr.addQuery('request',req);

  gr.orderBy('number');

  gr.query();

  while(gr.next()){

  gr.variable_pool.test_access_created="true";

  gr.update();

  }

this is updating only one record.but the query is fetching multiple records.

if i add

gr.setWorkflow(false);

before gr.update();

all the records are getting updated perfectly.

can we update the multiple records without making the workflow as false?

12 REPLIES 12

jayfreise
Mega Contributor

Hi,


Ok, so that must mean there are business rules preventing them from being updated, right?


-J


josh_nerius
ServiceNow Employee
ServiceNow Employee

Hi Mohammed,



Try surrounding your update code in a try/catch block. Something like this:



var req = current.request;


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request',req);


gr.orderBy('number');


gr.query();


while (gr.next()) {


      try {


              gr.variable_pool.test_access_created="true";


              gr.update();


      } catch (ex) {


              gs.log("Caught exception " + ex.message + " on " + gr.number, "TheNameOfYourWorkflow");


      }


}



This should catch the bad update gracefully while allowing the other updates to complete, and will help pinpoint which record is causing the error. After the code runs, take a look at your system logs to see which record is the culprit, and what the error is.


Mohammed Lais1
Mega Guru

Hi Josh,



Thanks for your inputs.


i came to know that one workflow is stopping to update the multiple records.



I will try to explain the test case.



there are 5 catalog items in one order guide. if i submit the order guide all 5 request items get generated.


the requirement is 4 catalog items(2nd to 5th) should be on hold until the first catalog item task gets closed.



what i tried to do was,


        1. In 1st item workflow after closing the task i have added one run script activity to update one common catalog variable which is present in all 4 catalog items.(by using the above code.)


        2. the rest 4 catalog items has same workflow which has a wait for condition activity. This activity checks whether the variable which is getting updated from 1st workflow. if it matches then the workflow continues.



The issue is,


        the 1st item workflow is updating the ritm record of 2nd catalog item, then in 2nd workflow it is checking the condition and the 2nd item workflow is moving forward. but the 3rd,4th and 5th item records are not getting updated ( the second workflow is stopping the remaining updates to happen.)



(If i removed the wait for condition activity from 2nd workflow, all records got updated through 1st workflow)



Is this any workflow thread issue or anything else?



(But in different dev instance the above combination of workflows is working perfect).


ctomasi


Hi Chuck,
please provide your inputs to above issue.


Hi Mohammed,



Have you thought about making the workflows (on 2-5) waitfor an event then trigger the event from a BR run from #1?



Trigger Workflow Events