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.

I am trying to create a background script for closing a change, for which workflow has broken

sunthosh
Tera Contributor

Hello

I am trying to create a background script for closing a change, for which workflow has broken. Advise on it please.

function getUserRegion() {

  var u = gs.getnumber();

  var gr = new GlideRecord('change_request');

  gr.addQuery('CHG00046',u);

  gr.query();

  while (gr.next()) {

          gr.u_stage= 7;

          gr.update();

  }      

1 ACCEPTED SOLUTION

If you need to change the state, then update the script to use the state field instead.



var gr = new GlideRecord('change_request');


    if (gr.get('number', 'CHG00046')) {


          gr.state= 7;


          gr.update();


View solution in original post

14 REPLIES 14

If you are just running it from scripts background, you can either a) leave off the function wrapper, b) change it to an anonymous function.



  var gr = new GlideRecord('change_request');


    if (gr.get('number', 'CHG00046')) {


          gr.u_stage= 7;


          gr.update();



By using the new GlideRecord('change_request') you are telling it you are about to interact with the change_request table.


Yeah, it appears to be correct, so long as your Change Request numbers are only five digits.   Otherwise it would be 'CHG0000046'.


Since you expect one and only one result, while (gr.next()) could simply be if (gr.next()). Chuck's code is plug-and-play, so you can copy his if you want to be sure.



(ctomasi beat me to it again.. )


sunthosh
Tera Contributor

Good day



When in used chuck code, the stage is not changing. It is accepting the numbers what we give in - "gr.u_stage= 7;"(Here in the field it takes 7).


find_real_file.png


I need to change the state mainly rather than stage.



Thanks


gr.state=3 should update the state to closed.


You should review the sys_choice table, element - 'state', table change_request to verify that state=3 corresponds to 'Closed'.


Thanks for the clear update Chris