function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jasjeet GrewalJasjeet 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.
 
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;
    }
}

 
CharuDuttCharuDutt
Hii Jassjeet
Try Below Code
public without sharing class AccountSearch { 
    @AuraEnabled(cacheable=true) 
    public static list<Account> getAccountDetails(String accountId){
        string userId = UserInfo.getUserId();
        system.debug('userId====>'+ userId);

        list<Account> accountDetails = [Select id, name, recordtypeid, type FROM Account where Id = accountId && OwnerId = :userId];

        return accountDetails ;
}
}
Please Mark It As Best Answer If It Helps
Thank You!

 
ravi soniravi soni
hi,
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
Jasjeet GrewalJasjeet Grewal
@charu No that does not work
@veer Yes, I am getting this.accId
//This wire method is not working

@wire (getAccountDetails, {accountId: "$accId"}) wiredAccounts({data, error}){
  if(data) {
    this.accounts =data;
    this.errors = undefined;
    console.log("data "+data);
  }else {
    this.accounts =undefined;
    this.errors = error;
    console.log("error - "+error);
    console.log("acc - "+accID);
  }
}

 
KdKomalKdKomal
Hi Jaspreet,

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.