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

Hi,

those are the parameters to the self-invoked function

the current and previous objects will help to fetch the previous and current value from the record on which BR runs

Regards
Ankur

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

I found a question on a practice paper for the CAD exam.

 

Here is the Business Rule script template:

 

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

 

This type of JavaScript function is known as:

 

1. Constructor

2. Scoped

3. Anonymous

4. Self Invoking

 

I gave 4 as the answer but was advised that this was incorrect and that the answer is 2. Can anyone confirm either way please? @Ankur Bawiskar

 

 

 

From my understanding it's option 4.

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

I'd imagine that, if that's what you think then it must be true.

 

Thank you again Ankur.

JithinPadma
Tera Contributor

JithinPadma_0-1738823252651.png
Can anyone please help me to understand the issue, how can I resolve it .am I missing any plugins?