0

Is there any way to pre-populate the current logged in user's contact in a lookup field in communities using LWC

HTML

<lightning-record-edit-form  object-api-name="CustomObj__c"> 
<lightning-input-field  field-name="CustomField__c"  onchange={nameHandler}
 </lightning-input-field>             
</lightning-record-edit-form>

JS

import { LightningElement, wire } from 'lwc';
import getUserInfo from '@salesforce/apex/UserDetails.getUserInfo';
import Id from '@salesforce/user/Id';

export default class LwcUserDetail extends LightningElement {

    @wire(getUserInfo, { userId: Id }) 
    userData;
}

Currently I have implemented this but not sure how to proceed

2
  • Yes. What have you tried so far?
    – sfdcfox
    Jan 13 at 4:42
  • @sfdcfox updated the answer. I am not sure how to proceed
    – code234
    Jan 13 at 5:00
0

You need to call the wired apex code to get the user's contact Id, here is an example code.

@AuraEnabled(cacheable=true)
public static Id getCommUserContactId(Id userId) {
    User u = [SELECT contactId FROM User WHERE id = :userId];
    return u.ContactId;
}
2
  • thanks @rahul also how can I also display the associated account related to the community user
    – code234
    Jan 13 at 8:47
  • @code234 You can query the related fields like Contact.AccountId in the same query. Jan 13 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.