There have been several articles written about using the scriptcommonfunction registry key to set a global function library for tablelevel scripts, but this option is not available for the other internal scripts.
So if we can't use the function library of the tablelevel scripts to create globally available functions for onCreate scripts, what are our options?
I believe that there are two mechanisms.
- The first is to use a Windows Script Component. The Windows scripting environment allows you to create reusable COM components with scripting languages such as JScript. Components could then be called from within the internal COM API
var oComponent = CreateObject("Component.MyComponent");
If you are going to go down that route then you may as well create a fully fledged compiled COM object to carry out the complex tasks.
- The second possibility is to store the scripts to be used in the onCreate script or Validate script actually in the CRM database.
This second idea rests on the fact that you can instantiate functions from strings via the new Function constructor in JScript.
functionName = new Function( [argname1, [... argnameN,]] body );
I created for myself a new entity called "myscripts". This table had 2 fields the key field (scrp_scriptid) and the field containing the script (scrp_text).
In my test example I created a record that had the simple script:
And I was able to reference this in a onCreate script very simply.
var myRecord = eWare.FindRecord("myscripts","scrp_scriptid=217");
var objScript = new Function("x",myRecord.scrp_text);
Valid = false;
ErrorStr = objScript("test");
This has basically the equivalent of creating the function like this:
Adding your own script entity may be too much work as you would have to also create the management pages for this area. You could always store the text of the function in another CRM field such as the custom_captions table. This approach has the advantage in that the management screens are already present.