Monday, September 08, 2008

Welcome Tavi

Let's give a big welcome to our newest author - Mr. Octavian Cucuta on CRM Stuff blog. He promises a lot :P

Large Integer division remainder

Recently, I had to obtain the remainder of a (seriously) large integer division (32 digits long divided by another shorter integer; base 10) and found that JavaScript reliably supports operations only on 15-digit long integers. Thus, this what I came up with:

// Gets the remainder from the division operation for LARGE numbers
// Support: positive integers; first parameter must not be 0
function GetRemainder(x, y) {
if ((y != 0) && (x.slice(0, 1) != "-") && (y.slice(0, 1) != "-"))
{
var nr = x;

// Removing the potential first '+' character
if (nr.slice(0, 1) == "+") {nr = nr.slice(1);}
if (y.slice(0, 1) == "+") {y = y.slice(1);}

// Removing leading blank and tab characters on both numbers
while ( (nr.charCodeAt(0) == 32) || (nr.charCodeAt(0) == 9) ){
nr = nr.slice(1);
}
while ( (y.charCodeAt(0) == 32) || (y.charCodeAt(0) == 9) ){
y = y.slice(1);
}

var len = nr.length;

// Checking if any non-digit characters are embedded into the input string
var i = 0;
for (i = 0; i < len; i++)
if (!((nr.charCodeAt(i) >= 48) && (nr.charCodeAt(i) <= 57)) ) {
return -1;
}

var number = parseInt(y,10);
var remainder = 0;
var ignore = 0;
var k = '';

while (ignore < len) {
if (remainder == 0) {
var w1 = y+' ';
var w2 = w1.length-1;
k = nr.slice(ignore, ignore+w2);

ignore += w2;
remainder = parseInt(remainder+k, 10) % number;
} else {
k = nr.slice(ignore, ignore+1);
ignore += 1;
remainder = parseInt(remainder+k, 10) % number;
}
}

return remainder;
}

return -1;
}


Usage:

document.write(GetRemainder("12345678901234567890123456789012", "123"))

will output 27.

Friday, September 05, 2008

OnChange Event

When starting to work with CRM, one of the first things you learn is how to put some script on the onChange event. It's easy to do this and it really works.
Let's say you want to fire that script at the first moment your page is loaded.
In CRM 3.0 you could do that writing a single line :
crmForm.all.yourfield.FireOnChange();
When starting to work with CRM 4.0 I tried to use the same code but it didn't work anymore. I was sooooo disappointed and I hardly found out the solution. That's why i thought of sharing it with you.
The code for firing a script put on the onchange event for CRM 4.0 is:
yourfield_onchange0();

I hope this helps. Happy coding! :)

Random Custom Serial Number Generator

A brief example on how to generate random serial numbers with JavaScript. Enjoy!

// Symbols Array (A-Z, 0-9)
var Symbols = new Array();

// Gets the ascii code for a certain character
function GetAsciiBySymbol(letter)
{
var code = letter.charCodeAt(0);
return code;
}

// Populates a certain array with the A-Z, 0-9 symbols
function PopulateArray(array) {
var Position = 0;

// Letters
for(i=GetAsciiBySymbol('A'); i<=GetAsciiBySymbol('Z'); i++) {

array[Position] = String.fromCharCode(i);
Position++;
}

// Numbers
for(i=GetAsciiBySymbol('0'); i<=GetAsciiBySymbol('9'); i++) {
array[Position] = String.fromCharCode(i);
Position++;
}

}

// Gets the array symbol by a certain index
function GetSymbol(index) {
return Symbols[index];
}

// Generates a new Serial Number
function GenerateSerial(serialLength, separatorSymbol, separatorPositions) {
var serial = "";
var Offset = 0;

for(i=0; i
var rnd = Math.floor(Math.random()*Symbols.length);
serial += GetSymbol(rnd);
}

if(SeparatorPositionsOk(separatorPositions,serialLength)) {
for(i=0; i
var position = separatorPositions[i];
serial = serial.substring(0,position + Offset) + separatorSymbol + serial.substring(position + Offset, serial.length); Offset++;
}
}

return serial;
}

// Checks if a certain array contains only items less than a maximum value
function SeparatorPositionsOk(array, maxValue) {
for(i=0; i maxValue) {
return false;
}
}

return true;
}


// Populate Symbols
PopulateArray(Symbols);

// Example 1 - Format: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
var SepPos = new Array();
SepPos[0] = 5;
SepPos[1] = 10;
SepPos[2] = 15;
SepPos[3] = 20;
var NewSerial = GenerateSerial(25, "-",SepPos);
alert("Serial Number: " + NewSerial);


// Example 2 - Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
SepPos = new Array();
SepPos[0] = 8;
SepPos[1] = 12;
SepPos[2] = 16;
SepPos[3] = 20;
SepPos[4] = 24;
NewSerial = GenerateSerial(32, "-",SepPos);
alert("Serial Number: " + NewSerial);

// Example 3 - Format: XXX-XXXXXXX
SepPos = new Array();
SepPos[0] = 3;
NewSerial = GenerateSerial(10, "-",SepPos);
alert("Serial Number: " + NewSerial);

Replace Chars in String - prototype example

An elegant way to replace certain chars from a string with new ones:

// Custom method for replacing certain chars from a string with new ones
function ReplaceChars(oldValue,newValue) {
var newText = this.split(oldValue);
newText = newText.join(newValue);
return newText;

}

// Attach the custom method to string object
String.prototype.Replace = ReplaceChars;

// Usage
var myString = "Hello World!";
alert(myString.Replace("o","0")); // will output "Hell0 W0rld!"

An easier way to hide or show a certain field in CRM

Simply use the function below:

// Shows or hides a certain field
function HandleField(field, display) {
document.getElementById(field+"_c").style.display = display;
document.getElementById(field+"_d").style.display = display;
}

/* Calling */
HandleField("fieldid", "inline"); // show inline
HandleField("fieldid", "block"); // show block
HandleField("fieldid", "none"); // hide