Dynamics CRM 2011 JavaScript Development

Leave a comment

By Hector Villafuerte.

Hector Certifications

This article explain how to use JScript Libraries to customize Microsoft Dynamics CRM forms. Microsoft Dynamics CRM allows forms extensions using JScript to change the built-in behavior of the CRM forms.

The following is a table with the events available in the CRM forms and its usages:

Event Description Usages
OnLoad

The OnLoad event occurs after the form has loaded.  Perform calculations based on changing values.
 Alert a user to a situation.
 Disable fields that should not be updated.
OnSave

It occurs when a user presses the Save or Save and close buttons or other actions that cause the form to be saved, such as the Save method.  Validate data. The following code shows how to cancel OnSave events.

event.returnValue = false;
return false;
OnChange

The OnChange event is available on every field. After a field changes and loose the focus.  Perform calculations to change other fields based on changing values.
 Change the formatting of fields, such as telephone numbers.
 Implement dynamic picklists (drop-down lists).
TabStateChange

This event occurs when a tab is expanded or collapsed.  This event is important if you are using script to modify the src property of an IFRAME control. 
OnReadyState

Complete

This event provides a location to include script that will execute as soon as the contents of the IFRAME have completed loading. Refresh something in the form, when the IFRAME loading is completed.

 

 

Xrm.PageAPI

This API is provided to interact with CRM controlled Forms.

 


 

The Xrm.Page.context (client-side context) object provides methods that are used to retrieve information that is specific to an organization, a user, or parameters that are passed to a page.

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

The Xrm.Page.data.entity object provides methods that are used to retrieve information that is specific to the record displayed on the page. This includes 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.

Xrm.Page.data.entity

Get by attribute name

var titleAttrib= Xrm.Page.getAttribute(“car_year”);

 

Get first control for attribute

var titleControl= Xrm.Page.getControl(“car_year “);

 

Getting an attribute value

Var titleValue= Xrm.Page.getAttribute(“car_year”).getValue();

Setting an Attribute value

Xrm.Page.getAttribute(“car_year “).setValue(2010);

 

Checking if an Attribute has changed

var titleChanged= Xrm.Page.getAttribute(“car_year”).getIsDirty();

 

Force a field to be required

Xrm.Page.getAttribute(“car_year”).setRequiredLevel(“required”);

 

Save and close the record
Xrm.Page.data.entity.save(“saveandclose”);

Xrm.Page.ui

 

Checking if a control is Visible

var titleVisible= Xrm.Page.getControl(“car_year”).getVisible();

 

Hide a control

Xrm.Page.getControl(“car_year “).setVisible(false);

 

Disable a control

Xrm.Page.getControl(“car_year “).setDisabled(true);

 

Set focus on a control

Xrm.Page.getControl(“car_year “).setFocus();

 

Set the label for a control

Xrm.Page.getControl(“car_year “).setLabel(“Car Year “);

 

Getting options for an Option Set control

var options = optionsetControl.getAttribute().getOptions();

 

Clear all options currently configured for the display control

optionsetControl.clearOptions();

 

Add only specific options back to the control list

optionsetControl.addOption(option);

 

Get FormType ( Create, Update, Read-Only, Disabled, Bulk Edit, Undefined)

Xrm.Page.ui.getFormType()

Xrm.Page.context

getClientUrl–provides a client side URL for accessing the CRM server from the client

getUserId–quickly get the GUID for the current user

getUserRoles–GUID list for each role the user is part of

getQueryStringParameters–array of key/value representing query string

getUserLcid–get language Id for the current user.

 

 

Jscript Libraries

Microsoft JScript libraries are Script (JScript) web resources that contain Jscript functions which can be used to handle form and field events or to perform actions for controls configured in the ribbon.

Once you create the Jscript, you should upload the Jscript resource:


 

After that you should associate it to the CRM form. Then you associate functions with Form and Field Events as shown below.

 


 


 

jQuery

Using jQuery to modify Microsoft Dynamics CRM 2011 application pages or forms is not supported. Do not use jQuery to interact with Microsoft Dynamics CRM 2011 forms. Use the Xrm.Page object model methods to access form elements.

The only supported use of jQuery in the Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online web application is to use the jQuery.ajax method to retrieve data from the REST endpoint.

You may also use jQuery within your own HTML web resource pages.

 


Leave a Reply