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

imran rasheed
Tera Contributor

Can someone explain the purpose of below predefined function in business rules.

 

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

// Add your code here

})(current, previous);

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@imran rasheed 

It's one of the template of writing your code within BR

The Script field is pre-populated with that template.

The function syntax is known in JavaScript as a self-invoking function or an Immediately Invoked Function Expression (IIFE). The function is immediately invoked after it is defined. ServiceNow manages the function and when it is invoked; developers do not explicitly call Business Rule Scripts.

Business Rule Scripts

Sharing the common syntax

1) Method 1

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

// Add your code here

})(current, previous);

2) Method 2 - Define your function and use your code

restrictIncidents();

function restrictIncidents() {

// your code here

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ahmmed Ali
Mega Sage

Found this from another thread.

The code/script in a business rule script inside a function provide the function to limit the visibility, or scope, of objects. When code is not enclosed in a function, variables and other objects are available to all other server-side scripts. This availability can lead to unexpected consequences that are difficult to troubleshoot.

 

Refer this link for more details- Business Rules Best Practices

https://community.servicenow.com/community?id=community_question&sys_id=ad37f5501b76c4d4d01143f6fe4bcbe2

 

You can relate same here. ServiceNow now populates the function definition OOTB on Business rule and it will have arguments like Current and previous objects where are GlideRecord objects of original and modified record on which BR is running.

 

Hope this helps.

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Hi Ali,

Thanks for the explanation. Got it. 🙂

Ankur Bawiskar
Tera Patron
Tera Patron

@imran rasheed 

It's one of the template of writing your code within BR

The Script field is pre-populated with that template.

The function syntax is known in JavaScript as a self-invoking function or an Immediately Invoked Function Expression (IIFE). The function is immediately invoked after it is defined. ServiceNow manages the function and when it is invoked; developers do not explicitly call Business Rule Scripts.

Business Rule Scripts

Sharing the common syntax

1) Method 1

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

// Add your code here

})(current, previous);

2) Method 2 - Define your function and use your code

restrictIncidents();

function restrictIncidents() {

// your code here

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

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

Does it mean to pull the current and previous values of fields within the table for which they are using a special predefined function?