Basic JavaScript functions for CRM 4 and CRM 2011 And How to attach JavaScript event in CRM control

We have to keep in mind when converting or migrating from CRM 4.0 to CRM 2011 that there are some differences in CRM JavaScript functions which are very important to take care. Some of them are describe below.

  1. Tabs
  2. Global Variables and Methods
  3. Form Properties and Methods

Let‘s talk all this things in detail.

  1. Tabs:

When we use Tab controls into java script, the following conversion technique is very useful. This identifies the tab index in Tab control.

microsoft dynamics
microsoft dynamics
CRM 4.0 CRM 2011
crmForm.all.tab0Tab
Xrm.Page.ui.tabs.get(0)
crmForm.all.tab1Tab
Xrm.Page.ui.tabs.get(1)
crmForm.all.tab2Tab
Xrm.Page.ui.tabs.get(2)
crmForm.all.tab3Tab
Xrm.Page.ui.tabs.get(3)
crmForm.all.tab4Tab
Xrm.Page.ui.tabs.get(4)
crmForm.all.tab5Tab
Xrm.Page.ui.tabs.get(5)
crmForm.all.tab6Tab
Xrm.Page.ui.tabs.get(6)
crmForm.all.tab7Tab
Xrm.Page.ui.tabs.get(7)
  1. Global Variables and Methods.

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

CRM 4.0 CRM 2011
ORG_LANGUAGE_CODE
Xrm.Page.context.getOrgLcid()
(Returns the LCID value that represents the Microsoft Dynamics
CRM Language Pack that is the base language for the organization.)
ORG_UNIQUE_NAME
Xrm.Page.context.getOrgUniqueName()
(Returns the unique text value of the organizations name.)
SERVER_URL
Xrm.Page.context.getServerUrl()
(Deprecated. Use getClientUrl instead. Returns the base server URL.
 When a user is working offline with Microsoft Dynamics CRM for 
Microsoft Office Outlook, 
the URL is to the local Microsoft Dynamics CRM Web services.)
USER_LANGUAGE_CODE
Xrm.Page.context.getUserLcid()
(Returns the LCID value that represents the Microsoft Dynamics CRM 
Language Pack that is the user selected as their preferred language.)
GenerateAuthenticationHeader()
Xrm.Page.context.getAuthenticationHeader()
(Deprecated. Returns the encoded SOAP header necessary to use 
Microsoft Dynamics CRM web service calls using JScript.)
  1. Form Properties and Methods.
Roadmap-and-Release-Cadence_big
Roadmap-and-Release-Cadence_big

Xrm.Page.data.entity provides methods to retrieve information specific to the record displayed on the page, the save method, and a collection of all the attributes included in the form. Attribute data is limited to attributes represented by fields on the form.

CRM 4.0 CRM 2011
crmForm.ObjectId
Xrm.Page.data.entity.getId()
(Returns a string representing the GUID id value for the record.)
crmForm.ObjectTypeName
Xrm.Page.data.entity.getEntityName()
(Returns a string representing the logical name of the entity for the record.)
crmForm.Save()
Xrm.Page.data.entity.save()
(Saves the record. This method has three possible parameters.)
crmForm.SaveAndClose()
Xrm.Page.data.entity.save('SaveAndClose')
crmFormSubmit.crmFormSubmitId.value
Xrm.Page.data.entity.getId()
(Returns a string representing the GUID id value for the record.)

Microsoft Dyanamic CRM 2011 Provides following events:

  • Form Events
    • OnLoad –  This is for code to execute once the form has finished loading.  When you customize CRM 2011 forms with JavaScript, this is your bread & butter.  Whether it’s showing/hiding fields, toggling fields to read-only, or alerting the user that an account is on credit hold; the OnLoad event is what you want the form to do immediately after the user opens the form.
    • OnSave – Frequently used to validate data on the form, this event allows you to ensure the user entered the appropriate information. (Note: if it’s mission critical, then use a plug-in.)
  • Field Events
    • OnChange– This fires after the user leaves a field they just changed.  Typically this is used to format or validate a field.

Note: It is important to know the difference between OnChange and the unsupported OnBlur.

  • iFrame Events
    • OnReadyStateChange– This is similar to the OnLoad event for the Form, but fires when the iFrame has finished loading. A nice example of this is when you want to execute something inside of the iFrame based on a field selection in you form.
  • In addition to the OnReadyStateChange, don’t forget that you can pass parameters to the iFrame. The page inside the iFrame, will need to read the URL parameters and process them appropriately, but this is somewhat event related since you can process the URL server side or client side once the page is loaded (from within the iFrame).
  • Tab Events
    • TabStateChanged – This event fires when the tab is Expanded or Collapsed. Most of the time, you won’t need this event; however, it comes in handy when refreshing iFrames.

But, there are some limitations in CRM 2011 b’coz CRM 2011 does not supports some events like onclick and ondblclick

How to Attach Events to Controls in CRM 2011:

As we know that CRM does not allow to have onclick OR ondblclick kind of events on any control directly. But if we need to have it in our application then we can do it like below.

function onload_method() {

    var control = document.getElementById(“fieldName”);

    if (control) {

        control.attachEvent(“onclick”, control_clicked);

        control.attachEvent(“ondbllick”, control_dblclicked);

    }

}

function control_clicked() {

    alert(“This function fires on field click”);

}

function control_dblclicked() {

   alert(“This function fires on field double click”);

}

About the Author: The vast domain of Microsoft Dynamics CRM Development India has fascinated John Walton  to come up with this content which reflects the preciseness in which Java scripting is implemented by the CRM developers.