Chin's Musings - Variable Processor Best Practices

Welcome to another installment of Chin's Musings! In this series, Systems Controller Chin Chuah will create helpful rundowns of various system features and processes to help you better understand the SmartSimple platform. These articles are geared toward experienced SmartSimple system users.

For further information about the SmartSimple Variable Processor, please visit our Wiki page or click on any of the accompanying links in this article. 

The Variable Processor is used to dynamically replace various values throughout the system with context to the current user or the object being viewed. While the Variable Processor is a very powerful and useful tool, one must understand the basics behind how it functions to configure it properly.

In general, any time that you retrieve a variable for use in a calculation, evaluation, or assignment to another standard field, you need to always encapsulate it within double quotes.

In Template Formulas
Problem 1:
@level1.name@=@level1.owner.firstname@ @level1.owner.lastname@;
Reason:
When assigning a string value into a custom field, the string must be encapsulated within quotes. This example would cause an error on the server and nothing would be saved to @level1.name@.

Problem 2:
@level1.name@='@level1.owner.firstname@ @level1.owner.lastname@';
Reason:
Names with apostrophes (e.g. Chin O'Brien) will terminate the single quoted string. This will cause an error on the server and nothing would be saved to @level1.name@.
Solution:
@level1.name@="@level1.owner.firstname@ @level1.owner.lastname@";
Always use double quotes around strings to be safe.

In Browser Scripts
Problem 1:
frm.cf_@customfield.id@.value="";
Reason:
In some cases where the field value is null, the variable processor will not replace the variable with a blank and you will see frm.cf_@customfield.id@.value on the page. This will break this particular script and hence any subsequent script functions that follow afterwards on the page.
Solution:
document.getElementById("cf_@customfield.id@");

Problem 2:
var oppid=@opportunityid@;
Reason:
Again, when the variable doesn't exist or is blank, this script will break and all subsequent scripts as well.
Solution:
var oppid="@opportunityid@";

In Visibility Conditions
Problem 1:
@opportunityid@>0
Reason:
When you create a new Level 1 it will look like the condition's working as you don't see the field, but in reality, this will cause server errors. This occurs because the @opportunityid@ is not replaced, as that value does not yet exist for new Level 1s. An error will cause the visibility condition to always fail, which will lead to undesired behaviour.
Solution:
"@opportunityid@">0
Note: For this particular example, there is a check box setting within the custom field configuration that allows you to hide fields on new objects until the object is saved at least once.

Everywhere Else
Problem 1:
@ReportProperty(12345,recordcount)@ + @custom number@
Reason:
If either of the above values is blank or null, this will cause an error and will not work.
Solution:
"@ReportProperty(12345,recordcount)@"*1 + "@custom number@"*1
The *1 is used to explicitly cast the result into a numeric value. This is needed in the cases that your @custom number@ contains an alphabetic character, the  *1 will convert it to a 0. If for some reason you use a @ReportProperty()@ that returns blank, ""*1 will also convert to a 0.

Problem 2:
Not using quotes within @sslogic()@ or @sscalculation()@ conditions will also create errors, but again, you may be oblivious to them as the @sslogic()@ doesn't show up. This may appear to be correct for certain situations.

Note - The following situations should be considered:

1) The .inRole variable will always return the values of true or false so there is no need to encapsulate this within double quotes (i.e. @me.inRole(Organization Contact)@).
2) When the variable value you are retrieving contains double quotes within itself you will need to escape these, or encapsulate the variable within single quotes if you are sure the variable value does not contain single quotes within.

Contact us at info@smartsimple.com or visit our main website to learn more about SmartSimple.

Comments

Popular posts from this blog

Introducing 24/5 Technical Support

SmartSimple Attends the Conference Board of Canada’s Corporate Social Responsibility Conference

Dedicated vs. Non-Dedicated Servers – What’s best for me?