MS CRM developers often have to write JavaScript in order to help manipulate the CRM form. Many libraries are now available which make client side scripting, DOM manipulation, validation and ajax much easier, however CRM 2011 now comes with some useful objects and features which now makes it easier than ever to do this.
In brief you will want to lookup the following:
Here are some examples of the most common I have come across:In brief you will want to lookup the following:
Xrm.Page.data.entity.attributes – field access and manipulation on the form
Xrm.Page.ui.controls – working with ui controls on the form
Xrm.Page.ui.navigation.items – working with navigation items on the form
Xrm.Utility – a set of useful helper functions
How to get text field value:
var value = Xrm.Page.data.entity.attributes.get("fieldname").getValue();
How to get the ID of a lookup field:
var id = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;
How to get the ID of the current record:
var id = Xrm.Page.data.entity.getId();
How to get the Text of a lookup field:
var text = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].name;
How to set the value of a lookup field:
function PopulateLookup(fieldname, id, name, type) {
var lookupData = new Array();
lookupData[0] = new Object();
lookupData[0].id = id;
lookupData[0].name = name;
lookupData[0].entityType = type;
Xrm.Page.getAttribute(fieldname).setValue(lookupData);
}
How to set value of a Text field:
Xrm.Page.data.entity.attributes.get("fieldname").setValue("my value goes here!");
Notes: as with all drop-downs, each option has a value and display text and you should also check for null
How to get option set value:
var opField = Xrm.Page.data.entity.attributes.get("fieldname");
var value = opField.getValue();
How to get option set text:
var opField = Xrm.Page.data.entity.attributes.get("fieldname");
var text = opField.getText();
How to select option in Option Set field:
Xrm.Page.data.entity.attributes.get("fieldname").setValue(2);
How to get values from a Data field:
var dateField = Xrm.Page.data.entity.attributes.get("fieldname").getValue();
if (dateField != null) {
var day = dateField.getDate();
var month = (dateField.getMonth() + 1); // increment by 1 as Jan starts at 0
var year = dateField.getFullYear();
}
How to set a Date field to Todays date:
Xrm.Page.data.entity.attributes.get("fieldname").setValue(new Date());
How to Hide and Show a field:
var field = Xrm.Page.ui.controls.get("fieldname");
var field = Xrm.Page.ui.controls.get("fieldname");
field.setVisible(false); // hide field
field.setVisible(true); // show field
How to Disabled and Enabled a field:
var field = Xrm.Page.ui.controls.get("fieldname");
field.setDisabled(true); // disable field
field.setDisabled(false); // enable field
How to Hide and Show a Navigation Item:
var navitem = Xrm.Page.ui.navigation.items.get("navitemname");
navitem.setVisible(false); // hide
navitem.setVisible(true); // show
How to Hide and Show a Tab:
var tab = Xrm.Page.ui.tabs.get("tabname");
tab.setVisible(false); // hide
tab.setVisible(true); // show
How to Save, Save and Close, and Close a Form:
Xrm.Page.data.entity.save(); // Save
Xrm.Page.data.entity.save(); // Save
Xrm.Page.data.entity.save("saveandclose"); // Save and Close
Xrm.Page.ui.close(); // Close
How to Identify Form Mode:
Xrm.Page.ui.close(); // Close
How to Identify Form Mode:
var formMode = Xrm.Page.ui.getFormType();
switch(formMode)
{
switch(formMode)
{
case 1: // Create
break;
case 2: // Update
break;
case 3: // Read Only
break;
case 4: // Disabled
break;
}
How to get the ID of the current user:
var id = Xrm.Page.context.getUserId();
How to get all the security roles for the current user:
var roles = Xrm.Page.context.getUserRoles();
How to open an existing CRM record in a new window:
Xrm.Utility.openEntityForm("account", guid);
Note: Just replace the first parameter with the entity logical name and the second parameter with the record guid.
How to open a new CRM record window:
Xrm.Utility.openEntityForm("account");
How to refresh a Sub-Grid:
Xrm.Page.ui.controls.get("gridname").refresh();
How to set Requirement Level of field using JavaScript:
Xrm.Page.getAttribute("fieldname").setRequiredLevel("none"); // No Constraint
Xrm.Page.getAttribute("fieldname").setRequiredLevel("required"); // Business Required
Xrm.Page.getAttribute("fieldname").setRequiredLevel("recommended"); // Business Recommended
How to get field Requirement Level:
Xrm.Page.getAttribute("fieldname").getRequiredLevel();
How to add an OnChange event handler to a field:
Xrm.Page.getAttribute("fieldname").addOnChange(functionName);
How to identify if a field value has changed:
Xrm.Page.getAttribute("fieldname").getIsDirty();
Note: this returns a Boolean value true/false
How to get all the security roles for the current user:
var roles = Xrm.Page.context.getUserRoles();
How to open an existing CRM record in a new window:
Xrm.Utility.openEntityForm("account", guid);
Note: Just replace the first parameter with the entity logical name and the second parameter with the record guid.
How to open a new CRM record window:
Xrm.Utility.openEntityForm("account");
How to refresh a Sub-Grid:
Xrm.Page.ui.controls.get("gridname").refresh();
How to set Requirement Level of field using JavaScript:
Xrm.Page.getAttribute("fieldname").setRequiredLevel("none"); // No Constraint
Xrm.Page.getAttribute("fieldname").setRequiredLevel("required"); // Business Required
Xrm.Page.getAttribute("fieldname").setRequiredLevel("recommended"); // Business Recommended
How to get field Requirement Level:
Xrm.Page.getAttribute("fieldname").getRequiredLevel();
How to add an OnChange event handler to a field:
Xrm.Page.getAttribute("fieldname").addOnChange(functionName);
How to identify if a field value has changed:
Xrm.Page.getAttribute("fieldname").getIsDirty();
Note: this returns a Boolean value true/false
Hi
ReplyDeleteMaybe u can help me
I want, via java , to change automatically the owner of the task when creating a new one ..
Thanks in advance
maya
Thanks for sharing a best informetion
ReplyDeleteCRM Development Dubai:ERP Development Dubai