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

Notifications

Announcements

No record found.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 47 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans