Auto-populate Lookup Values using JavaScript in D365
1. Create One Many Relationship or Many to One Relationship between any two Entities
Ex: Account and Contact
a. Consider Account as A Parent Entity(Parent Table) and Contact as Child Entity(Child Table)
b.1-N or N-1 relationship is created between them. So Lookup is Field is Available in Child Table
c. Create some Columns which were similar to Parent Table i.e. Account and Child Table i.e. Contact
Example Columns are:
1.CompanyName(Single line of Text) => new_companyname
2.CompanyPhoneNumber(phone number) => new_companyphonenumber
3.CompanyEmail(Email) => new_companyemail
4.CompanyIncome(Currency) => new_companyincome
5.CompanyEstablishedDate(Date) => new_companyestablisheddate
c. In the main Form of contact add similar fields which we created and the Lookup Field Generated
2. Copy all the Logical Names of the Fields or Columns we have Created.
3. Write JavaScript as below Directly on WebResource(classic)or an Editor and save it in WebResource Library (modern)
function LookupFieldValidation(executionContext)
{
var formContext = executionContext.getFormContext();
var accountlookup = formContext.getAttribute("accountlookup").getValue();//getting lookup value
if(accountlookup != null) //checking whether account lookup is null or not
{
var lookupId = accountlookup[0].id; // getting the id of accountLookup and storing in Variable Id
//Xrm.WebApi.online.retrieveRecord("parententity",id,"?$select=columnvalue1,columnvalue2").then({ retrieve records and storing them in variables})
//retrieving Records from Parent Entity through Id and the Selected Records
Xrm.WebApi.online.retrieveRecord("account",lookupId,"?$select=new_companyname,new_companyphonenumber,new_companyemail,new_companyincome,new_companyestablisheddate").then(
function success(result){
var companyname = result["new_companyname"];
var companyphonenumber = result["new_companyphonenumber"];
var companyemail = result["new_companyemail"];
var companyincome = result["new_companyincome"];
var money = companyincome; //use this type of variable assigning if you don't get any output values
var companyestablisheddate = result["new_companyestablisheddate"];
var datevalue = companyestablisheddate;
formContext.getAttribute("new_companyname").setValue(companyname);
formContext.getAttribute("new_companyphonenumber").setValue(companyphonenumber);
formContext.getAttribute("new_companyemail").setValue(companyemail);
formContext.getAttribute("new_companyincome").setValue(money);
formContext.getAttribute("new_companyestablisheddate").setValue(datevalue);
},
function (error)
{
Xrm.Utility.alertDialog(error.message); // Alert Error with a Message if failed
}
) ;
}
}
4. Trigger this On Event Handlers
1.OnSave of the Form
else
2.OnChange of the Lookup Field
5. Save and Publish
6. Hope you got Output in your Applications, Wohoooo .....
For Any Queries or Discussions Contact me @gmail: samsonkongala1@gmail.com
Also, Contact me on LinkedIn :
https://www.linkedin.com/in/samson-kongala-633019185/
Comments
Post a Comment