Posts Tagged ‘JScript syntaxes’

New JScript syntaxes – CRM 2013

With the advent of CRM 2013, below are the new set of client API methods

CRM 2013 Client API

CRM 2013 Client API


  • Xrm.Page.context.getUserName() Returns username of the currently logon user
  • Xrm.Page.context.getClient() Returns the client (i.e., Returns “Outlook”, “Web”, or “Mobile”)
  • Xrm.Page.context.client.getClientState()Returns  whether user is working online or offline.

Note – Xrm.Page.context.isOutlookOnline and isOutlookClient methods are deprecated


  • refresh data on form without reloading the page
  •, errorCallback) Allows you to save the record asynchronously with the option to set callback functions to be executed after the save operation is completed.
  • Validates the form and returns a Boolean.
  • – Allows to set IsDirty options on the form to (true/false).


  • Returns a string value of the primary attribute for the entity.


  • Xrm.Utility.openWebResourceDialog(webResourceName,webResourceData,width,height) Displays html web resource as dialogs window.
  • Xrm.Utility.alertDialog(message,onCloseCallback) Displays alert box with a callback function.
  • Xrm.Utility.confirmDialog(message,yesCloseCallback,noCloseCallback) Displays confirm dialog with different callbacks depending on Yes/No button clicks.

 Set Notifications

  • Check this link for syntaxes

Number Fields

  •      Xrm.Page.ui.controls.get(controlName).setPrecision(2) – Overrides field’s precision

Date Fields

  •    Xrm.Page.ui.controls.get(controlName).setShowTime(true) Controls whether to show the time for a date field.
  •    Xrm.Page.ui.controls.get(controlName).setIsAllDay()Allows to set the date control to all day date time.

Look up Fields

  •    Xrm.Page.ui.controls.get(controlName).addCustomFilter(filter,entityLogicaName)
    •    Applies a custom filter to the lookup view
    •    entityType is optional and if it is not passed it will default to all entity views
  •    Xrm.Page.ui.controls.get(controlName).addPreSearch(handler)Allows to add additional logic that will occur just before the search dialog box
  •   Xrm.Page.ui.controls.get(controlName).removePreSearch(handler)Removes event handler functions that previously been set in addPreSearch event.


Categories: CRM 2013 Tags: ,

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 provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string. provides an entity object that provides collections and methods to manage data within the entity form.


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


— “CRM Enity” Properties

  •   //Entity Name
  •                     //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


Get Lookup


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

  •“new_country”).getOptions()   // All Options
  •“new_country”).getText()         // Option Label value
  •“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

  •  // Form is IsDirty
  •“new_name”).getIsDirty()  //Field is IsDirty

Save Form

  • function save(){;}

Save&close Form

  • function saveandclose(){“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