Function library for tablescripts

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Function library for tablescripts

  • Comments 10
  • Likes
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.

In the JS file containing your functions you will need to add the ASP code tag before and after them, i.e., <% …your functions here…%>. 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:
<!-- #include file ="..\myfunc.js"-->
Response.Write("test: " + getit() );

  • Does this work with validation and create scripts as well? ie. if I set this registry value, add a bunch of functions to my script library, can I simply call them from createscripts and vallidatescripts?

  • This technique only works with Tablelevel scripts.  See the article "Creating Global Code for OnCreate and Validate scripts (Internal COM API)"

  • I don't understand the first point: 1. Add a registry entry: scriptcommonfunctions (as string)

    Where i must create the "registry entry" ?

  • I have the same question than Jonathan!

    It means that I will add an include file in the first line of tablescript?

    "<!-- #include file ="myfunc.js"-->"


  • I have updated the article with an image that will hopefully explain where you add the registry entry.

  • Thanks Jeff! I understand now!

  • Hi Jeff, Is it possible to have more than one script file here, comma seperated or something?

  • Nic

    Alas, no.  But an existing script file could be extended with your own functions.

  • Thanks for the aclaration.

  • In the ScriptCommonFunctions reg key I can point to an external file. I use this alot. However, in that file can I do an include which links to another file or pulls the contents from another location? What about storing the script as a translation and using GetTrans to pull the function out?