Introduction
In Dynamics 365 CRM to perform customizations, web resources of type JavaScript were 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 are used to set different attributes using form context.
Step 1
Login to the required environment and select the 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 Multiline 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
Note
- Make sure to publish all customizations and upload JavaScript (js) file.
- We can also set /write values to other data types as well.
- 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.