- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 11:20 AM
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:
- Post Request to #2 to insert data >>>
- Request Status History >>>
- Business Rule below gets ran on AFTER , Insert/Update.
- Catalog Request Order >>>
- 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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 11:37 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 11:37 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 11:51 AM
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).
- POST to Request Status History to create entry
- Business Rule starts AFTER insert
- Grab the Request ID from the new entry
- Query Catalog Request Item for that entry using Request ID
- Attach the Catalog Request Item to the Request Status History's reference field.
- Grab the Catalog Request Itms's Requested Item Reference and and try and change the state here.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 11:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 12:00 PM
Nate, that looks like it did it! Would you be able to tell me where I screwed up?
- Will