Dynamics 365, EnterprisePrepopulating Lookup and dropdown fields on a Form in D365 CRM Portals using JS
In this blog we will see how we can prepopulate lookup and dropdown (option set) fields on an Entity form in D365 CRM Portals using JS.
- D365 CRM Portals
- D365 CRM Environment
We know that if we want to prepopulate a lookup or a dropdown field on a form in CRM Portals the best method is to use Entity Form Metadata. But there is an alternative way to prepopulate the Lookup and dropdown (Option set) fields on an Entity form in D365 CRM Portals using JS.
To implement the functionality of prepopulating a lookup field we will be using the account lookup field on the CRM entity form based on the value entered in the contact field for a created opportunity in the partner portal. For the prepopulating the dropdown field we will be using the opportunity status field.
- First of most we will see how to prepopulate the lookup field on the entity form based on the value entered in another field. As mentioned in the above scenario we need to prepopulate an account field on an opportunity form based on the value present in the contact field.
- To insert a value into a lookup field using JS(jquery) we need the following information as mentioned below.
- The field value to be displayed. For eg. Account full name.
- The GUID if the record to be displayed. For eg. GUID of the account record.
- The logical name of the entity. For eg. Logical name of the account entity.
- Now we will see how to get these details form the existing information we have on the opportunity form. When we open an opportunity record, on the details page in the URL section we get the id of the opportunity record as shown below
- We will insert this id into the fetch xml using liquid template to obtain details of the contact related to the following opportunity record as shown below.
- As shown above we have used the opportunity id in the fetchxml to get the contact details. Once we have obtained the contact details from which we will be using the contact name value in the next fetchxml to get the related account information.
- In the following below shown fetchxml we will get the value and the id of the account record to be inserted.
Once we get the account record details we will write the jquery in the following manner to insert the value into the lookup field.
<script> $(“form_attributeid_name”).val(“record_value”); $(“form_attributeid”).val(“record_GUID”); $(“form_attributeid_entityname”).val(“entity_logicalname”); </script>
- Once we publish the liquid code we will see that the account field on the opportunity form is prepopulated with the value related to the value present in the contact field.
- For prepopulating the dropdown(option set) field we need the option set value of the record to be displayed. To set the value in the option set we will write the following jquery.
Hope you all find this blog helpful. Happy coding in CRM portals.