+ Start a Discussion
Vivek KidambiVivek Kidambi 

CreatedBy.Name not returning the name but returns ID

The following query returns createdbyid/LastModifiedbyId instead of name in apex whereas returns name when using workbench or query editor in dev console. How do i get the name directly instead of ID?  

select Customer_Number__c,Name,Quality_Rating__c,Primary_Institution_Type__c,CreatedBy.Name,CreatedDate,LastModifiedBy.Name,LastModifiedDate from Account WHERE ID =: myID 
Best Answer chosen by Vivek Kidambi
Hi there,

If you are using a debug statemnet to view the output, then you will only see the Ids, however, you will be able to reference the actual names. For example, if you had something like this
List<Account> acc = [Select id, createdby.Name, lastmodifiedby.name from Account limit 1];
for(Account a:acc){
	system.debug('### created by = ' + a.createdby.name);
	system.debug('### lastmodified by = ' + a.lastmodifiedby.name);
The debug statemnets would correctly display the User's name and not the Id.

Please remember to mark this thread as solved with the answer that best helps you.

Kind Regards