CRM

How to get lookup value and set it on another lookup field in Dynamics CRM using Javascript.

In this blog, we will learn about How to get lookup value and set it on another lookup field in Dynamics CRM using Javascript.

Here I explain step by step How to get lookup value and set it on another lookup field in Dynamics CRM using Javascript.
Key Point:Before How to get lookup value and set it on another lookup field in Dynamics CRM using Javascript. Learn about Some basic knowledge.

  • How to create Solution?
  • How to create Entity ?
  • Step 1:First create solution. Click on Setting area and select Solutions.Below Screenshot describe.

    Step 2.After that Click on Solutions new popup window is open.

    Step 3.Click on New Button for create new solution New popup window is open.

    Step 4.Write the name of the Solution in Display Name field and select default publisher name in Publisher field and write the “Version”. Looks Like Screenshot and after that Click On “Save & Close” button

    Step 5.Click on Newly generated Solution “LookUpDemo”.

    Step 6.Next Step is to Create “Entity”. Click on New Button and Click on Entity.

    Step 7.When you click on Entity New popup window is open. Write the name of Entity in “Display Name” Field and Write the Plural Name of Entity in “Plural Name” Field. After that Select the Area that displays the “Entity” and finally click on “Save and Close” button.

    Step 8.Open your newly created Entity “LookUpDemoApp” and click on form.

    Step 9.After that open Form Type “Main”.Looks like Screenshot.

    Step 10.This step is important.When you click on Main Form New Window is open and then here we add two “LookUp” field on Main Form. Click on New Field.

    Step 11.Write the Name of “Lookup field” on Display name Fied and select data type “Lookup” and select Target record type as you wish here I am select “Account”. Finally, Click on “Save and close Button”.The same step follows for second “Lookup” Field looks like the screenshot. After that dragon and drop both lookup field on “General” Section.


    Step 12.Next Step to add “Web Resources”.Click On Web Resources.

    Step 13.After that click on “New” button.

    Step 14.Write the name of web resources in Name Field and Display Name Field after that select type “Script(JScript)” and Select Language after that click on “Text Editor” button like Screenshot.

    Step 15. Write this Code on Text Editor and Click On “OK” button.

     
    function GetSetLooupValue(){ 
    debugger
    //"new_lookupfield1 " is a name of  lookup Field
    //"new_lookupfield2" is name of lookup Field
    
     var lookupvalue1= Xrm.Page.getAttribute("new_lookupfield1");
        if (lookupvalue1 != null) {
            var lookup = lookupvalue1.getValue();
            if ((lookup != null)) {
                if (lookup[0].id != null) {
                    var lookup1 = new Array();//create a new Array
                    lookup1[0] = new Object();
                    lookup1[0].id = lookup[0].id;//retrieve the id of the record
                    lookup1[0].name = lookup[0].name;//retrieve the name of the record
                    lookup1[0].entityType = lookup[0].entityType;//retrieve the entitytype
              //here set lookup field one value into second look up Field
                    Xrm.Page.getAttribute("new_lookupfield2").setValue(lookup1);
                }
              
            }
        }
    }
    
    

    Step 15.After that Click on “Save” and “Publish”.

    Step 16.After that open your “Main Form” where you add “Lookup” Field. Double click on “LookupField1” or single click on “LookupField1” and after that Click on “Change Properties”.

    Step 17.When You are double click on “LookupField1” New window is open then click on Events. Here we add a web resources file name and write the name of Function “GetSetLooupValue” on “OnChange” Events and after that click on “Add” button.

    Step 18.After that Click on “Save” and “Publish”.

    Step 19.Final Step refresh your page and click on Setting. You can see show your Entity Name like “LookUpDemoApps”. Click on this entity.
    Fill the Value on First Lookup Field automatically insert this value on second LookupFiled. Screenshot Describe the Output.

    Any Question and suggestion related to this article please Comment Me.

    2 thoughts on “How to get lookup value and set it on another lookup field in Dynamics CRM using Javascript.

    Leave a Reply

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