web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Auto populate a field based

(0) ShareShare
ReportReport
Posted on by 55

Hi all,

I am completely new to javascript. We have come across an issue where a text field on a form (Form A) needs to be populated from a field on contact form. The logic is when a field value ( lookup field on Form A) is changed the text field below it shows the related value. Could someone possibly share the code please.

I have the same question (0)
  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    So you means you want the field A on form A be populated from field B on contact form based on the lookup value of contact in form A?

    If so, please refer to the following code.

    function getValueFromLookUp(executionContext){
        var formContext=executionContext.getFormContext();
        //get contact lookup field value on form A
        var Lookup_Contact=formContext.getAttribute("Lookup_contact");
        if(Lookup_Contact!=null){
            //get contact record id
            var value=Lookup_Contact.getValue();
            var id=value[0].id;
            //use record id to retrieve field B on contact form
            Xrm.WebApi.retrieveRecord("contact",id,"?$select=fieldB").then(
                function success(result){
                    //get the field B value and set it to field A 
                    var fieldB=result.fieldB;
                    formContext.getAttribute("fieldA").setValue(fieldB);
                },
                function (error){
                    console.log(error.message);
                }
            )
    
        }
        
    }

    Add this JS function to form A contact lookup field onchange event.

    Remember to click "Pass execution context as first parameter"

    pastedimage1588139015888v1.png

    Best Regards,

    Leo

  • Sip Khann Profile Picture
    55 on at

    Hi Leo Zhang ,

    Thanks a lot. That did work like a charm except for one small issue when lookup field is empty again on form A, i get the below error. And also i had to add another field (text field) as the current field (field b) is a whole number (data type). 

    Really appreciate the response.

    pastedimage1588726200958v1.png

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    1.You can add a judge rule that checks if the lookup field is empty.

    2.Whatever number or text field, you could use "formContext.getAttribute("field").setValue(value);" to set value to them.

    Here's a blog about how to set/get values from different type of field.

    https://crm365blog.wordpress.com/2018/05/29/get-set-field-values-in-c-javascript-and-utility-functions/

    Best Regards,

    Leo

  • Sip Khann Profile Picture
    55 on at

    Hi Leo Zhan,

    Thanks for suggestion. I tried adding the below code but it does not work. Cant understand why though.

    else if (Lookup_Contact==null) {

    formContext.getAttribute("Field").setValue(field A);

    }
      
    }

     

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    I think you need to change the code to "Lookup_Contact != null"

    Regards,

    Leo

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 79

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans