Sunday, September 7, 2008

Microsoft Dynamics CRM Javascript Access to Form Elements

Here's how to access each control on your forms using Javascript. Using this information you can add very useful functionality to CRM's front end.

There are a couple of ways to do this, you can choose which ever suits your situation best...

Currently displayed entitie's GUID:
        crmForm.ObjectId

To access a control on the form:
    crmForm.all.[attributename]
    // OR
    document.getElementById("[attributename]")

Hide/show attribute(set to visible or invisible):
    //Hide it
    crmForm.all.[attributename].style.display = "none";
    //Show it
    crmForm.all.[attributename].style.display = "inline";

Enable/disable attribute:
   //Disable
   crmForm.all.[attributename].Disabled = true;
   //Enable
   crmForm.all.[attributename].Disabled = false;

Does control/attribute contain any data?
   if (crmForm.all.[attributename] != null)
       //Contains some data

Control's value(this is the current on screen value. ie, may not have been saved yet):
   crmForm.all.[attributename].DataValue
   // OR
   crmForm.all.[attributename].value
   // OR if its a checkbox
   crmForm.all.[attributename].Checked = [true false=""]
   // OR if its a lookup
     //Descriptive name for the selected item
     crmForm.all.[attributename].DataValue.name
     //GUID value of the selected item
     crmForm.all.[attributename].DataValue.id
     //Type of entity selected in the lookup(1=Account, 2=Contact...)
     crmForm.all.[attributename].DataValue.type
     //Descriptive name for the selected entity type
     crmForm.all.[attributename].DataValue.typename


1 comment:

Pranay said...

Thanks for a usefull post. I have a query here,
lets say i have added an "OnChange" event for a form field and basing on some logic in that jscript i would want to retain the old value of the field.
ex: lets say the value si 10 and i updated it to 20, and i want to populate it back to 10.

How can i do this?