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
Alvin TanAlvin Tan 

Get Apex Object Reference Id Values

Is there a way for me to get reference fields name or values easily when I have an instance of an object. For example, Case has the field contact name. In apex when I access this field it is the id of a contact rather than the name. Is there an efficient way for me to get values, specifically name, out of the Contact that belongs to that id without trying having to query the whole contact object and any other reference objects that I may need? 

Thanks!
Anthony McDougaldAnthony McDougald
Hello Alvin,
Hope that your day is off to an amazing start. What you're asking for requires a SOQL query but it doesn't require the return of the entire database. I would use the below query to return the name of a contact associated to the Case record I am using if I was in your position. Please let us know if this helps and have a blessed day.
\\APEX CLASS
Case c = new Case();
insert c;
c = [SELECT Contact.Name FROM Case WHERE Id = :c.Id];

\\IF THIS IS FROM AN APEX TRIGGER, USE THE BELOW INSTEAD
Case c = [SELECT Contact.Name FROM Case WHERE Is = :Trigger.new];

\\Either way, the relationship field you're looking for is the Contact.Name field for the Case object


Best Regards,
Anthony McDougald
Alvin TanAlvin Tan
Hi Anthony,

Thanks for the response! Based off your response, I'm assuming there's no simpler function calls or forms of dot walking that can get me to roughly equivalent to that SOQL query? Thanks!
dk alivedk alive