Get/Set Field values in C#, JavaScript and Utility Functions
Get and Set all types of fields in JavaScript
- Get and set value for a simple text field
var new_myfield = Xrm.Page.getAttribute("new_myfield").getValue();
Xrm.Page.getAttribute("new_myfield").setValue("Lorum Ipsum");
- Get and set value for an option set field
Get value is same as we get from a text field but in order to set value in option set, use option’s numeric value like this
Xrm.Page.getAttribute("new_myfield").setValue(17400000);
Get selected option text from option set field
var selectedOptionText = Xrm.Page.getAttribute("new_myfield ").getSelectedOption().text;
- Get and set two option field
var twoOptionFieldValue = Xrm.Page.getAttribute("MyTwoOptionField").getValue();
The variable twoOptionFieldValue will contain true or false.
Xrm.Page.getAttribute("MyTwoOptionField").setValue(true);
- Get and set whole number field
var twoOptionFieldValue = Xrm.Page.getAttribute("WholeNumberField").getValue();
Xrm.Page.getAttribute("WholeNumberField").setValue(12345);
- Get and set floating point field
var floatingPointFieldValue = Xrm.Page.getAttribute("FloatingPointNumberField").getValue();
Xrm.Page.getAttribute("FloatingPointNumberField").setValue(12345.123);
- Get and set decimal number field
var decimalNumberField = Xrm.Page.getAttribute("DecimalNumberField").getValue();
Xrm.Page.getAttribute("DecimalNumberField").setValue(12345.123);
- Get and Set currency field
var currencyFieldValue = Xrm.Page.getAttribute("CurrencyField").getValue();
Xrm.Page.getAttribute("CurrencyField").setValue(parseFloat(100.12));
- Get and Set multiple lines of text field
It is same as for single line of text. Please see point 1 for reference.
- Get and Set date and time field
Get is same as single line of text. Please see point 1 for reference. For Set, you just need to pass valid date and time from js.
- Get and Set lookup field
//-- Get lookup values
var customerId = Xrm.Page.getAttribute("parentaccountid").getValue()[0].id;
var customerName = Xrm.Page.getAttribute("parentaccountid").getValue()[0].name;
var entityType = Xrm.Page. getAttribute("parentaccountid").getValue()[0].entityType;
//-- Set lookup values
var id = "131268b6-4cc7-4598-94c1-309cc513af91";
var name = "Test Opportunity";
var entityType = "opportunity";
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = id;
lookupValue[0].name = name; //-- You can skip this property while setting value
lookupValue[0].entityType = entityType;
Xrm.Page.getAttribute("opportunityid").setValue(lookupValue);
Utility functions in JavaScript
- Set the entire form Read Only in Dynamics CRM 2013, 2015, 2016 and in Dynamics 365.
Here is code to make entire CRM form fields as read-only/disabled
function disableAllFieldsOnForm() {
Xrm.Page.ui.controls.forEach(
function (control, index) {
if (control.getControlType() != "subgrid" && control.getControlType() != "webresource")
control.setDisabled(true);//-- pass true or false
});
}
- Set requirement levels on fields
You can set requirement level like this
Xrm.Page.getAttribute("new_myfield").setRequiredLevel("none");
Xrm.Page.getAttribute("new_myfield").setRequiredLevel("required");
- Set visibility of Tabs, Sections and Fields
//-- Set Tab Visibility
Xrm.Page.ui.tabs.get("myTabName").setVisible(true); //-- pass true or false
//-- Set Section Visibility
Xrm.Page.ui.tabs.get("myTabName").sections.get("mySectionName").setVisible(true); //-- pass true or false
//-- Set Field Visibility
Xrm.Page.getControl("myfieldName").setVisible(true); //-- pass true or false
- Show/Hide notification on form and fields
Display an error message for the control to indicate that data isn’t valid. When this function is used, a red “X” icon appears next to the control.
var message = 'Please enter valid data in the field';
var uniqueId = "1";
Xrm.Page.getControl("myFieldName").setNotification(message,uniqueId);
Use this js function to display form level notifications.
//--Set notification on form
var message = 'Please enter valid data in the field';
var level = 'WARNING'; //-- It can also be "ERROR" or "INFO"
var uniqueId = "1";
Xrm.Page.ui.setFormNotification(message, level, uniqueId);
//--Another way to show notifications on form without uniqueId
Xrm.Page.ui.setFormNotification("Showing an INFORMATION notification.", "INFORMATION");
Xrm.Page.ui.setFormNotification("Showing a WARNING notification. ", "WARNING");
Xrm.Page.ui.setFormNotification("Showing an ERROR notification. ", "EROR");
//--Clear notification from form
var uniqueId = "1";
Xrm.Page.ui.clearFormNotification(uniqueId);
//-- Clear all notifications on form level
Xrm.Page.ui.clearFormNotification();
Get and Set all types of fields in C#
Get fields values
public void GetFields()
{
string fetchXmlString = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='5'>
<entity name='lead'>
<attribute name='companyname' />
<attribute name='leadid' />
<attribute name='businesstype' />
<attribute name='decisionmaker' />
<attribute name='numberofemployees' />
<attribute name='address1_longitude' />
<attribute name='exchangerate' />
<attribute name='revenue' />
<attribute name='description' />
<attribute name='rejectiondate' />
<attribute name='parentaccountid' />
<order attribute='companyname' descending='false' />
<filter type='and'>
<condition attribute='parentaccountid' operator='not-null'/>
</filter>
</entity>
</fetch>";
EntityCollection entColl = crmService.RetrieveMultiple(new FetchExpression(fetchXmlString));
if (entColl != null && entColl.Entities != null && entColl.Entities.Count > 0)
{
Entity entLead = entColl.Entities[0];
//-- Option Set
if (entLead.Contains("businesstype") && entLead["businesstype"] != null)
{
OptionSetValue businesstype = (OptionSetValue) entLead["businesstype"];
int selectedOptionValue = businesstype.Value;
}
//--Two option field
if (entLead.Contains("decisionmaker") && entLead["decisionmaker"] != null)
{
bool decisionmaker = (bool) entLead["decisionmaker"];
}
//-- Whole Number field
if (entLead.Contains("numberofemployees") && entLead["numberofemployees"] != null)
{
int numberofemployees = (int) entLead["numberofemployees"];
}
//-- Floating Point Number field
if (entLead.Contains("address1_longitude") && entLead["address1_longitude"] != null)
{
double address1_longitude = (double) entLead["address1_longitude"];
}
//--Decimal Number field
if (entLead.Contains("exchangerate") && entLead["exchangerate"] != null)
{
decimal exchangerate = (decimal)entLead["exchangerate"];
}
//--Currency field
if (entLead.Contains("revenue") && entLead["revenue"] != null)
{
Money revenue = (Money) entLead["revenue"];
decimal revenue_value = revenue.Value;
}
//--Date and Time field
if (entLead.Contains("rejectiondate") && entLead["rejectiondate"] != null)
{
DateTime rejectiondate = (DateTime) entLead["rejectiondate"];
}
//-- Lookup field
if (entLead.Contains("parentaccountid") && entLead["parentaccountid"] != null)
{
EntityReference parentaccountid = (EntityReference) entLead["parentaccountid"];
Guid id = parentaccountid.Id;
string logicalName = parentaccountid.LogicalName;
string name = parentaccountid.Name;
}
}
}
Set fields values
public void SetFields()
{
Entity entLead = new Entity("lead");
entLead["leadid"] = new Guid("21cb74e2-6254-e811-80dc-005056be7607");
//-- Option Set
entLead["businesstype"] = new OptionSetValue(0); //-- Individual = 0, Business = 1
//--Two option field
entLead["decisionmaker"] = true;
//-- Whole Number field
entLead["numberofemployees"] = 10;
//-- Floating Point Number field
entLead["address1_longitude"] = 110.123;
//--Decimal Number field
entLead["exchangerate"] = 11.21;
//--Currency field
Money revenue = new Money();
revenue.Value = new decimal(2351.1123);
entLead["revenue"] = revenue;
//--Date and Time field
entLead["rejectiondate"] = DateTime.Now;
//-- Lookup field
EntityReference parentaccountid = new EntityReference("account",new Guid("5DFCDD2D-FCE8-E311-940C-005056BD2B49"));
entLead["parentaccountid"] = parentaccountid;
crmService.Update(entLead);
}
References:
This was originally posted here.

Like
Report
*This post is locked for comments