GlideRecord script not updating item even though debug statements showing updated field.

willkara
Kilo Contributor

So I'm trying to update an items state through a Business Rule Script. I can succesfully POST to the table and the script gets call, but nothing is updated on the resultant item(s).

Here's an idea of structure of the data:

  1. Post Request to #2 to insert data >>>
  2. Request Status History >>>
    1. Business Rule below gets ran on AFTER , Insert/Update.
  3. Catalog Request Order >>>
  4. Requested Item

A user would make a POST request to #1 which records the histories of the changes. This gets successfully written to the table, it then queries the DB for the correct #2 to reference and successfully references/links it in the table.

The problem is coming up when I'm trying to change the state of #3, which is referenced in #2.

Here's a snippet of the code I'm using to update the states. I've also followed it with a picture of the tables and how the reference fields are created.

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

try {

var getID = current.request_id;

var receivedStatus = current.status;

var gr = new GlideRecord("x_dci_scscd_catalo_catalog_request_order");

gr.addQuery("request_id", getID);

gr.query();

if (gr.next()) {

        gs.info("FOUND SOMETHING");

        if(receivedStatus == "complete"){

                  //Debugging Statements...

                  //These two print out the correct values, but it isn't being saved

                  gs.info("FOUND COMPLETE");

                  gs.info("FIRST: " + gr.item_information.state);

                  //Queried Object >>   Referenced Requested Item >> State

                  //This part doesn't seem to be saving back to the item, while the following info statement is showing the change.

                  gr.item_information.state = '3';

                  gs.info("SECOND: " +gr.item_information.state);

        }

        else if(receivedStatus == "cancelled"){

                  gr.item_information.state = '7';

        }

        //Ignore this, just havent changed it yet....

        else if(receivedStatus == "error"){

                  gr.item_information.stage = "dci_catalog_request_error";

        }

else{

gs.info("Error with request, please check request: " + getID);

}

gr.update();

current.catalog_request_item = gr.sys_id;

}

current.autoSysFields(false);

current.setWorkflow(false);

current.update();

} catch(err) {

gs.error("Something broke while trying to change the request status: " + err);

}

})(current, previous);

corItemInfo.PNG

1 ACCEPTED SOLUTION

Nate23
Mega Guru

I think I realize what you are trying to do...



so here is some revised code:



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


try {


var getID = current.request_id;  


var receivedStatus = current.status;  


 


var gr = new GlideRecord("x_dci_scscd_catalo_catalog_request_order");  


 


gr.addQuery("request_id", getID);  


gr.query();



var myRitm = new GlideRecord('sc_req_item');      


var receivedStatus = current.status;





if(!gr.next())


return;



if(myRitm.get(gr.item_information.sys_id )) {  


        gs.info("FOUND SOMETHING");  




 


        if(receivedStatus == "complete"){  


 


                  //Debugging Statements...  


                  //These two print out the correct values, but it isn't being saved  


                  gs.info("FOUND COMPLETE");  


                  gs.info("FIRST: " + myRitm.state);  


 


                  //Queried Object >>   Referenced Requested Item >> State  


                  //This part doesn't seem to be saving back to the item, while the following info statement is showing the change.  


     


myRitm.state = '3';  


 


                  gs.info("SECOND: " +myRitm.state);  


 


 


        }  


        else if(receivedStatus == "cancelled"){  


                  myRitm.state = '7';  


        }  


 


        //Ignore this, just havent changed it yet....  


        else if(receivedStatus == "error"){  


                myRitm.stage = "dci_catalog_request_error";  


        }  


else{  


gs.info("Error with request, please check request: " + getID);  


}  


 


 


myRitm.update();  


current.catalog_request_item = myRitm.sys_id;  


}  


current.autoSysFields(false);  


current.setWorkflow(false);  


current.update();  


 


 


 


} catch(err) {  


gs.error("Something broke while trying to change the request status: " + err);  


}  


})(current, previous);  


View solution in original post

6 REPLIES 6

sure, so as you pointed out you needed to update Request Status History >> Catalog Request Item >> Requested Item (change state here). so in order to do an update on the requested item record you needed to pull the GlideRecord object so you could perform the update(). in your initial code you were trying to dot walk which is fine for pulling information but not for posting. to post you need the true glide object. so here we performed basically two nested queries. I may be making this all up though...


willkara
Kilo Contributor

Ahhhh ok. Good catch. So dot walking is only ok for GETTING info, but not SETTING info.



I'll mark that one down in my books. I wish they covered that in my ServiceNow Scripting Training last week.



Thanks for the assist!