Simple Business Rule not working

scwillson
Mega Guru

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.

1 ACCEPTED SOLUTION

Daniel A-C
Tera Expert

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..


missing_func_dec.png



When I amend the code as follows, it passes the syntax checker...


func_dec_present.png


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


View solution in original post

7 REPLIES 7

No problem at all. Assuming you are on Dublin or later, you should check out the JavaScript debugger as it's really good for troubleshooting these kind of issues with server-side JavaScript like advanced business rules.


Create a before business rule on sc_request table and put this script in there



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


  var gr = new GlideRecord('sc_req_item');


  gr.addQuery('request',current.getValue('sys_id'));


  gr.query();


  while(gr.next()) {


  if( gr.variables.salon_number){


  current.location = gr.variables.salon_number;


  }


  }


})(current, previous);



Abhinay Erra
Giga Sage

I recommend to write business rule on request table rather than requested item.