Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to access catalog variables from Business Rule Script

agarc657
Giga Contributor

Hello Everyone,

I have a Business Rule Script that populars a field called Requester upon a task being created

The Business rule is run on the task table.

The issues i'm having is that there is a variable on the Catalog item that the user populates called "requested for" and I need to assign its value

to my Requester field. However on the task level how do I access or dot walk to the catalog variables .

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

                  // check to see if task is an incident

                    if (current.sys_class_name == 'incident')

                                  {

                                      current.u_requester = current.caller_id;

                                    }

                    if (current.sys_class_name == 'sc_req_item')

                                    {

                              if(JSUtil.notNill(current.variable_pool.requested_for)){

                                      current.u_requester = current.variable_pool.requested_for;

                                }

                              else{

                                    current.u_requester =current.opened_by ;

                              }

                              }

                    if (current.sys_class_name == 'sc_task')

                          {

                            if(JSUtil.notNill(current.variable_pool.requested_for)){

                              current.u_requester =current.request_item.variable_pool.requested_for;

                      }

                          else{

                            current.u_requester =current.request_item.opened_by ;

                      }

                      }

                          })(current, previous);

the bolded lines are my the ones im having problem with. Is there a way to access these variables from a business rule ? if not can does anyone have an alternative option for

populating my the requester fields based on the script above.

Edit to be clear both current.variables and current.variable_pool do not work

Thanks everyone

10 REPLIES 10

I have tried current.variables but this does not work


The only other thing I can think of is to remove the JSUtil functions and just test variable existence.



See below. Hope it works.



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


  // check to see if task is an incident


  if (current.sys_class_name == 'incident')


  {


  current.u_requester = current.caller_id;


  }



  if (current.sys_class_name == 'sc_req_item')


  {


  if(current.variables.requested_for)


  {


  current.u_requester = current.variables.requested_for;


  }


  else


  {


      current.u_requester = current.opened_by ;


  }


  }


  if (current.sys_class_name == 'sc_task')


  {


  if(current.variables.requested_for)


  {


  current.u_requester = current.request_item.variables.requested_for;


  }


  else


  {


  current.u_requester =current.request_item.opened_by ;


  }


  }


})(current, previous);


Hello,



Please create a separate Business Rules on base table like below and try, also i was thinking if you can have onSubmit client script to populate requester info.



Business Rule on Incident Table


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


// check to see if task is an incident


  current.u_requester = current.caller_id;


  current.update();


})(current, previous);



Business Rule on sc_req_item Table


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


if(current.variable_pool.requested_for.getDisplayValue() != "")){


  current.u_requester = current.variable_pool.requested_for;}


else{


  current.u_requester = current.opened_by;}


  current.update();


})(current, previous);



Business Rule on sc_task Table


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


if(current.variable_pool.requested_for.getDisplayValue() != ""){


  current.u_requester =current.variable_pool.requested_for;}


else{


  current.u_requester =current.opened_by;}


  current.update();


})(current, previous);



little modification in script as i referenced the OOB BR on sc_req_item table, please check if this helps:



find_real_file.png


Hi Andrew,



Well, I assume the script is not fetching the object with current. Either you can do Glide Query to the particular table defining an object, i.e., var ritm = new GlideRecord('sc_req_item'), OR what you can do is to go on and create separate before Insert Business rule on the corresponding tables. I'm just giving code/script for the Requested Item (sc_req_item) table. You may refer this and leverage the script for other tables also.



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


  if(JSUtil.notNill(current.variables.requested_for)){


  current.u_requester = current.variables.requested_for;


  }


  else{


  current.u_requester =current.opened_by ;


  }


})(current, previous);



I hope this helps.Please mark correct/helpful based on impact


venkatvraman17
Kilo Guru

Hi Andrew,



If you are creating the Catalog task from the workflow, you can consider populating the value of the requester field on the task table from the workflow itself. As the workflow is attached to the Requested item object, current object will contain access to the variable and you should be able to pass the information to the task you are creating as well.



Thanks,


Venkatraman.