Many CRM development tasks involve manipulation and concatenation of strings, whether it’s concatenating a Field DavaValue from 2 or more fields, building messages that involve dynamic data, constructing FetchXml Requests and Soap messages or constructing strings that contain CSS or other meaningful html information, in one way or another you end up writing a script that looks like the following examples:
//Exampe 1
var someValue = “The account number: ” + crmForm.accountnumber.DataValue + “ is not valid\n”;
someValue+= “More helpful info on how to fill a valid account number here.”
alert(someValue);
or
//Example 2
var iLeft = 100;
var iTop = 100;
var cssText = “position:absolute;left:” + iLeft + “;top:” + iTop + “;”;
document.all[“”].style.cssText = cssText;
or
//Example 3
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;
var windowFeature = “toolbars=0;width=” + iWidth + “,height=” + iHeight + “,top=” + iTop + “,left=” + iLeft;
window.open( “” , “ ” , windowFeatures);
There are several disadvantages of using this simple concatenation technique:
1.The first which is the most obvious and discussed here is the lack of multi-lingual support for text messages. If for instance you need to translate the text that appears in the first example above, how can you accomplish the task and still keep the dynamic value concatenation in the correct context?
2. Another major disadvantage involves construction of large strings such as when you build soap message or a large FetchXml queries which can dramatically decrease IE responsiveness and cause performance issues.
3. Large string that are constructed in this manner sometimes make it impossible to read and debug
The following JavaScript objects facilitates these types of tasks and will help you avoid the problems mentioned above.
The first code snippet facilitates the concatenation of strings using a class called StringBuilder (similar to C# System.Text.StringBuilder mechanism)
/* JS String Builder */
StringBuilder = function()
{
var parts = [];
this.Append = function( text ){parts[ parts.length ] = text;return this;}
this.Reset = function(){parts = [];}
this.ToString = function(){return parts.join( "" );}
}
//Usage
function OnCrmPageLoad()
{
//Solving example 2
var iLeft = 100;
var iTop = 100;
var cssText = new StringBuilder();
cssText .Append(“position:absolute;left:”).Append(iLeft).Append( “;top:”).Append(iTop).Append( “;”);
document.all[“”].style.cssText = cssText.ToString();
//Solveing example 3
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;
var windowFeature = new StringBuilder();
windowFeature.Append(“toolbars=0,”);
windowFeature.Append(“width=”).Append(iWidth).Append( “,”);
windowFeature.Append(“height=”).Append(iHeight).Append( “,”);
windowFeature Append(“top=”).Append(iTop).Append(“,”);
windowFeature.Append(“left=”).Append(iLeft);
window.open( “” , “ ” , windowFeatures.ToString());
}
OnCrmPageLaod();
The second code snippet contains an extension function to the built-in JavaScript string object which helps you solve the problem mentioned in example 1 above and also makes it possible to write code similar to the String.Format c# method.
String.prototype.Format = function( args )
{
var result = this.replace( /\{(\d{1})\}/ig ,
function match()
{
return args[arguments[1]];
}
);
return result;
}
function OnCrmPageLoad()
{
//Solve Exampe 1
var someValue = new StringBuilder();
someValue.Append(“The account number: {0} is not valid\n”.Format([crmForm.accountnumber.DataValue]));
someValue.Append(“More helpful info on how to fill a valid account number here.”);
alert(someValue.ToString())
//Solve Example 2
var iTop = 50;
var iLeft = 50;
var iWidth = 800;
var iHeight = 600;
//Create string template with place holders
var windowFeature = “toolbars=0;top={0},left={1},width={2},height={3}”;
//Format the String
windowFeature = windowFeature.Format([iTop,iLeft,iWidht,iHeight]);
window.open( “” , “ ” , windowFeatures);
}
OnCrmPageLoad();
The last example extends the StringBuilder class and creates a StyleBulder that is used to concatenate CSS rules with ease
StyleBuilder = function()
{
var cssText = new StringBuilder();
this.Add = function( key , value ){cssText.Append( key ).Append( ":" ).Append( value ).Append( ";" );}
this.ToString = function(){return cssText.ToString();}
}
//Usage
function OnCrmPageLoad()
{
//Solve Example 2
var iLeft = 100;
var iTop = 100;
var cssText = new StyleBuilder();
cssText.Add(“position”,”absolute”);
cssText.Add(“left”, iLeft);
cssText.Add(“top”,iTop);
document.all[“”].style.cssText = cssText.ToString();
}
OnCrmPageLoad();
I hope you find this interesting and helpful.