diff --git a/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/README.md b/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/README.md new file mode 100644 index 0000000000..2621a9cf60 --- /dev/null +++ b/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/README.md @@ -0,0 +1,11 @@ +Using current.update() in Business rule is not recommended from servicenow due to following reasons- +1. Before- Records will be saved automatically after BR executes so using current.update is redundant +2. After- Triggers all "before" BRs again causing recursion calls and performance issue +3. Async- Same issue as in After +4. Query- current object doesn't exist and will cause error +5. Display- Runs before UI is rendered. Using current.update doesn't make sense. + +To avoid such scenarios, a business rule can be configured on the 'sys_script' table will trigger an error message and +block the creation of any new business rule if it detects the use of current.update() within the script. + +Runs: onBefore insert. diff --git a/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/script.js b/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/script.js new file mode 100644 index 0000000000..a62c1a970a --- /dev/null +++ b/Server-Side Components/Business Rules/Prevent Creation of Recursive BR/script.js @@ -0,0 +1,12 @@ +(function executeRule(current, previous /*null when async*/) { + // Check if the script contains 'current.update()' + if (current.script && current.script.toLowerCase().includes('current.update()')) { + var errMsg = "Error: 'current.update()' should not be used in Business Rules."; + + // Abort the insert/update operation + gs.addErrorMessage(errMsg); + current.setAbortAction(true); + } + + +})(current, previous);