Tuesday, September 8, 2009

Formatting CRM DateTime Field


As you might have noticed CRM DateTime field DataValue returns the following value: “Tue Sep 29 13:00:00 PDT 2009”
Sometimes you need to change the value to another format e.g. dd/mm/yyyy or mm-dd-yyyy hh:MM.
This is quite easy to do with C# since the format string is pretty extensive however the JavaScript Date object is pretty slim and so the only option is to write your own framework / prototype extension.

The following script extends the JavaScript Date Object and adds a toFormattedString function which accepts common format strings such as
dd – days, mm – months, yyyy – full year, hh – hours , MM – minutes , ss – seconds , ms – milliseconds , APM – AM/PM
You can extend the prototype function further if you require additional formatting.

Here is the code and usage example:

Date.prototype.toFormattedString = function(format)
{
var d = this;
var f = "";
f = f + format.replace( /dd|mm|yyyy|MM|hh|ss|ms|APM|\s|\/|\-|,|\./ig ,
function match()
{
switch(arguments[0])
{
case "dd":
var dd = d.getDate();
return (dd < 10)? "0" + dd : dd;
case "mm":
var mm = d.getMonth() + 1;
return (mm < 10)? "0" + mm : mm;
case "yyyy": return d.getFullYear();
case "hh":
var hh = d.getHours();
return (hh < 10)? "0" + hh : hh;
case "MM":
var MM = d.getMinutes();
return (MM < 10)? "0" + MM : MM;
case "ss":
var ss = d.getSeconds();
return (ss < 10)? "0" + ss : ss;
case "ms": return d.getMilliseconds();
case "APM":
var apm = d.getHours();
return (apm < 12)? "AM" : "PM";
default: return arguments[0];
}
});

return f;
}

function OnCrmPageLoad()
{
var d = new Date(crmForm.all..DataValue);
alert(d.toFormattedString("mm-dd-yyyy hh:MM:ss ms"));
alert(d.toFormattedString("dd/mm/yyyy hh:MM APM"));
}

OnCrmPageLoad();

CRM 4.0 Creating a Readonly picklist

CRM Picklist control (HTML select tag) does not have a read-only attribute like other input html controls. The only way to make it read-only is to disable it which grays out the control completely. Once you disable the Picklist you can’t change its border or font color (i.e. make it look like a read-only field). The only way to achieve the functionality is to change the control behavior so each time the user tries to change the Picklist value the Picklist initial value is re-selected.

Here is the JS code that does the job:


function OnCrmPageLoad()
{
ReadOnlyPicklist(crmForm.all.);
}

function ReadOnlyPicklist(picklist)
{
picklist.savedIndex = picklist.selectedIndex;
picklist.onchange = function()

{
this.selectedIndex = this.savedIndex;
}
}

OnCrmPageLoad();

CRM 4.0 Changing Tab Order

A while back i posted a solution for changing vertical tabbing to horizontal tabbing. The code ran a bit slow with lookups and so the code below is a revision of the previous posting with the fix.


function OnCrmPageLoad()
{
ReArangeTabIndex();
}

function ReArangeTabIndex()
{
for( var i = 0 ; i < crmForm.all.length ; i++ )
{
var element = crmForm.all[ i ];
if (element.tabIndex)
{
if (element.className == "ms-crm-Hidden-NoBehavior" || element.tagName == "A")
{
continue;
}

element.tabIndex = 1000 + (i*10);
}
}
}

OnCrmPageLoad();