Notifications
Announcements
No record found.
Hi ,
I have a account and in that I have contact how should I get contact Id
*This post is locked for comments
This question needs more context. Form Script? Plugin? Web Request? Language?
yes from java script
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.
Thanks for your reply and great stuff .
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.
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.
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2