Self-Executing Anonymous Functions
So basically, instead of defining a Business Rule like:
myNewScript123();
function myNewScript123() {
current.field_name = some value;
}
you can now write it as:
(function(){
current.field_name = some value;
})();
It does not save you a lot of typing, but you do not have to worry about coming up with a new function name each time.
I had been reluctant to use them in my solutions to people's question before actually writing about it so as to not cause any confusion. I just wasn't sure, but am confident now in their use, and so will use them whenever appropriate from now on. For instance, my "Applications with Role" article really should have the following code in the "Query with" field:
(function(){
current.addQuery('roles', parent.name);
})();
It's the same code, just wrapped in a Self-Executing Anonymous Function for safety!
And with the new code editor in Berlin and above, you can add an Editor Macro that will easily allow you to add the S-EAF syntax to your code by typing "seaf" and then the tab key within a script field. Just import the attached XML into your instance (remove the "txt" extension).
Labels:
https://www.servicenow.com/community/in-other-news/self-executing-anonymous-functions/ba-p/2291899