› Dynamics 365, Enterprise › Prepopulating Lookup and dropdown fields on a Form in D365 CRM Portals using JS

Prepopulating Lookup and dropdown fields on a Form in D365 CRM Portals using JS

Lester D'Souza
Software Developer
Published on January 1, 2018
Category: Dynamics 365, Enterprise

Overview:

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.

Pre-Requisites:

  1. D365 CRM Portals
  2. D365 CRM Environment

Introduction

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.

Scenario:

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.

Process:

  • 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.
    1. The field value to be displayed. For eg. Account full name.
    2. The GUID if the record to be displayed. For eg. GUID of the account record.
    3. 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.
Jquery syntax:

<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.
    $(form_attributeid).val(“attribute_optionsetvalue”);

Hope you all find this blog helpful. Happy coding in CRM portals.



Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Looking for a demo?

We will get back to you very soon!
We promise.

  • This field is for validation purposes and should be left unchanged.