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

Shishir Srivast
Mega Sage

If you are running your Business Rule on sc_task table and I think you should be able to access the field simple by current.<field_name>;


I can see that you are using current for every table, i don't think that's possible, you need to GlideRecord the table and then assign the value to that particular table's field..



example:


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


    var sc_req_item = new GlideRecord('sc_req_item');


    sc_req_item.get(parent_id);


    var gr = new GlideRecord('sc_task');


    gr.addQuery('parent', sc_req_item.sys_id);


    gr.addQuery('state', '1').addOrCondition('state', '-5');


    gr.query();


    if (gr.next()) {


          if (sc_req_item.stage.changes())


                sc_req_item.update();


    } else {  


          sc_req_item.stage = 'complete';


          sc_req_item.update();


    }


}


Shishir Srivast
Mega Sage

You need to create different Business Rules for different tables to use the current object of the table to pass value from one filed to another of same table.


amlanpal
Kilo Sage

Hi Andrew,



Just try to fetch the variable value from the Business rule as object.variables.vaariable_name. In your case, it would be current.variables.requested_for.



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


reg_1979
Tera Guru

Business rules will run on the base table even if you insert a record into the derived table. Shishir is right though, you should probably attach a business rule to each of the derived tables. Here's your fixed business rule.



(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.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(JSUtil.notNill(current.variables.requested_for))


  {


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


  }


  else


  {


  current.u_requester =current.request_item.opened_by ;


  }


  }


})(current, previous);