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

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);  


willkara
Kilo Contributor

Nate,



JUST SAW YOUR CHANGED CODE, WILL TRY THEN UPDATE



Took a look at the code and I don't think this is going to work. I can't grab the myRitm info until the end since that is where I set it (Line-46 of my code).



The Request_ID is just an identifying field to help track orders.



The myRitm relates to the original table this way:



Request Status History >> Catalog Request Item >> Requested Item (change state here).



  1. POST to Request Status History to create entry
  2. Business Rule starts AFTER insert
  3. Grab the Request ID from the new entry
  4. Query Catalog Request Item for that entry using Request ID
    1. Attach the Catalog Request Item to the Request Status History's reference field.
  5. Grab the Catalog Request Itms's Requested Item Reference and and try and change the state here.
    1. Using the Catalog Request Item's item_information field.


So I can't go var getID = current.request_id.item_information.sys_id;       since the request_id field is only a field, not a reference.


sorry I had noticed that once I submitted the code and did a couple revisions probably while you were typing this. Please look at the code again


willkara
Kilo Contributor

Nate, that looks like it did it! Would you be able to tell me where I screwed up?



- Will