Retrieve Value from Lookup Field using Java Script

Hi Readers,

Step 1: Create a JavaScript as mentioned below.

Function Name: retrive_customer_lookup

Pass parameter in function to access exiting context(Data) on record.

cr601_customername replace with Lookup field Logical Name.

customername_fieldControl store GUID of Customer Name Lookup Field.


Step 2: Write down the code in java script.

function retrive_customer_lookup(executionContext)

{

    debugger;   

    var formContext = executionContext.getFormContext();

    var customername_fieldControl = formContext.getAttribute("cr601_customername").getValue()[0].id.slice(1, -1);//;


    if (customername_fieldControl !=null && customername_fieldControl != undefined && customername_fieldControl != "")

    {

        var req = new XMLHttpRequest();

        req.open("GET", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/cr601_customerdetails("+customername_fieldControl+")?$select=cr601_address,cr601_city,cr601_state&$expand=cr601_cr601_customersitedetails_CustomerName_cr($select=_cr601_customername_value)", true);

        req.setRequestHeader("OData-MaxVersion", "4.0");

        req.setRequestHeader("OData-Version", "4.0");

        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        req.setRequestHeader("Accept", "application/json");

        req.setRequestHeader("Prefer", "odata.include-annotations=*");

        req.onreadystatechange = function () {

            if (this.readyState === 4) {

                req.onreadystatechange = null;

                if (this.status === 200) {

                    var result = JSON.parse(this.response);

                    console.log(result);

                    // Columns

                    var cr601_address = result["cr601_address"]; // Text

                    var cr601_city = result["cr601_city"]; // Text

                    var cr601_state = result["cr601_state"]; // Text


                    if (cr601_address  != null)

                    {

                    formContext.getAttribute("cr601_address").setValue(cr601_address);

                    }


                    if (cr601_city  != null)

                    {

                        formContext.getAttribute("cr601_city").setValue(cr601_city);

                    }


                    if (cr601_state  != null)

                    {

                        formContext.getAttribute("cr601_state").setValue(cr601_state);

                    }

                } 

                else {

                    console.log(this.responseText);

                }

            }

        };

        req.send();

    }

}


Step 2: Open the Forms as below.










Step 3: Click on the Main form to add the javascript function.











Step 4: Select the Customer Name field and click on the Event Handler button shown in the below image.




































Step 5:  Select Libery file. and write function names as mentioned in JavaScript.
Click on "Pass Execution context as first parameter" to pass the existing context.

Step 6: After that clock on the "Done" button shown in the above image and click on to "Save and Publish Button".


Step 7: Open the form to review the development. Select 'Customer Name' and verify if the other fields are automatically populated.




Regards,
Dharmendra Chavda

Comments

Popular posts from this blog

Run a Dynamics 365 Plugin from JavaScript on Button Click

Data Upload into CRM from Excel Using Power Automate

Add Java Script and CSS in PowerApps Portal