Java Script Code Reference MS CRM
Javascript
Code Reference Topics for CRM
Get value from a CRM field
var
temp = Xrm.Page.getAttribute("CRMFieldSchemaName").getValue();
Set value of a CRM field
Xrm.Page.getAttribute("CRMFieldSchemaName").setValue('My
New Value');
Get Guid of current entity
var
id = Xrm.Page.data.entity.getId();
Hide a tab in a CRM Form
Xrm.Page.ui.tabs.get(5).setVisible(false);
or
Xrm.Page.ui.tabs.get("tab_5").setVisible(false);
Show a tab in a CRM Form
Xrm.Page.ui.tabs.get(5).setVisible(true);
or
Xrm.Page.ui.tabs.get("tab_5").setVisible(true);
Hide a section in a CRM
Form
Xrm.Page.ui.tabs.get("tab_5").sections.get("tab_5_section_2").setVisible(false);
Show a section in a CRM
Form
Xrm.Page.ui.tabs.get("tab_5").sections.get("tab_5_section_2").setVisible(true);
Stop AutoSave
//Stop
Auto Save
var
saveEvent = context.getEventArgs();
if
(saveEvent.getSaveMode() == 70) { //Form AutoSave Event
saveEvent.preventDefault(); //Stops the
Save Event
}
Get schema name of a CRM
field
var
dateFieldSchemaName = execObj.getEventSource().getName();
Call the onchange event of
a field
Xrm.Page.getAttribute("CRMFieldSchemaName").fireOnChange();
Alternative Alert function
Xrm.Utility.alertDialog(message,onCloseCallback)
message:
The text of the message to display in the dialog (string).
onCloseCallback:
A function to execute when the OK button is clicked (function).
Confirm Dialog function
Xrm.Utility.confirmDialog(message,yesCloseCallback,noCloseCallback)
message:
The text of the message to display in the dialog (string).
yesCloseCallback:
A function to execute when the OK button is clicked (function).
noCloseCallback:
A function to execute when the Cancel button is clicked.
Get the selected value of
picklist
Xrm.Page.getAttribute("CRMFieldSchemaName").getSelectedOption().text;
or
Xrm.Page.getAttribute("CRMFieldSchemaName").getText();
Set the requirement level
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("none");
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("required");
Xrm.Page.getAttribute("CRMFieldSchemaName").setRequiredLevel("recommended");
Set Submit Mode
//
The data is always sent with a save.
Xrm.Page.getAttribute("CRMFieldSchemaName").setSubmitMode("<strong>always</strong>");
//
The data is never sent with a save. When this is used the field(s) in the form
for this attribute cannot be edited.
Xrm.Page.getAttribute("CRMFieldSchemaName").setSubmitMode("<strong>never</strong>");
//
Default behavior. The data is sent with the save when it has changed.
Xrm.Page.getAttribute("CRMFieldSchemaName").setSubmitMode("<strong>dirty</strong>");
Set the focus to a field
Xrm.Page.getControl("CRMFieldSchemaName").setFocus(true);
Stop an on save event
event.returnValue
= false;
Enable or Disable all
fields on a CRM Form
function
doesControlHaveAttribute(control) {
var controlType = control.getControlType();
return controlType != "iframe"
&& controlType != "webresource" && controlType !=
"subgrid";
}
function
disableFormFields(onOff) {
Xrm.Page.ui.controls.forEach(function (control,
index) {
if (doesControlHaveAttribute(control)) {
control.setDisabled(onOff);
}
});
}
Call
disableFormFields function giving true or false as a parameter to it in order
to enable or disable all fields on a CRM Form.
Show/Hide Tabs and Sections
function
setVisibleTabSection(tabname, sectionname, show) {
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null) {
if (sectionname == null)
tab.setVisible(show);
else {
var section = tab.sections.get(sectionname);
if (section != null) {
section.setVisible(show);
if (show)
tab.setVisible(show);
}
}
}
}
Note:
show must be boolean value like true or false.
Return array of strings of
users security role GUIDs:
Xrm.Page.context.getUserRoles();
Writing Client based
scripts (Writing code for CRM for Tablets)
var
isCrmForTablets = (Xrm.Page.context.client.getClient() == "Mobile");
if
(isCrmForTablets)
{
// Code for CRM for Tablets only goes here
}
else
{
// Code for web browser or Outlook Client
only goes here
}
//
Code for any Client goes here
Clients
here may be a Browser, Outlook or Mobile. Their values are in a string format
as Web, Outlook and Mobile respectively.
Set statuscode of a Custom
Entity
Xrm.Page.getAttribute('statuscode').setValue(1);
Save current entity record
on the form
Xrm.Page.data.entity.save();
Save current entity record
and open New Form
Xrm.Page.data.entity.save('saveandnew');
Save current entity record
and Close Window
Xrm.Page.data.entity.save('saveandclose');
Fire on change event using
javascript
Xrm.Page.getAttribute('CRMFieldSchemaName').fireOnChange();
Check if any fields in the
form have been modified (returns boolean value)
var
formIsModified = Xrm.Page.data.entity.getIsDirty();
Close current Crm page
using JavaScript
Xrm.Page.ui.close();
Refresh CRM Form using
JavaScript
Xrm.Page.data.refresh();
How to get an Option by passing value?
The
code below returns an Option object with text and value properties. value
argument represents the numeric value of the optinonsetvalue field for
searching the Option:
var
myOption = Xrm.Page.getAttribute("fieldname").getOption(value);
How to get selected CRM
Option Set Value item?
The
code below returns the Option object with text and value properties:
var
selectedOption =
Xrm.Page.getAttribute("fieldname").getSelectedOption();
How to get selected value
of an CRM Option Set Value?
The
code below gives us the numeric value of selected OptionSetValue field:
var
optionsetValue = Xrm.Page.getAttribute("fieldname").getValue();
How to get Text of an CRM
Option Set Value?
The
code below gives us the text of selected OptionSetValue field:
var
optionsetText = Xrm.Page.getAttribute("fieldname").getText();
How to set an CRM Option
Set Value (by numeric value)?
The
code below helps us to set the value of OptionSetValue by a numeric value
passed to the setValue function:
Xrm.Page.getAttribute("fieldname").setValue(value);
How to set an CRM Option
Set Value by Text?
Actually
we can’t set the selected item by text but we can search all options until we
find the one whose text (label) is the same as one the one you have then get
its value like below:
function
setOptionSetValueByText("fieldname", optionText) {
var options =
Xrm.Page.getAttribute("fieldname").getOptions();
for (i = 0; i < options.length; i++) {
if (options[i].text == optionText) {
Xrm.Page.getAttribute("fieldname").setValue(options[i].value);
}
}
}
How to get values of a
lookup field?
function
getLookupDetails() {
var entityName, entityId, entityLabel,
lookupFieldObject;
// parentaccountid is the lookup field name
that we try to reach its values
lookupFieldObject =
Xrm.Page.data.entity.attributes.get('parentaccountid');
if (lookupFieldObject.getValue() != null) {
entityId =
lookupFieldObject.getValue()[0].id;
entityName =
lookupFieldObject.getValue()[0].entityType;
entityLabel =
lookupFieldObject.getValue()[0].name;
}
// Here you can use the id, name and
entityType of the lookup
// Here also you can get the entity and the
attribute values of it
// To do that please refer to the Note
below !
}
Note: You can refer to my article (Retrieve
Data using OData queries with Javascript in CRM 2013) to pass the id of a
lookup field and get values of an entity.
How to set a lookup field
value?
The
function below sets new_teamid lookup field:
function
setLookupField() {
var lookupData = new Array();
var lookupItem = new Object();
//Set the GUID
lookupItem.id = myTeamId;
//Set the name
lookupItem.name = myTeamName;
lookupItem.entityType = "team";
lookupData[0] = lookupItem;
//If existing value is empty, then set new
value
var existingValue =
Xrm.Page.getAttribute("new_teamid").getValue();
if (existingValue === null) {
Xrm.Page.getAttribute("new_teamid").setValue([{
id: myTeamId,
name: myTeamName,
entityType: "team"
}]);
} else {
return;
}
}
In
this article I am going to share a javascript code snippet to let you retrieve
option set label (text) or value of any Optionset field from any entity in
Microsoft Dynamics CRM.
First
of all you need to refer a js resource file named Sdk.Metadata.js in your form.
You
can find this resource at
sdk\samplecode\js\soapforjscript\soapforjscript\scripts
After
adding this resource to your CRM Form you just need to use below function:
//
This function Gets optionset values and text via given entityname and optionset
attribute
//
To use this function Sdk.Metada.js must be added to form.
//
This javascript (Sdk.Metada.js) file source can be found at the location :
"sdk\samplecode\js\soapforjscript\soapforjscript\scripts"
function
GetOptionSetLabel(EntityLogicalName, AttributeLogicalName) {
SDK.Metadata.RetrieveAttribute(EntityLogicalName, AttributeLogicalName,
"00000000-0000-0000-0000-000000000000", true,
function(result) {
for (var i = 0; i <
result.OptionSet.Options.length; i++) {
var text =
result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;
var value =
result.OptionSet.Options[i].Value;
//
Here you can use text or value of the OptionSet to do whatever you want
}
},
function(error) {}
);
}
Showing notification on a
CRM Form
The
code below runs when the Contact Form loads. I showed the three types of
notifications at the same time on the same Form. You can do it seperately if
you would like to.
Xrm.Page.ui.setFormNotification('Hey!
This is an ERROR notification', 'ERROR');
Xrm.Page.ui.setFormNotification('Hey!
This is a WARNING notifcation', 'WARNING');
Xrm.Page.ui.setFormNotification('Hey!
This is an INFORMATION notification', 'INFORMATION');
Hiding notification on CRM Form
In
order to hide a notification on a CRM Form we should have passed one more value
to setFormNotification method to identify the notification.
For
example if we have written our code above like below,
The
first notification would have had the the ID of 1
The
second notification would have had the the ID of 2
The
third notification would have had the the ID of 3
The
reason of that we passed the ID values like 1, 2 and 3 respectively as third
parameter to setFormNotification method .
Xrm.Page.ui.setFormNotification('Hey!
This is an ERROR notification', 'ERROR', '1');
Xrm.Page.ui.setFormNotification('Hey!
This is a WARNING notifcation', 'WARNING', '2');
Xrm.Page.ui.setFormNotification('Hey!
This is an INFORMATION notification', 'INFORMATION', '3');
Now
we can hide these notifications from the Contact Form.
To
do that we need the code below:
Xrm.Page.ui.clearFormNotification('1');
Xrm.Page.ui.clearFormNotification('2');
Xrm.Page.ui.clearFormNotification('3');
Notifications of CRM fields
We
can also set notifications for CRM fields on the Forms depending on the
condition of the corresponding field value.
To
do that we write the below code:
Xrm.Page.getControl("CrmFieldName").setNotification("YourMessageAboutTheField");
The
result of the code above for the address field will be like below:
field
notfications in CRM
Hiding
notification of a CRM field
We
can use the below code to hide a notification of a field.
Xrm.Page.getControl('CrmFieldName').clearNotification();
Expand Collapse tabs using JavaScript in CRM
Expand a tab in CRM
Xrm.Page.ui.tabs.get("tabname").setDisplayState('expanded');
Collapse a tab in CRM
Xrm.Page.ui.tabs.get("tabname").setDisplayState('collapsed');
Comments
Post a Comment