- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:04 AM
I have a catalog item with a location reference field variable. Lets call it "var_loc". I want to populate the parent REQ's location field with that same value.
Simple right? well it doesn't seem to want to work for me. So before I go to HI and create a stupid ticket, I wanted to throw it on community to see if someone can spot what is wrong.
BR
Table: sc_req_item
When: after (also tried 'before')
Insert : true
Condition: Item = Order Equipment
script
(function executeRule(current, previous /*null when async*/) {
var loc = current.variables.var_loc.getValue();
var req = current.request;
var gr = new GlideRecord('sc_request');
if (gr.get(req)) {
gr.location = loc;
gr.update();
}
})(current, previous);
I've also tried
(function executeRule(current, previous /*null when async*/) {
var loc = current.variables.var_loc.getValue();
var req = current.request;
var gr = new GlideRecord('sc_request');
gr.addQuery('sys_id',req);
gr.query();
if (gr.next()) {
gr.location = loc;
gr.update();
}
})(current, previous);
both of the scripts above DO NOT work when run as business rules, but put them both into Background scripts, and they each work fine.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:40 AM
Hi Simon,
Is that the whole of the script that you are entering into the script field on the business rule? When I try to set up a business rule using your code, I get warnings that the function declaration is missing..
When I amend the code as follows, it passes the syntax checker...
Not sure which version of ServiceNow you're using, but this was on Fuji.
However; I can see two more pressing issues with your code...
1. Replace var loc = current.variables.var_loc.getValue();
with
var loc = current.variables.var_loc;
...the getValue(); is not required in a business rule.
2. Parent request doesn't exist at the point that the requested item is created. The bit that will fail is:
if (gr.next()) {
as whilst a sys_id has been aligned to the sc_request (through initialisation of the sc_request), the request record doesn't get inserted to the database until after the child requested items have been created.
I believe the order of execution is as follows:
1. Create requested item records with (as-yet unsaved) sys_id of sc_request in the sc_req_item.request
2. Create request record
So I'd advise that you change your BR to fire after the creation of the parent request and to then interrogate the child/children and make a decision as to which location to set into the parent request. I've done similar in prior implementations and it works well.
Good luck!
Thanks
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:16 AM
Hi Simon,
Try using current.variables.variable_name as below:
var loc = current.variables.var_loc;
gs.log(loc);
Also Check in the logs what value you are getting.
Thanks
Akhil
Hit Like/Helpful/Correct, if applicable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:48 AM
yes, my mistake. I had added the "getValue()" in a desperate attempt to get it to work, and forgot to remove it.
However it still doesn't work.
I am 100% certain the my script is getting the correct sys_ids for the 'req' and 'loc' variables. I've amended my code to spit out each into an info message.
(function executeRule(current, previous /*null when async*/) {
var loc = current.variables.salon_number;
var req = current.request;
var gr = new GlideRecord('sc_request');
gr.addQuery('sys_id',req);
gr.query();
if (gr.next()) {
gr.location = loc;
gr.update();
} else {
gs.addInfoMessage("test failed");
gs.addInfoMessage("loc = " + loc);
gs.addInfoMessage("req = " + req);
}
})(current, previous);
It still doesn't work, and it is something to do with the way I am getting the Glide object.
Since the REQ and RITM are generated as soon as I checkout of the catalog, that is when the failure message shows.
Do you think the issue is perhaps related to the order in which the records are created? perhaps the REQ is not created until after the BR runs. Is there a way to delay a BR with a timer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:40 AM
Hi Simon,
Is that the whole of the script that you are entering into the script field on the business rule? When I try to set up a business rule using your code, I get warnings that the function declaration is missing..
When I amend the code as follows, it passes the syntax checker...
Not sure which version of ServiceNow you're using, but this was on Fuji.
However; I can see two more pressing issues with your code...
1. Replace var loc = current.variables.var_loc.getValue();
with
var loc = current.variables.var_loc;
...the getValue(); is not required in a business rule.
2. Parent request doesn't exist at the point that the requested item is created. The bit that will fail is:
if (gr.next()) {
as whilst a sys_id has been aligned to the sc_request (through initialisation of the sc_request), the request record doesn't get inserted to the database until after the child requested items have been created.
I believe the order of execution is as follows:
1. Create requested item records with (as-yet unsaved) sys_id of sc_request in the sc_req_item.request
2. Create request record
So I'd advise that you change your BR to fire after the creation of the parent request and to then interrogate the child/children and make a decision as to which location to set into the parent request. I've done similar in prior implementations and it works well.
Good luck!
Thanks
Daniel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 06:50 AM
As I suspected. Thanks for the clarification on the ordering of the record creation. I'll try it from the REQ.