Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron

This is something I've been wanting to share for a while now and figured it was finally time to do so. I won't explain too much about how they actually work, as Mark Dalgleish has a great article that does just that - http://markdalgleish.com/2011/03/self-executing-anonymous-functions. I will, however, explain why and where I use them within the ServiceNow platform.

By now, I think most ServiceNow developers know that any server-side code should be wrapped within a function in order to protect variables from being unexpectedly modified in some other bits of code - http://wiki.servicenow.com/index.php?title=Business_Rules#Business_Rule_Variables_are_Global. Of course when I learned about that, I started wrapping my code within functions in order to protect it as well. Problem is, I started wrapping all my code in functions called "runScript()" or something similar. But, I started thinking, am I really protecting myself, or am I opening myself up for more problems if the wrong "runScript()" function was called. Everything seemed to be running OK, but I could not be sure.

I had started to think I should name the functions based on the records' sys_id in it to ensure uniqueness. Maybe I was getting paranoid, but I just wanted to make sure I was not introducing a different problem by wrapping the code in functions with the same name. Then I ran across Mark's article one day and knew that I finally found the proper solution. From that day on, I started using Self-Executing Anonymous Functions wherever I could - in Business Rules, Workflow scripts, server UI Actions (as well as mixed Client/Server UI Actions), Access Control rules, Relationships, etc.... Basically anywhere a script runs on the server. You can see a couple of examples in some answers I've provided to other user's issues:


Business Rule - Preventing Duplicate Data being created by Update or Insert
Relationship - See related child incidents in each child incident

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).

2 Comments