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
mavsmavs 

relationship queries

In Apex, how to query contact fields in the same query?

 

Event ev=[Select who.email,who.title from event where Id='xxxxx']; ?

 

If we can't do this way, can someone please advice how i can grab the contact Object values in soql query by quering Event Object?

Message Edited by mavs on 02-26-2010 12:42 PM
Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

I thought I knew the answer but turns out I was wrong.  This does NOT work due to the fact that WhoId can be either from Lead or Contact (ignore my query in a loop):

 

 

trigger doStuff on Event (before insert) { for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId.)EndsWith('003')){ Event eDetails=[SELECT Event.Contact.Title FROM Event WHERE WhoId=:e.WhoId]; system.debug('Contact Title: '+eDetails.Contact.Title); } } }

 

Use this method instead.  It's more bulkified than the above too:

 

 

trigger doStuff on Event (before insert) { List<Id> cIds=new List<id>(); Map<Id,Contact> contactIdToContact=new Map<Id,Contact>(); for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId).EndsWith('003')){ cIds.add(e.WhoId); } } List<Contact> contacts=[Select Title, Name FROM Contact WHERE Id IN:cIds]; for (Contact c: contacts){ contactIdToContact.put(c.Id,c); } for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId).EndsWith('003')){ system.debug('Contact Name: '+contactIdToContact.get(e.WhoId).Name); system.debug('Contact Title: '+contactIdToContact.get(e.WhoId).Title); } } }

 

 

 

 

 That means you have to do a separate query for contact details not inline w/ the Event details:

 

 

 

Related, here's a great guide to SOQL:

http://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/

All Answers

jkucerajkucera

I thought I knew the answer but turns out I was wrong.  This does NOT work due to the fact that WhoId can be either from Lead or Contact (ignore my query in a loop):

 

 

trigger doStuff on Event (before insert) { for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId.)EndsWith('003')){ Event eDetails=[SELECT Event.Contact.Title FROM Event WHERE WhoId=:e.WhoId]; system.debug('Contact Title: '+eDetails.Contact.Title); } } }

 

Use this method instead.  It's more bulkified than the above too:

 

 

trigger doStuff on Event (before insert) { List<Id> cIds=new List<id>(); Map<Id,Contact> contactIdToContact=new Map<Id,Contact>(); for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId).EndsWith('003')){ cIds.add(e.WhoId); } } List<Contact> contacts=[Select Title, Name FROM Contact WHERE Id IN:cIds]; for (Contact c: contacts){ contactIdToContact.put(c.Id,c); } for (Event e:Trigger.new){ //check if the whoId is a contact if (String.ValueOf(e.WhoId).EndsWith('003')){ system.debug('Contact Name: '+contactIdToContact.get(e.WhoId).Name); system.debug('Contact Title: '+contactIdToContact.get(e.WhoId).Title); } } }

 

 

 

 

 That means you have to do a separate query for contact details not inline w/ the Event details:

 

 

 

Related, here's a great guide to SOQL:

http://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/

This was selected as the best answer
thecrmninjathecrmninja

Hey John, I was able to query the Who.ID field successfully in a trigger and only execute it for the Lead object.  Of course, I'm a bit of a newbie and I'm concerned that maybe I've missed something.  What do you (or others) think?

 

 

trigger LeadStating on Task (after insert)

{

//List for recieiving WhoID values of tasks triggering trigger
List<ID> leadStatsIDs = New List<ID>();

//String value that will recieve whoid to ensure trigger only fires for Tasks related to Leads
Public String LeadURL;

//Run through Tasks in trigger, for those associated to Lead, add to above List
for (Task t:System.Trigger.new)
{if (t.subject.contains('Call')&&t.whoid<>Null) {
LeadURL = String.valueof(t.whoid);
if(LeadURL.startsWith('00Q'))
leadStatsIDs.add( t.whoId ) ;
}

//....call the list to a map and do other things that matter in my org
}}

 

 The only difference I see is that I am working a 'startsWith' angle while you were using a the 'endswith' one (my glance over your approach didn't make it clear how you would get endswith to work). 

 

Again, I'm a novice and I'm more concerned if I need to go back to work on my trigger.

 

Message Edited by thecrmninja on 03-01-2010 11:46 AM