Making ServerSide objects available to ClientSide code (onChange and Custom Content scripts)

If you have started to code with Sage CRM you will know that the world is seemingly divided into two, ServerSide and ClientSide. The objects and properties that we can use ServerSide can not be used within the browser. We have useful methods like CRM.GetContextInfo() which we use in Create Scripts, Validation Rules, and TableLevel Scripts which are just not available to the browser executed onChange scripts or code that is placed in a screen's Custom Content box.

Imagine a situation where you are trying to add clientside behaviour within a screen such as the CaseSummary screen. You are in the content of the Case but you need information about the company. Perhaps you wish to have something happen when a SLA is changed for the case when the company type is 'Prospect'. In ServerSide code this is easy information to get, but in ClientSide code we are limited to what data we have in the browser. Using the DOM we can look at any information on screen whether in the main frame or in the TopContent but if the data is not there it can not be accessed.

But there is actually one example of a ServerSide object that is also available ClientSide and this is the CurrentUser object.

The CurrentUser object is carried through to the browser and can be used in Custom Content scripts and onChange scripts. An example of using the CurrentUser object in clientside code is shown in the article "Controlling Buttons using Client Side code":

So if a ServerSide object like CurrentUser can be made available within the browser, then we must be able to make our own objects available.

The technique that can be used to do this is basically the same trick we can use to pass client side Scripts in a field oncreate script as discussed in the article "Passing client side Scripts in a field oncreate script".

In my example I need to make Company information available in a Case screen. I am going to use a createscript to pass the object to the clientside using the field's Caption property.

For this, I can add the following create script to the field case_referenceid in the casedetailbox screen.


var strCompanyName= CRM.GetContextInfo("company", "comp_name");
var strCompanyType = CRM.GetContextInfo("company", "comp_type");

Caption = CRM.GetTrans("customtags","scriptopen");
Caption += "function customcompany(CompanyName,CompanyType)"
Caption += "{this.comp_name=CompanyName; this.comp_type=CompanyType;}"
Caption += "var CurrentCompany=new customcompany('"+strCompanyName+"','"+strCompanyType+"')";

Caption += CRM.GetTrans("customtags","scriptclose");
Caption += CRM.GetTrans("colname","case_reference")+":";


This code assumes that translations have been created for the script open and close tags.

My little example creates an object called CurrentCompany that has two properties (comp_name and comp_type) which can be referenced in clientside code. To test this we can add an onChange script to the field case_description.

window.alert(CurrentCompany.comp_type)
Anonymous