Use of FormContext
Microsoft Dynamics CRM offers an object model (formcontext) to manipulate, analyze state of form and data.
The formcontext container provides properties and methods to work with the data on a form.
Following are the ways to get/set data from different data-types field using formcontext object model :
1. Single Line or Multiple line of Text :
getTextFieldValue: function(executionContext) {
var formContext = executionContext.getFormFcontext();
var getTextValue = formContext.getAttributes("firstname").getValue(); //get text value
var setTextValue = formContext.getAttributes("title").setValue("Write Text here!"); //set text value
}
2. OptionSet Value :
getOptionsetValue: function(executionContext) {
var formContext = executionContext.getFormFcontext();
var getOptionsetValue = formContext.getAttributes("FieldSchemaName").getValue(); //return code of optionset text
var getOptionsetText = formContext.getAttributes("FieldSchemaName").getText(); //return text of optionset
var setOptionsetValue = formContext.getAttributes("FieldSchemaName").setValue(1); // set option set value
}
3. Lookup Field :
//Get the Lookup Value
function getLookupDetails(executionContext) {
var formContext = executionContext.getFormContext();
var entityName, entityId, entityLabel, lookupFieldObject;
lookupFieldObject = formContext.data.entity.attributes.get("parentcontactid");
if (lookupFieldObject.getValue() != null) {
entityId = lookupFieldObject.getValue()[0].id.slice(1, -1);
entityName = lookupFieldObject.getValue()[0].entityType;
entityLabel = lookupFieldObject.getValue()[0].name;
}
}
// Set the Lookup Value.
function setLookupField(executionContext) {
var formContext = executionContext.getFormContext();
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.id = "74a968c5-6505-ea11-a81e-000d3a300ec6";
lookupItem.name = "Nancy";
lookupItem.entityType = "contact";
lookupData[0] = lookupItem;
formContext.data.entity.attributes.get("parentcontactid").setValue(lookupData);
}
4. Get Multiselect Optionset Value:
getMultiselectOptionsetValue: function(executionContext) {
var formContext = executionContext.getFormContext();
var OttPlatform = formContext.getAttribute('new_ottplatform') ? formContext.getAttribute('new_ottplatform').getValue() : null;
var OttPlatformCode = '';
if(OttPlatform) {
for(var i = 0; i < OttPlatform.length; i++) {
OttPlatformCode += OttPlatform[i] + ';';
}
formContext.getAttribute('textfieldSchemaName').setValue(OttPlatformCode);
} else {
formContext.getAttribute('textfieldSchemaName').setValue('');
}
}
Note: Xrm Page is deprecated from version 9.0.
Comments
Post a Comment