Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How do I Autopopulate a field based on the lookup selection?

Posted on by 138

Hello All,

I am new to D365, currently I am trying to write a JS code to Auto populate a Company name Text field if the respective Company Name (Existing Client) is chosen in lookup by the user in the Custom entity. I can write this as a workflow but it happens only on Save. So I am looking to write JS code to achieve this change. So whenever users change the value in Existing Client Field then the respective company name should be populated from the Existing Client.

Can you kindly help here please?

2465.pastedimage1630960982914v1.png

Best Regards

Gowtham

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do I Autopopulate a field based on the lookup selection?

    Hi Gowtham89, 

    Please click Yes under "Did this answer your question?" to close this thread if the answer is helpful.

     pastedimage1631063714359v1.png

    Thanks.

  • Gowtham89 Profile Picture
    Gowtham89 138 on at
    RE: How do I Autopopulate a field based on the lookup selection?

    Hello, Thank you so much for the step by step explanation. I will try this out and will keep you posted once it is sorted out.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do I Autopopulate a field based on the lookup selection?

    Hi Gowtham89,

    You can use following js code into lookup field onChange event in form properties.

    function getLookupName(executionContext) {
        var formContext = executionContext.getFormContext();
        var lookupFieldObject = formContext.getAttribute("new_existingclient"); 
        if (lookupFieldObject.getValue() != null) {
            var entityName =  lookupFieldObject.getValue()[0].name;
        }
        var company = formContext.getAttribute("new_company");
        company.setValue(entityName);
    }
    

    Go Settings > Customizations > Customize the system > Web Resources to add new js resource.

    7587.pastedimage1630979968367v3.png

    Then expand Entities to open one form of the entity you need

    5305.pastedimage1630980210850v6.png

    Click 'Form Properties' to open dialog to add resource you just created.

    8475.pastedimage1630980127999v5.png

    2021.pastedimage1630980097907v4.png

    Save and Publish all customizations.

    Test:

    Changing lookup field value without clicking save button, company field has been populated with lookup field name. 

    0728.pastedimage1630980288784v7.png

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans