Home > CRM 2011, JScript > Useful CRM 2011 JScript Syntaxes

Useful CRM 2011 JScript Syntaxes

XRM.Page Model diagram

Xrm.Page Model

Xrm.Page Model

The Xrm.Page object serves as a namespace object to consolidate three properties on the form:

Xrm.Page.context

Xrm.Page.context provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string.

Xrm.Page.data.entity

Xrm.Page.data provides an entity object that provides collections and methods to manage data within the entity form.

Xrm.Page.ui

Xrm.Page.ui provides collections and methods to manage the user interface of the form.

Syntaxes

— “CRM Enity” Properties

  •  Xrm.Page.data.entity.getEntityName()   //Entity Name
  •  Xrm.Page.data.entity.getId()                     //Record GUID
  •  Xrm.Page.ui.getFormType()                      //CRM Form Type (Integer)
    **Create (1), Update (2), Read Only (3), Disabled (4), Bulk Edit (6)

Get & Set Text Field

  • Xrm.Page.data.entity.attributes.get(“fld_name”).getValue()
  • Xrm.Page.data.entity.attributes.get(“fld_name”).setValue(“First”)

Get Lookup

  •      Xrm.Page.data.entity.attributes.get(“primarycontactid”)
  •     Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].id
  •     Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].name

Set Lookup

  •  var lookUpValue = new Array();
    lookUpValue[0] = new Object();
    lookUpValue[0].id = idValue;
    lookUpValue[0].name = textValue;
    lookUpValue[0].entityType = ‘{Entity_Name}’;
  • Xrm.Page.getAttribute(“primarycontactid”).setValue(lookUpValue);

— Clear the Lookup

  • To clear the lookup field value, set with ‘null’
  • Xrm.Page.getAttribute(lookupAttribute).setValue(null);

Option Set Properties

  •  Xrm.Page.data.entity.attributes.get(“new_country”)
  • Xrm.Page.data.entity.attributes.get(“new_country”).getOptions()   // All Options
  •  Xrm.Page.data.entity.attributes.get(“new_country”).getText()         // Option Label value
  •  Xrm.Page.data.entity.attributes.get(“new_country”).getValue()       // Selected option value

Hide/Show controls

  • Xrm.Page.ui.controls.get(“name”).setVisible(true/false)

Disable/Enable controls

  •  Xrm.Page.ui.controls.get(“name”).setDisabled(true/false)

Set Focus

  • Xrm.Page.ui.controls.get(“name”).setFocus();

Set field’s Requirement Level

  • Xrm.Page.getAttribute(“name”).setRequiredLevel(“none”);
  • Xrm.Page.getAttribute(“name”).setRequiredLevel(“required”);
  •  Xrm.Page.getAttribute(“name”).setRequiredLevel(“recommended”);

Set the label for the control

  •  Xrm.Page.ui.controls.get(“name”).setLabel(“Label Text”);

Set URL for IFrams/WebResource

  •  Xrm.Page.ui.controls.get(“IFrame/WebResource Name”).setSrc(“URL”);

Get Parent of current control

  •  Xrm.Page.ui.controls.get(“name”).getParent();

“Current User” & “Organization” details

  • Xrm.Page.context.getUserId()                     //Current User ID
  •  Xrm.Page.context.getOrgUniqueName() //Organization Name
  •  Xrm.Page.context.getServerUrl()              //Server URL
  •  Xrm.Page.context.getUserRoles()             //Role ID’s of current user

Check isDirty

  • Xrm.Page.data.entity.getIsDirty()  // Form is IsDirty
  • Xrm.Page.data.entity.attributes.get(“new_name”).getIsDirty()  //Field is IsDirty

Save Form

  • function save(){Xrm.Page.data.entity.save();}

Save&close Form

  • function saveandclose(){Xrm.Page.data.entity.save(“saveandclose”);}

Close Form

  • function close(){Xrm.Page.ui.close();}

Expand/Collapse Tabs

  • var myTab = Xrm.Page.ui.tabs.get(“{tab_name}”);
  • myTab.setDisplayState(“expanded”); // To expand tab
  • myTab.setDisplayState(“collapsed”); // To collapse tab

Hide/Show Tabs & Sections

  • var myTab = Xrm.Page.ui.tabs.get(“{tab_name}”);
  • myTab.setVisible(True/False);
  • var mySec = myTab.sections.get(“{sec_name}”);
  • mySec.setVisible(True/False);

— Get current form properties if you have multiple forms

  • var item = Xrm.Page.ui.formSelector.getCurrentItem();if (item != null) {var itemId = item.getId(); // Form GUID

    var itemLabel = item.getLabel(); // Form Name

    }

  • Note – If your entity has only one form defined, “getCurrentItem()” will return null so the “item” become “null”

 —Look up window customizations (Unsupported) 

We can perform below customization actions on the lookup dialog window using JScript

 – Disable the “View Selection” dropdown

 – Hide the “New” button

 – Hide the “Search” textbox (i.e.,Quick search)

//To hide “New” button

crmForm.all.attr_name.AddParam(“ShowNewButton”, 0);

// To hide “New” button – Post UR 13

var lookupControl = Sys.Application.findComponent(“attr_name”);

if (lookupControl != null){

lookupControl._element._behaviors[0].AddParam(“ShowNewButton”, 0);

}

//To disable “View Selection” dropdown

document.getElementById(“{attr_name}”).setAttribute(“disableViewPicker”, “1”);

//To hide “Search” textbox

document.getElementById(“{attr_name}”).setAttribute(“disableQuickFind”, “1”);

After applying the script (i.e., After placing the above lines in form onload event), the lookup window looks as below

Customized lookup dialog

Customized lookup dialog

Quick Reference :-

Great post on the difference B/W CRM 4.0 syntaxes Vs CRM 2011 syntaxes

Advertisement
  1. March 12, 2012 at 5:54 PM

    Great list of JS syntaxes! Exactly what I was looking for.

    Thanks

  2. May 25, 2012 at 1:59 AM

    I’m looking to hide the New button on a particular lookup dialog, but I am not sure where the event hooks are, can you point me in the right direction?

    • June 1, 2012 at 5:57 PM

      on onload() event of form’s jscript…Place below statement

      //To hide “New” button
      crmForm.all.{lookup_fldname}.AddParam(“ShowNewButton”, 0);

  3. June 7, 2012 at 8:00 PM

    I’ll try it out, thanks a bunch!

    • d g
      July 11, 2012 at 5:58 PM

      how to hide “properties” button on the lookup window. This script working for only “new” button. how to hide or disable properties button

  4. d g
    July 11, 2012 at 5:58 PM

    how to hide “properties” button on the lookup window. This script working for only “new” button. how to hide or disable properties button

  5. Konrad Viltersten
    January 5, 2013 at 11:32 PM

    This was both helpful and very convenient. Nice!

  6. October 24, 2013 at 2:38 PM

    Hi,

    I have written the JavaScript code for crm 2011, but after crm upgradation to 2013,

    that code not working in crm 2013. After up-gradation to crm 2013 the lookup is missing its multi value functionality.

    so i need to change below code:

    document.getElementById(“new_product”).setAttribute(“lookupstyle”, “multi”);

    Thanks in advance

  7. Naga Raju Padala
    November 6, 2013 at 1:42 PM

    Hi Rajeev,

    we are using crm 2013. we have written some scripts for customization. in these scripts
    we have used selectNodes(), selectSingleNode() functions. what are the replacements for selectNodes(), selectSingleNode() functions in XrmServiceToolKit. and how to use them in crm 2013.

    if you have any information regarding this, please help me.

    thanks in advance,
    Nagaraju

  8. Naga Raju Padala
    November 6, 2013 at 1:43 PM

    Naga Raju Padala :
    Hi Rajeev,
    we are using crm 2013. we have written some scripts for customization. in these scripts
    we have used selectNodes(), selectSingleNode() functions. what are the replacements for selectNodes(), selectSingleNode() functions in CRM 2013. and how to use them in crm 2013.
    if you have any information regarding this, please help me.
    thanks in advance,
    Nagaraju

    • November 10, 2013 at 7:11 PM

      Hi,
      Did u try the XRMServiceToolKit Beta for CRM 2013?

  9. Praveen
    July 15, 2014 at 11:03 AM

    Nice article bro.
    Thanks in Advance

  1. September 19, 2011 at 9:01 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: