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
timomertimomer 

Grab data from a different object - easy question!?

Hi,

 

Im new to apex and just starting to pick it up, so sorry if this is a simple question.

 

I have a custom object on Opportunities, when saving this custom object I need to grab some data from the Opportunity.

 

For example I need to grab the UserName of the owner of the Opportunity, how would I do this? From the custom object I can grab the  Opportunity ID (chat.Oppertunity__c) but not sure what to do with it next? 

 

 

Thanks in advance for your help!

tim

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Query for the related data.

 

Opportunity o = [Select o.Owner.Username, o.OwnerId From Opportunity o where o.Id = :chat.Opportunity__c];

String ownerUsername = o.Owner.Username;

 

All Answers

aalbertaalbert

Query for the related data.

 

Opportunity o = [Select o.Owner.Username, o.OwnerId From Opportunity o where o.Id = :chat.Opportunity__c];

String ownerUsername = o.Owner.Username;

 

This was selected as the best answer
timomertimomer
you are a star! thank you so much :)


was looking for some example query as I thought that would be the way but could not find anything that had a detailed explanation on how it would work.

One quick question...
below you use "o.Owner.Username" that works perfectly, but, in the Eclipse IDE I do not get this listed as an option to select after o. - do I have a missing configured? Or is the IDE limited and this information is listed elsewhere?

many thanks!
tim