Function library for tablescripts

2 minute read time.
The code on this page was corrected 31st October 2009.
Tablescripts are great, they allow you to add code to Sage CRM which will always fire when a table event happens, for example, the company is updated. Look in the developer/admin guides for more details on them.

The problem is that you may find that you want to write a function that you could use in all your tablescript not just one. This can be done by doing the following:

1. Add a registry entry: scriptcommonfunctions (as string), enter it's value as, for example, myFunctions.js.

2. Create your myFunctions.js with all your favourite functions in, using the normally javascript syntax of:
function funx1 (arg1) { ****** code ********** }
function funx2 (arg1) { ****** code ********** }
3. Copy your myFunctions.js file to the CustomPages directory in your virtual directory.
4. Do an IISReset (you could try metadata refresh if you wanted to be gentle)

Now you can all your functions in your javascript for your tablescript events!! It's all about reuse :-)

Problem Description:
How do you include the same JS file used for tablescript functions in your Sage CRM ASP Pages? Conventional wisdom has been that this impossible as the ASP page would treat the functions as html so you couldn't call them, however I have found a solution to this most vexing of problems.


Solution:
In the JS file containing your functions you will need to add the ASP code tag before and after them, i.e., . Impossible you say, that will cause the tablescripts to throw an error as they will not understand the ASP code tag, and you would indeed be correct in your Sage CRM wisdom. So how do you do it? Well, here is the trick, you hide the tags in javascript comments so the tablescript ignores them. This still works for the ASP page because it does the opposite ignoring the javascript comment and finding the ASP code tag and therefore recognising the functions as functions!! Hooray! OK, here are the samples to put the cherry on top of the proverbial cake. Oh, I have added a sneaky bit to the ASP page to hide the javascript comment or it will display as HTML.


Sample from JS file (saved as myfunc.js) :
**************************************************************
//
function getit( )
{
return "by George it works";
}

// %>
**************************************************************


Sample from TableScript:
**************************************************************
function UpdateRecord()
{
// call function from JS File referenced by registry setting
Values("Oppo_Description")= getit( );
}
**************************************************************


Sample from ASP page:
**************************************************************

Response.Write("

Response.Write("-->");
Response.Write("test: " + getit() );
%>
**************************************************************