Back to Basics # 41: Use Form Context To Set Form Values with Web resource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, to perform customizations web resources of type JavaScript was widely used. To set  different attributes that are present in CRM Form, form context client api reference can be used. As an example, contact form and contact entity record used to set different attributes using form context.

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, to write or set  text field like firstname use the below code

formContext.getAttribute(“firstname”).setValue(‘Venkata’);

                //Text field

                var firstname= formContext.getAttribute(“firstname”).getValue();

                console.log(‘First name-‘+firstname);

as shown in the below figure.

Step 4:

After Step 3, to write/set Multi line text field like composite address use the below code

   formContext.getAttribute(“address1_composite”).setValue(‘Hyderabad1234’);

            //address1_composite- Multiline text

             var compositeaddress=formContext.getAttribute(“address1_composite”).getValue();

             console.log(‘Composite Address-‘+compositeaddress);

as shown in the below figure.

Step 5:

After Step 4, to write/set Phone field like telephone1 use the below code

formContext.getAttribute(“telephone1”).setValue(‘123456’);

            //telephone1- Phone

            var telephone=formContext.getAttribute(“telephone1”).getValue();

            console.log(‘telephone-‘+telephone);

as shown in the below figure.

Step 6:

After Step 5, to write/set whole number field like cr5bc_discount use the below code

formContext.getAttribute(“cr5bc_discount”).setValue(24);

var discount=formContext.getAttribute(“cr5bc_discount”).getValue();

            console.log(‘discount-‘+discount);

as shown in the below figure.

Step 7:

After Step 6, save and publish the web resource and open contact record in dynamics and observe console log to observe the results as shown in the below figure

Figure 7

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. We can also set /write values to other data types as well.
  3. Microsoft documentation found here

Conclusion: In this way, one can easily use form context in web resource to set / write data present in CRM Forms for all entities easily.

2 thoughts on “Back to Basics # 41: Use Form Context To Set Form Values with Web resource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind February 2022 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s