You need to sign in to do that
Don't have an account?
Jasjeet Grewal
Get Account details from logged in user id - LWC
Hi,
As logged in user to LWC, I need to display user's related Account details on LWC page.
User object has standard field AccountId.
Using that AccountId, I need to retrieve the Account details.
But, I am unable to retrieve it.
Here's my code.
As logged in user to LWC, I need to display user's related Account details on LWC page.
User object has standard field AccountId.
Using that AccountId, I need to retrieve the Account details.
But, I am unable to retrieve it.
Here's my code.
import getAccountDetails from "@salesforce/apex/AccountSearch.getAccountDetails"; userId = USER_ID @track accID @track accounts; @track error; @track errors; @wire(getRecord, { recordId: "$userId", fields: [ACCOUNT_ID, ACCOUNT_NAME] }) wireuser({error, data}) { if (error) { this.error = error ; } else if (data) { this.accID = data.fields.AccountId.value; } } //this is not working @wire (getAccountDetails, {accountId: "$accId"}) wiredAccounts({data, error}){ if(data) { this.accounts =data; this.errors = undefined; }else { this.accounts =undefined; this.errors = error; console.log("error - "+error); } }
public without sharing class AccountSearch { @AuraEnabled(cacheable=true) public static Account getAccountDetails(String accountId){ Account accountDetails = [Select id, name, recordtypeid, type FROM Account WHERE id=:accountId]; return accountDetails; } }
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
else if (data) { this.accID = data.fields.AccountId.value;
console.log('this.accID ===> ' + this.accID );
}
try above code and make sure you are getting this.accID.
thank you
@veer Yes, I am getting this.accId
You may want to user "$accID" in your "getAccountDetails" method. Please note JS is case sensitive. In your first wire even though you are setting this.accID to a value, in the "getAccountDetails" wired method, "accId" is being used which would be undefined.
I hope this helps.