web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

xRM Form Scripting

nghieppham Profile Picture nghieppham 4,755

We often forgot some basic things but it almost important for the big things we want to you, this article just want to share the basic things that we should remember when we working with MS CRM client script. It has been update the latest of CRM 2013...

Form Scripting Cheat Sheet

Accessing Attributes

Task Example
Access an attribute by name var nameAttribute = Xrm.Page.getAttribute("name"); Assigns the attribute for the Account Name field to thenameAttribute variable
Access all attributes var allAttributes = Xrm.Page.getAttribute(); Assigns an array of all the attributes in the Xrm.Page.data.entity.attributes collection to the allAttributes variable.

Using Attributes

Task Example
Get the value of an attribute var nameValue = Xrm.Page.getAttribute("name").getValue(); Assigns the value of the Account Name field to the nameValue variable.
Set the value of an attribute Xrm.Page.getAttribute("name").setValue("new name"); Sets the value of the Account Name field to “new name”.
Get the currently selected option object in an optionset attribute var addressTypeOption = Xrm.Page.getAttribute("address1_addresstypecode").getSelectedOption(); Assigns the selected option in the Address Type field to the addressTypeOption variable.
Determine whether an attribute value has changed var isNameChanged = Xrm.Page.getAttribute("name").getIsDirty(); Assigns a Boolean value that indicates whether the Account Name field value has changed to the isNameChanged variable.
Change whether data is required in a field in order to save a record Xrm.Page.getAttribute("creditlimit").setRequiredLevel("required"); Makes the Credit Limit field required.
Determine whether the data in an attribute will be submitted when the record is saved var nameSubmitMode = Xrm.Page.getAttribute("name").getSubmitMode(); The nameSubmitMode variable value will be either always, never, or dirty to represent the submitMode for the Account Name field..
Control whether data in an attribute will be saved when the record is saved Xrm.Page.getAttribute("name").setSubmitMode("always"); The example will force the Account Name field value to always be saved even when it has not changed.
When field level security has been applied to an attribute, determine whether a user has privileges to perform create, read, or update operations on the attribute. var canUpdateNameAttribute = Xrm.Page.getAttribute("name").getUserPrivilege().canUpdate; Assigns a Boolean value that represents the user’s privilege to update the Account Name field to the canUpdateNameAttribute variable. For more information, see How Field Security Can Be Used to Control Access to Field Values in Microsoft Dynamics CRM

Accessing Form Controls

Task Example
Access all the controls for a specific attribute var nameControls = Xrm.Page.getAttribute("name").controls.get(); Assigns an array of all the controls for the name attribute to the nameControls variable.
Access a control by name var nameControl = Xrm.Page.getControl("name"); The first control added to a form for an attribute will have the same name as the attribute. Each additional control name will have an index number appended to the name. For example, three controls for the name attribute will have the names: name, name1, and name2 respectively.
Access all controls var allControls = Xrm.Page.getControl(); Assigns an array of all the controls in the Xrm.Page.ui.controls collection to the allControls variable.

Use Form Controls

Task Example
Determine if a control is visible var isNameVisible = Xrm.Page.getControl("name").getVisible(); Assigns a Boolean value to the isNameVisible variable that represents whether the Account Name field is visible.
Hide or show a control Xrm.Page.getControl("name").setVisible(false); Hides the Account Name field.
Get a reference to the attribute for the control var nameAttribute = Xrm.Page.getControl("name").getAttribute(); Assigns the attribute for the control for the Account Name field to the nameAttribute variable.
Disable or enable a control Xrm.Page.getAttribute("name").controls.forEach( function (control, index){control.setDisabled(true); });
Tip
Remember that any attribute may have multiple controls.
Change the label for a control Xrm.Page.getControl("name").setLabel("Company Name");
Get the parent of a control var parentSection = Xrm.Page.getControl("name").getParent();
Set focus on a control Xrm.Page.getControl("name").setFocus();

