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.

  • LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Auto populate a field based

    Hi partner,

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

    Regards,

    Leo

  • Sip Khann Profile Picture
    55 on at
    RE: Auto populate a field based

    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
    RE: Auto populate a field based

    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
    RE: Auto populate a field based

    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

  • Verified answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Auto populate a field based

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,265 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,927 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans