Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

[JS] Resolving account lookupfield from a contact lookupfield

Posted on by Microsoft Employee

Hi,

when a user selects a contact, the related account should be set in the corresponding account lookup.

Everything works fine, but when i save the record, the contact field value is getting deleted.

Any suggestions?

    function setRelatedAccount() {

        try {

            var check = Xrm.Page.getAttribute(incident_account).getValue();

            if (check != null) {

                console.log("Account already set!");

            }

            else {

                var contact_field = Xrm.Page.getAttribute(incident_contact);

                if (contact_field != null) {

                    var contact_id = Xrm.Page.getAttribute(incident_contact).getValue()[0].id;

                    if (contact_id != null) {

                        contact = Xrm.Page.getAttribute(incident_contact).getValue();

                        retrieveAccountForContact("ContactSet", contact_id); //get the account for a contact

                        Xrm.Page.getAttribute(incident_tmpaccount).setValue(account);

                        Xrm.Page.getAttribute(incident_tmpcontact).setValue(contact);

                    }

                }

                else {

                    console.log("Contact Field not available!");

                }

            }

        } catch (ex) {

            console.log(ex);

        }

    }

*This post is locked for comments

  • tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: [JS] Resolving account lookupfield from a contact lookupfield

    Hi,

    Just for your information, because I believe that code provided by João will help - the reason it did not work is because you have probably this field disabled and in order to save disabled field you have to set its submit mode to "always"

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: [JS] Resolving account lookupfield from a contact lookupfield

    Hi Jonathan

    You can force the field to be updated when the new value is applied using

    Xrm.Page.getAttribute("field_name").setSubmitMode("always");

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

Featured topics

Product updates

Dynamics 365 release plans