Best practice scripting for business rules

gmbroth
Kilo Contributor

Hi,

 

I have a before-insert business rule defined on table sc_request; I'm not sure if it's global or not. Currently, my Javascript is entered directly into the BR but I'd like to define it as a function in a class and reference it from the BR:

 

  1. I read the documentation about doing this for global BRs but, as I mentioned above, I'm not sure if my BR is global. This seems like a good practice for any BR or can it be done only for global BRs?
  2. If yes, how do I define my class and functions so they'll be accessible to the BR? Is it System Includes?
  3. How does the BR reference the function?

 

A small example or pointer to documentation would be great - thank you!

 

Garry

1 ACCEPTED SOLUTION

jmservicenow
Tera Guru

Garry,



You will want to utilize something called (which you mentioned) Script Includes. These are scripts that can be called within various other scripting areas of ServiceNow.



Within the script include, you'll want to do something such as GetCISupportGroup:



var GetCISupportGroup = Class.create();


GetCISupportGroup.prototype = {


        initialize: function(ci_sys_id) {


                  this.ci = ci_sys_id;


        },


        getSupportGroup() {


                  var gr = new GlideRecord('cmdb_ci');


                  gr.get(this.ci);


                  return gr.support_group;


        },


        type: 'GetCISupportGroup';


};



You can then create a Business Rule that populates this on Incident:


Condition: !current.assignment_group && current.cmdb_ci


Script:


current.assignment_group = GetCISupportGroup(current.cmdb_ci).getSupportGroup();



I hope this helps!


View solution in original post

4 REPLIES 4

jesusemelendezm
Mega Guru

Hi Garry,



A global business rule is any business rule where the selected Table is Global. Global business rules have no condition or table restrictions and load on every page in the system.



If you are selecting the table sc_request that means, It is not a global Business rule.



Regards,


Jesus


jmservicenow
Tera Guru

Garry,



You will want to utilize something called (which you mentioned) Script Includes. These are scripts that can be called within various other scripting areas of ServiceNow.



Within the script include, you'll want to do something such as GetCISupportGroup:



var GetCISupportGroup = Class.create();


GetCISupportGroup.prototype = {


        initialize: function(ci_sys_id) {


                  this.ci = ci_sys_id;


        },


        getSupportGroup() {


                  var gr = new GlideRecord('cmdb_ci');


                  gr.get(this.ci);


                  return gr.support_group;


        },


        type: 'GetCISupportGroup';


};



You can then create a Business Rule that populates this on Incident:


Condition: !current.assignment_group && current.cmdb_ci


Script:


current.assignment_group = GetCISupportGroup(current.cmdb_ci).getSupportGroup();



I hope this helps!


Thanks John, your example is most helpful.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Garry,



The purpose of a global business rule is to define a piece of service side scripting that you can call and reuse from other scripts. However, that functionality is in practice considered legacy with Script Includes being the better way to define reusable/callable code. The difference is that global business rules are loaded all the time and script includes are only loaded when you call them, reducing the performance impact.



Script Includes - ServiceNow Wiki