4

In Lightning web components, is there a way to get the Contact Id of current user without using Apex Server side code?

However, we can easily find the User Id, using below code:

import { LightningElement } from 'lwc';
import Id from '@salesforce/user/Id';

export default class MiscGetUserId extends LightningElement {
    userId = Id;
}

2 Answers 2

6

The simple answer is no as of Winter '20.

You can find out the currently available Salesforce module details in the documentation, which provides an outline of each module and links to the module detail. The user-centric module is explicitly documented here. There is no API for accessing the contact detail without using server-side code and a wire or imperative Apex call, or a wire using standard wire services (as per the answer from user3302919).

2
  • As of API version 60 - is this possible already?
    – compski
    Commented Mar 12 at 13:58
  • I don't see anything in the documentation suggesting it is available in Spring '24.
    – Phil W
    Commented Mar 12 at 14:48
12

This code has worked for me to get the id of the contact user without using APEX. Make sure you try this from a logged-in user.

import { LightningElement, wire } from "lwc";
import { getRecord, getFieldValue } from "lightning/uiRecordApi";
import CONTACT_ID from "@salesforce/schema/User.ContactId";

// this gets you the logged in user
import USER_ID from "@salesforce/user/Id";
export default class LogoAndApplicantName extends LightningElement {

  @wire(getRecord, { recordId: USER_ID, fields: [CONTACT_ID] })
  user;

  get contactId() {
    return getFieldValue(this.user.data, CONTACT_ID);
  }

Then on the HTML:

<span>
      {contactId}    
</span>
1
  • Whilst this doesn't directly include custom Apex, this is still using a wire, and therefore a server round-trip (for uncached values). It isn't a bad solution, though doesn't handle an error when the standard getRecord function from uiRecordApi is invoked - it assumes that this.user.data will exist and contain the contact ID field, which it may not. Instead there could be no data and no error, or an error. So the contactId getter could fail.
    – Phil W
    Commented Feb 16, 2020 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .