Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get Account from Contact with Javascript using parentcustomerid

(0) ShareShare
ReportReport
Posted on by 120

Hello everyone,

I'm currently writing a script that tries to obtain the account entity connected with a contact. I'm using the command below:

Xrm.WebApi.retrieveRecord("contact", recid2, "?$select=fullname&$expand=parentcustomerid($select=name)").then(
			function success(result) {
				console.log("Retrieved values: Name: " + result.fullname);
			},
			function (error) {
				console.log("Query failed");
			}
If I remove the &$expand=parentcustomerid($select=name) it works normally, and it returns the contact's fullname. But with it, 
the query fails and it returns the "Query failed" message. I've noticed that the field parentcustomerid of the contact entity is not a Lookup field but a Customer Type.
How can I get the account name of the related account of this contact?

*This post is locked for comments

  • Guido Preite Profile Picture
    Guido Preite 54,069 Super User 2024 Season 1 on at
    RE: How to get Account from Contact with Javascript using parentcustomerid

    use CRM REST Builder to create and test your query

    https://github.com/jlattimer/CRMRESTBuilder

  • Giannis Tsak Profile Picture
    Giannis Tsak 120 on at
    RE: How to get Account from Contact with Javascript using parentcustomerid

    Ah, I rushed a bit.

    I do not have the account in the form. What I'm trying to do is to reach the account entity from the the email entity through contact. Email(form)->contact->account.  I can't use the formContext.getControl since the field account is not in the email. Is there any way to retrieve the account entity this way?

  • Giannis Tsak Profile Picture
    Giannis Tsak 120 on at
    RE: How to get Account from Contact with Javascript using parentcustomerid

    Thank you very much, both of you!

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: How to get Account from Contact with Javascript using parentcustomerid

    If you want only name of Account then you will get it in Parent customer id, There is no need to retrieve Account entity.

    Use code that is suggested by Mahadeo.

  • Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at
    RE: How to get Account from Contact with Javascript using parentcustomerid

    Hi..

    You dont need to retrieve account information, if that field is on Form.

    you can use

    var accountLookup = formContext.getControl("parentcustomerid").getAttribute().getValue();

    var acountName = accountLookup[0].name;

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans