Thursday, November 10, 2011

"Start-Up - Script" for MS CRM 2011

This script can contain every aspect of scripting with only one trigger point. It also checks the FormType state for "Create", "update" and so on.. This javascript is used by many people already, but I've updated it with "onchange" aswell:

The only thing you'll have to do is: Add this as a webresource to the entities Form that needs javascript. and add the function "OnCrmPageLoad" to the "Form OnLoad" tirgger point!
/* Script Entry Point */
function OnCrmPageLoad() {
var FormTypes =
{
Undefined: 0,
Create: 1,
Update: 2,
ReadOnly: 3,
Disabled: 4,
QuickCreate: 5,
BulkEdit: 6
}
runAlways();
switch (crmForm.FormType) {
case FormTypes.Create: OnNewFormLoad(); break;
case FormTypes.Update: OnUpdateFormLoad(); break;
case FormTypes.ReadOnly: OnReadOnlyFormLoad(); break;
case FormTypes.Disabled: OnDisabledFormLoad(); break;
case FormTypes.QuickCreate: OnQuickCreateFormLoad(); break;
case FormTypes.BulkEdit: OnBulkEditFormLoad(); break;
case FormTypes.Undefined: alert("Error"); break;
}
}
/* Implement each Form Type you wish to address */
function runAlways() {
onchange(); //Loads all fields that should have onchange functions on them
}
function OnNewFormLoad() {}
function OnUpdateFormLoad() {}
function OnReadOnlyFormLoad() { }
function OnDisabledFormLoad() { }
function OnQuickCreateFormLoad() { }
function OnBulkEditFormLoad() { }
/* --------------- Below is all the Private Functions ---------------------*/
function onchange(){
Xrm.Page.data.entity.attributes.get("prefix_fieldname").addOnChange(examplefunction);
}
function examplefunction(){
alert("Hello World");
}
Hope you like this script! It's one of the "life saving" scripts for CRM 2011
(it's also avaiable for MS CRM 4.0)
- Happy Scripting!