How to Get and Set All Types of Value Using Custom JavaScript in Dynamics 365

How to Get and Set All Types of Value Using Custom JavaScript in Dynamics 365



Publish Date: 12/26/2024


In Dynamics 365, JavaScript is a powerful tool for manipulating and managing data on forms. Whether you are building custom scripts or automating business processes, understanding how to get and set values for different field types using JavaScript is essential. This guide will walk you through the various field types in Dynamics 365 and how to efficiently retrieve and modify their values using JavaScript.

What is FormContext in Dynamics 365?

The FormContext object provides access to all the data on a form, allowing developers to interact with field values, set them, and trigger business logic. By leveraging the FormContext, you can easily retrieve and set values for various field types, such as text fields, numbers, currency, date-time fields, and lookups.



Getting and Setting Field Values in JavaScript

1. Single Line of Text

  var singlelineoftext = formContext.getAttribute("field_logicalName").getValue();
  formContext.getAttribute("field_logicalName").setValue(singlelineoftext);
  // or
  formContext.getAttribute("field_logicalName").setValue("Sample Text");        

2. Multiple Lines of Text (Memo)

var Multiplelineoftext = formContext.getAttribute("field_logicalName").getValue();
formContext.getAttribute("field_logicalName").setValue(Multiplelineoftext);
//or
formContext.getAttribute("field_logicalName").setValue("Sample multiline text value.");        

3. Number Fields (Whole, Decimal, Floating Point)

  var Number = formContext.getAttribute("field_logicalName").getValue();
  formContext.getAttribute("field_logicalName").setValue(Number);
  //or
  formContext.getAttribute("new_numberfield").setValue(12345);        

4. Currency

   var Currency = formContext.getAttribute("field_logicalName").getValue();
   formContext.getAttribute("field_logicalName").setValue(Currency);
   //or
   formContext.getAttribute("new_currencyfield").setValue(5000.00);        

5. Date and Time

    var dateandtime = formContext.getAttribute("field_logicalName").getValue();
    formContext.getAttribute("field_logicalName").setValue(dateandtime);
    //or
    const today = new Date();
    formContext.getAttribute("new_datefield").setValue(today);        

6. Option Set (Dropdown)

    var OptionSet = formContext.getAttribute("field_logicalName").getValue();
    formContext.getAttribute("field_logicalName").setValue(OptionSet);
    //or
    formContext.getAttribute("new_optionsetfield").setValue(100000001);        

7. Two Options (Yes/No)

    var twoOption = formContext.getAttribute("field_logicalName").getValue();
    formContext.getAttribute("field_logicalName").setValue(twoOption);
    //or
    formContext.getAttribute("new_twooptions").setValue(true); // true = Yes, false = No.        

8. Lookup Fields

    var lookupField = formContext.getAttribute("field_logicalName").getValue();
    if (lookupField) {
        formContext.getAttribute("field_logicalName").setValue([
            {
                id: lookupField[0].id,
                name: lookupField[0].name,
                entityType: lookupField[0].entityType
            }
        ]);
    }

                                                         //or


var lookupField = formContext.getAttribute("field_logicalName").getValue();

var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = lookupField[0].id;
lookupValue[0].name = lookupField[0].name;
lookupValue[0].entityType = lookupField[0].entityType;

formContext.getAttribute("field_logicalName").setValue(lookupValue);
        




"Thank you for reading! If you found this article helpful and want more updates about Dynamics 365 and integration solutions, don't forget to follow me here on LinkedIn!"


Visit Us:


要查看或添加评论,请登录

STERTAK || DYNAMICS 365 || POWER PLATFORM的更多文章

社区洞察

其他会员也浏览了