- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:00 PM
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);
Solved! Go to Solution.
- Labels:
-
Change Management
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:11 PM
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.
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:05 PM
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.
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:44 PM
Hi Ali,
Thanks for the explanation. Got it. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:11 PM
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.
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2021 10:14 PM
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?