How to get/set form field values in Dynamics 365 CRM using js

getset form field values using jsgetset form field values using js

Dynamics CRM (Customer Relationship Management) is a powerful tool for managing customer interactions and data. The ability to customize forms to meet the needs of your organization is one of its key features. In Dynamics 365 CRM forms, JavaScript is commonly used to get and set field values dynamically.

We will explore how to efficiently interact with different types of fields within Dynamics 365 CRM forms in this article.

1. Getting Field Values

To retrieve the value of a field in Dynamics 365 CRM using JavaScript, you can utilize the Xrm.Page.getAttribute(attributeName).getValue() method.

2. Setting Field Values

Similarly, you can set the value of a field in Dynamics 365 CRM using JavaScript by employing the Xrm.Page.getAttribute(attributeName).setValue(value) method.

  • Set/Get the value of Text & Other Field Types:
function SingleLineOfTextFieldValue(executionContext) {
    // Get the form context from the execution context
    var formContext = executionContext.getFormContext();
    
    // Get the attribute for the account number field
    var accountNumberField = formContext.getAttribute("accountnumber");
    
    // Check if the account number field exists
    if (accountNumberField != null) {
        // Get the current value of the account number field
        var accountNumberValue = accountNumberField.getValue();
        
        // Set a new value for the account number field
        accountNumberField.setValue("BYA-2019-AIR-0099");
    }
}

3. Date and Time Field

// Get the attribute for due date from the form context
var dueDateField = formContext.getAttribute("duedate");

// Check if the due date field exists
if (dueDateField != null) {
    // Get the current value of the due date field
    var dueDateValue = dueDateField.getValue();
    
    // Use dueDateValue as needed

    // Create a new Date object for December 31, 2024, at 11:59:59 PM
    var newDate = new Date("2024-12-31T23:59:59");

    // Set the due date field value to the new date
    dueDateField.setValue(newDate);
}

4. Lookup and Customer Fields

  • Purpose: These fields allow you to reference related records from other entities. They are commonly used for scenarios like linking an account to a contact or associating a case with a customer.

    To get/set the value of a lookup field:
// Get the parent account lookup attribute from the form context
var parentAccountLookup = formContext.getAttribute("parentaccountid").getValue();

// Check if the parent account lookup has a value and if it's not empty
if (parentAccountLookup != null && parentAccountLookup.length > 0) {
    // Extract the GUID of the selected account
    var accountId = parentAccountLookup[0].id;
    // Extract the name of the selected account
    var accountName = parentAccountLookup[0].name;
    // Use accountId and accountName as needed
}

// Define the GUID and name for the new account
var newAccountId = "{GUID_of_another_account_record}";
var newAccountName = "Contoso Corp"; // Name of the account

// Create a new account lookup object
var newAccountLookup = [{
    id: newAccountId,
    name: newAccountName,
    entityType: "account" // Entity type (account or contact)
}];

// Set the value of the parent account lookup attribute to the new account lookup
formContext.getAttribute("parentaccountid").setValue(newAccountLookup);

Forms in Dynamics 365 CRM can be customized and extended using JavaScript. Using JavaScript, you can create dynamic and interactive forms tailored to the needs of your organization.

JavaScript empowers you to unlock the full potential of Dynamics 365 CRM forms, whether it’s retrieving data or updating field values dynamically based on user interactions.

Leave a Reply

Your email address will not be published. Required fields are marked *