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 CRM (Archived)

js to get contact id

(0) ShareShare
ReportReport
Posted on by

Hi ,

I have a account and in that I have contact how should I get contact Id 

*This post is locked for comments

I have the same question (0)
  • joenewstrom Profile Picture
    6 on at

    This question needs more context. Form Script? Plugin? Web Request? Language?

  • Community Member Profile Picture
    on at

    yes from java script

  • Suggested answer
    joenewstrom Profile Picture
    6 on at

    If it is a child record you can use the REST endpoint to query the database. If it is the primary contact you can use the Xrm.Page.Data.Attributes collection that is loaded by the application into memory on page load. I built a bunch of Open Source tooling (freely available at github.com/.../simplexrm) to make JavaScript easy in CRM, so you could write code like this:

    function getChildContactIds (accountId) {

       simpleXrmRest.query({

           entity: "Contact", //entity schema name

           select: "contactid", //comma separated list of attributes to return from server

           filter: "ParentCustomer eq guid'" + accountId + "'",

           callback: function () {

               var results = simpleXrmRest.getRetrieveMultipleResults(this);

               //handle results format [{contactid: "GUID1"}, {contactid: "GUID2"}]

           }

       })

    }

    function getPrimaryContactId () {

       var result = simpleXrm.getLookupID("primarycontactid");

       //handle result formatted as string

    }

    There are other toolsets out there and you should start by spending time with the SDK, but once you dig through it you will find that the simpleXrm toolset saves a lot of time and reduces your code's complexity.

  • Suggested answer
    Community Member Profile Picture
    on at

    Thanks for your reply and great stuff .

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi Suresh,

    As per my understanding in your Account entity you have a lookup Contact.And you want to get that contact Id.If it is your requirement then try following code.

    function getContactId() {

       var ContactId = Xrm.Page.getAttribute("Contactfield logical name").getValue()[0].id;

    alert(ContactId);

               }

    Hope it helps you.

    Thanks,

    Shiva.

  • joenewstrom Profile Picture
    6 on at

    Shiva, be very careful! Your approach will fail under multiple conditions. You have to build in null checks for lookups because they are complex (object nested in an array). If the lookup is empty, you will get an error because CRM.Page.getAttribute("attributename").getValue()[0] does not exist.

    That is one of many reasons why I built the simpleXrm library. I got tired of pasting:

    var results = null;

    if (Xrm.Page.getAttribute(a).getValue() && Xrm.Page.getAttribute(a).getValue()[0] && Xrm.Page.getAttribute(a).getValue()[0].id) {

       results = Xrm.Page.getAttribute(a).getValue()[0].id;

    }

    ...Which is the "safe" way to do it in direct interaction with the SDK.

    I like the compact

    simpleXrm.getLookupID("attributename");

    myself, it is much cleaner and informs future developers of the intent of the code.

  • shivaram Profile Picture
    3,315 on at

    Yes Joe,

    Thanks for your suggestion...

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans