CRM 常用的一些JavaScript
Set Control Disabled:
function disableField(name, shouldDisable) //name is control name,shouldDisable is true or false
{
var control = Xrm.Page.getControl(name);
if (control != null)
{
control.setDisabled(shouldDisable);
}
}
Set Attribute Value:
function setAttributeValue(name, value)//name is control name
{
var attribute = Xrm.Page.getAttribute(name);
if (attribute != null)
{
attribute.setValue(value);
}
}
Set the value of an Option Set (pick list) field:
function SetOptionSetField() { var AddressType = Xrm.Page.data.entity.attributes.get("name"); AddressType.setValue(1); }
Set the value of a Lookup field:
// Set the value of a lookup field function SetLookupValue(fieldName, id, name, entityType) { if (fieldName != null) { var lookupValue = new Array(); lookupValue[0] = new Object(); lookupValue[0].id = id; lookupValue[0].name = name; lookupValue[0].entityType = entityType; Xrm.Page.getAttribute(fieldName).setValue(lookupValue); } }
Split a Full Name into First Name and Last Name fields:
function PopulateNameFields() { var ContactName = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name; var mySplitResult = ContactName.split(" "); var fName = mySplitResult[0]; var lName = mySplitResult[1]; Xrm.Page.data.entity.attributes.get("firstname").setValue(fName); Xrm.Page.data.entity.attributes.get("lastname").setValue(lName); }
Set the Requirement Level of a Field:
Note: this example sets the requirement level of the Address Type field on the Account form to Required.
Note: setRequiredLevel(“none”) would make the field optional again.
function SetRequirementLevel() { var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode"); AddressType.setRequiredLevel("required"); }
Show/Hide a field:
function hideName() { var name = Xrm.Page.ui.controls.get("name"); name.setVisible(false); }
Save the form:
function Save() { Xrm.Page.data.entity.save(); }
Close the form:
Note: the user will be prompted for confirmation if unsaved changes exist
function Close() { Xrm.Page.ui.close(); }
Get the GUID of the current record:
function AlertGUID() { var GUIDvalue = Xrm.Page.data.entity.getId(); if (GUIDvalue != null) { alert(GUIDvalue); }
}
Get the GUID of the current user:
function AlertGUIDofCurrentUser() {
var UserGUID = Xrm.Page.context.getUserId();
if (UserGUID != null) {
alert(UserGUID);
}
}
Determine the CRM server URL:
// Get the CRM URL var serverUrl = Xrm.Page.context.getServerUrl(); // Cater for URL differences between on premise and online if (serverUrl.match(/\/$/)) { serverUrl = serverUrl.substring(0, serverUrl.length - 1); }
Get the Security Roles of the current user:
function AlertRoles() { Xrm.Page.context.getUserRoles(); }
Refresh a Sub-Grid:
var targetgird = Xrm.Page.ui.controls.get("target_grid"); targetgird.refresh();