JavaScript function for getting and setting field values in Dynamics CRM

 

JavaScript function for getting and setting field values in Dynamics CRM:

function getSetFieldValue(executionContext, fieldName, fieldValue) {

  // Get the form context from the execution context
  var formContext = executionContext.getFormContext();
  // Get the field attribute
  var field = formContext.getAttribute(fieldName);
  // Check if the field exists
  if (field) {
    // Check if the field value is provided
    if (fieldValue !== undefined) {
      // Set the field value
      field.setValue(fieldValue);
    }
    // Return the field value
    return field.getValue();
  }
  // Return null if the field does not exist
  return null;
}
You can use this function to get or set any type of field value, such as text, option set, date, lookup, etc. For example:
// Get the value of a text field
var name = getSetFieldValue(executionContext, "name");

// Set the value of an option set field
getSetFieldValue(executionContext, "statuscode", 2);

// Get the value of a lookup field
var account = getSetFieldValue(executionContext, "parentaccountid");

Comments

Popular posts from this blog

Power Automate - Update Child record when parent record is updated by using power automate in MS Dynamics CRM

set the active stage in Dynamics 365 when the option set value is "yes"