Every once in a while, I look into the top search terms people have entered into Google or Bing (lol, who uses Bing?) that take them to this site. I do this to see if there are any common questions that people come here looking for answers to, but which I don’t yet have an article to answer. This month, that search term is “Can Script Includes use current”. This article aims to provide an answer to that question.
The question
I interpret the question “Can Script Includes use current” to mean:
If I write a function inside a Script Include (SI), and then call it from a Business Rule, can that function (in the SI) access the current object (from the Business Rule)?
Here’s an example SI which is attempting to do exactly that:
var CanIUseCurrent = Class.create();
CanIUseCurrent.prototype = {
initialize: function() {},
tryUsingCurrent: function() {
gs.info('The current record\'s display value is: .', current.getDisplayValue());
//^ This is bad!
},
type: 'CanIUseCurrent'
};
As you can see, the tryUsingCurrent() method is accessing a variable called current which is not declared or defined anywhere within the Script Include itself. Below, we’ll discuss whether this is possible, and whether it’s a good idea.
The short answer
The short answer to this question is: Yes, technically you can access anything that lives in the “calling” scope - but you shouldn’t. At least not without some extra steps to achieve “function purity”, which I’ll describe more in the long answer below.
If you take one thing from this article, let it be this:
Whenever possible, do not rely on “context” or the external/parent scope from which you expect your function to be called.
Never assume anything that you don’t have to, about any scope other than the function-scope you’re currently writing in.
Click below to read more, and see the full answer, including several important technical caveats!
Read more