Back to Basics # 40: Understand Form Context to Read Form Data with Web Resource in Dynamics CRM
Introduction:
In Dynamics 365 CRM, to perform customizations web resources of type JavaScript was widely used. To read 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 read 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, open web resource and attached JavaScript file , open that javascript file post downloading in any editor of your choice, in this example I considered visual studio code as shown in the below figure.
Step 4:
After Step 3, in handle onload function initialize a variable and verify if it has value for reading values on the form using below code
var formContext=executionContext.getFormContext();
if(formContext!==null && formContext!=’undefined’)
{
// logic here
}
as shown in the below figure.
Step 5:
After Step 4, to read text field like firstname use the below code
var firstname= formContext.getAttribute(“firstname”).getValue();
console.log(‘First name-‘+firstname);
as shown in the below figure.
Step 6:
After Step 5, to read Multi line text field like composite address use the below code
var compositeaddress=formContext.getAttribute(“address1_composite”).getValue();
console.log(‘Composite Address-‘+compositeaddress);
as shown in the below figure.
Step 7:
After Step 6, to read Phone field like telephone1 use the below code
var telephone=formContext.getAttribute(“telephone1”).getValue();
console.log(‘telephone-‘+telephone);
as shown in the below figure.
Step 8:
After Step 7, to read Date only field like BirthDate use the below code
var birthdate=formContext.getAttribute(“birthdate”).getValue();
console.log(‘birthdate-‘+birthdate);
as shown in the below figure.
Step 9:
After Step 8, to read Email field like emailaddress1 use the below code
var emailaddress=formContext.getAttribute(“emailaddress1”).getValue();
console.log(’emailaddress-‘+emailaddress);
as shown in the below figure.
Step 10:
After Step 9, to read whole number field like cr5bc_discount use the below code
var discount=formContext.getAttribute(“cr5bc_discount”).getValue();
console.log(‘discount-‘+discount);
as shown in the below figure.
Step 11:
After Step 10, to read Auto number field like cr5bc_tagiduse the below code
var tagid=formContext.getAttribute(“cr5bc_tagid”).getValue();
console.log(‘tagid-‘+tagid);
as shown in the below figure.
Step 12:
After Step 11, to read Choice field like gendercode the below code
var gendercodevalue=formContext.getAttribute(“gendercode”).getValue();
var gendercodetext=formContext.getAttribute(“gendercode”).getText();
console.log(‘gendercodetext-‘+gendercodetext);
as shown in the below figure.
Step 13:
After Step 12, to read guid of lookup field, lookup entity name,to get the name of the record like account selected in contact entity the below code
//Lookup logic
//to get the Guid of lookup record [account]
var lookupGuid=formContext.getAttribute(“parentcustomerid”).getValue()[0].id;
console.log(‘lookupGuid-‘+lookupGuid);
// entity name
var lookupentityType = formContext.getAttribute(“parentcustomerid”).getValue()[0].entityType;
console.log(‘lookupentityType-‘+lookupentityType);
// to get the name of the record
var lookupname = formContext.getAttribute(“parentcustomerid”).getValue()[0].name;
console.log(‘lookupname-‘+lookupname);
as shown in the below figure.
Step 13:
After Step 13, final code look like this in if condition
if(formContext!==null && formContext!=’undefined’)
{
//Text field
var firstname= formContext.getAttribute(“firstname”).getValue();
console.log(‘First name-‘+firstname);
//address1_composite- Multiline text
var compositeaddress=formContext.getAttribute(“address1_composite”).getValue();
console.log(‘Composite Address-‘+compositeaddress);
//telephone1- Phone
var telephone=formContext.getAttribute(“telephone1”).getValue();
console.log(‘telephone-‘+telephone);
//BirthDate- Date Only
var birthdate=formContext.getAttribute(“birthdate”).getValue();
console.log(‘birthdate-‘+birthdate);
//emailaddress1- Email
var emailaddress=formContext.getAttribute(“emailaddress1”).getValue();
console.log(’emailaddress-‘+emailaddress);
//cr5bc_discount- Whole number
var discount=formContext.getAttribute(“cr5bc_discount”).getValue();
console.log(‘discount-‘+discount);
//cr5bc_tagid – Autonumber
var tagid=formContext.getAttribute(“cr5bc_tagid”).getValue();
console.log(‘tagid-‘+tagid);
//gendercode – Choice
var gendercodevalue=formContext.getAttribute(“gendercode”).getValue();
var gendercodetext=formContext.getAttribute(“gendercode”).getText();
console.log(‘gendercodetext-‘+gendercodetext);
//Lookup logic
//to get the Guid of lookup record [account]
var lookupGuid=formContext.getAttribute(“parentcustomerid”).getValue()[0].id;
console.log(‘lookupGuid-‘+lookupGuid);
// entity name
var lookupentityType = formContext.getAttribute(“parentcustomerid”).getValue()[0].entityType;
console.log(‘lookupentityType-‘+lookupentityType);
// to get the name of the record
var lookupname = formContext.getAttribute(“parentcustomerid”).getValue()[0].name;
console.log(‘lookupname-‘+lookupname);
}
And save web resource and update selected web resource in step2 and publish
Step 15:
After Step 14,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.
- Make sure to enable check box pass execution context as first parameter while Configuring Event on On Load property as shown in the below figure
- Remaining part of the webresource was not explained to concentrate on the content related to this article.
- Microsoft documentation found here
Conclusion: In this way, one can easily use form context in web resource to read data present in CRM Forms for all entities easily.
This was originally posted here.
*This post is locked for comments