Use IFRAME and Web Resource Controls An IFRAME control allows you to include a page within a form by providing a URL. An HTML web resource added to a form is presented using an IFRAMEelement. Silverlight and image web resources are embedded directly within the page.

Task Example
Set the URL for the content to be displayed in an IFRAME. Xrm.Page.getControl("IFRAME_targetPage").setSrc("http://www.bing.com"); Sets a URL to be the IFRAME.src for the control.
Get the URL that represents the default configured URL for an IFRAME. var initialUrl = Xrm.Page.getControl("IFRAME_bing").getInitialUrl(); Assigns the initial URL configured to be displayed in the IFRAME to the initialUrl variable.
Gets the object in the form that represents the web resource or IFRAME. var obj = Xrm.Page.getControl("IFRAME_bing").getObject(); Assigns an object reference to the obj variable. For an IFRAME this will be the IFRAME Document Object Model (DOM) element. For a Silverlight web resource it will be the Object Object element that represents the embedded Silverlight plug-in.

Use the SubGrid Control The SubGridcontrol is a grid within a form. It has one unique method.

Task Example
Refresh the data displayed in the subgrid Xrm.Page.getControl("accountContactsGrid").refresh();Refresh the Contactssubgrid.

Use Form Navigation You can use and manipulate the navigation items on the left side of the form. These navigation items typically show records related to the record displayed in the form. You can access navigation items using the Xrm.Page.ui.navigation.items Collection Like all collections in the form, there is a get and forEachmethod.

Task Example
Set the label of a navigation item Xrm.Page.ui.navigation.items.get("navAddresses").setLabel("Other Addresses");
Show or hide a navigation item Xrm.Page.ui.navigation.items.get("navAddresses").setVisible(false);
Set focus on a navigation item. Xrm.Page.ui.navigation.items.get("navAddresses").setFocus();

Use Tabs and SectionsEach form has collection of tabs. Each Tab has a collection of sections. Each section has a collection of controls. You can programmatically access these elements and use their methods.

Task Example
Expand or collapse a tab Xrm.Page.ui.tabs.get("general").setDisplayState("collapsed");
Show or hide a tab Xrm.Page.ui.tabs.get("general").setVisible(false);
Change the label for a tab Xrm.Page.ui.tabs.get("general").setLabel("Major");
Show or hide a section Xrm.Page.getControl("industrycode").getParent().setVisible(false);

Add or Remove Event Handlers at Runtime Event handlers are usually configured using the form editor in the application but you can also add them to the form OnSave event and attribute OnChange events at run time using these APIs. The examples in this section will refer to the following function definition: function myFunction() { //perform action here } While you can add an anonymous function, the function must have a name to reference in order to remove it.

Task Example
Add a function to the OnSave event Xrm.Page.data.entity.addOnSave(myFunction);
Remove a function from OnSave event Xrm.Page.data.entity.removeOnSave(myFunction);
Add a function to the OnChange event of an attribute. Xrm.Page.getAttribute("name").addOnChange(myFunction);Name field.
Remove a function from the OnChange event of an attribute Xrm.Page.getAttribute("name").removeOnChange(myFunction);

Use Contextual Information Use these methods to get information about the user, the organization, and the client. The following table provides some of the most useful context methods. For all the context methods, see Client-Side Context Reference

Task Example
Get the unique identifier for the current user. var userId = Xrm.Page.context.getUserId();

Use Entity Data The following table contains methods you can use to get information about the current record. For more information, see Xrm.Page.data.entity.

Task Example
Get the Id of the current record var recordId = Xrm.Page.data.entity.getId();
Save the current record Xrm.Page.data.entity.save(); Note: use saveandclose or saveandnew to perform the equivalent actions using the Save & Close or Save & New
Determine whether any data in the current record is changed. var isDirty = Xrm.Page.data.entity.getIsDirty();

  Other Resources Client-Side Programming Reference


This was originally posted here.

Comments

*This post is locked for comments