+ Start a Discussion
AbhiroopAbhiroop 

Retrieving the name of account owner

Hi everyone;

 

How can I access (retrieve)  the name of the account owner using the name of the account. 

 

I found that account is in look up relationship with User , So running this query results in an error

 

 a) String acctname= [select Owner from account where name=: objAccount.Name];

 

 

Error: MyController2 Compile Error: No such column 'Owner' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 22

 

 

 

b) String acctname= [select a.$User.FirstName from account a where a.name=: objAccount.Name];

 

Error: MyController2 Compile Error: Didn't understand relationship '$User' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 22

 

Thanx in advance !!

itsawsitsaws

Hi abhiroop,

 

Please try below query :

Account acc = [Select Id,ownerid from account where name = 'AllisWell'];
User userobj = [select id,Name from user where id = : acc.ownerid];
System.Debug('>>>>>>>>>>>>>>>>>>>>>>.' + userobj.Name);

 

let me know whether you get your solution or not.

 

thanks

itsaws

Sean TanSean Tan

You need to pull the appropriate field from the Owner. The Owner is not a field in of itself, it is the join reference to the User table.

 

Try this:

 

Account a = [ SELECT Id, Owner.Name FROM Account WHERE Name = :objAccount.Name ];
System.debug(a.Owner.Name);

 

Bhawani SharmaBhawani Sharma

Sean's query is the correct one, just a minor change

List<Account> accs = [Select Id, Owner.Name from Account where Name =: accountName limit 1];

 

if(accs.size() > 0) {

  System.debug(accs[0].Owner.Name);

 

  //do processing

}

 

Having a list for SOQL query helps you to avoid the "No row for assignment for Sobject" exception.