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
Nathan PratsNathan Prats 

Queries : How to get Who.Title from Events

Hello, 

I am desperately trying to get the Contact Titles from Events.
I couldn't create an activity custom field to get the Who.Title value.
I couldn't write a query with Who.Title so i'm trying to filter a contact query.
However, the below doesn't work. What am I doing wrong ? 

SELECT Id, Title, (SELECT CreatedDate, WhatId , WhoId FROM Events WHERE CreatedDate = TODAY AND Meeting__c = TRUE)
FROM Contact
WHERE Id IN (SELECT WhoId FROM Event WHERE CreatedDate = TODAY AND Meeting__c = TRUE)

Any help is more than welcome, 

Nathan,
Dhanya NDhanya N
Hi Nathan,

It seems that we cannot access Title, Email or Phone fields on Task.Who or Event.Who. Reference for this : https://developer.salesforce.com/forums/?id=906F00000008wPeIAI

Because the WhoId and WhatId fields of Task/Event are polymorphic (i.e. can point to many different kinds of objects) you can't just query relationships through them like you can for normally-related objects. Instead, you'll have to store whoId and then perform query on Contact based on that whoId.

Sample code:
trigger EventsOnContact on Event (after insert) {

    String contactKey = '003';
    Id contactId;
    for(Event objEvent : trigger.new)
        {
            String strWhoId = String.valueOf(e.whoId).substring(0 , 3);
            if(strWhoId == contactKey)
                {
                    contactId = objEvent.whoId;
                }
        }
    
    if(contactId != null)
        {    
            List<Contact> lstContact = [Select id , Title, (SELECT Id FROM Events WHERE CreatedDate = TODAY AND Meeting__c = TRUE) from Contact where id =: contactId];
            //logic here         
   
        }
}
Let me know if it works.

Thanks,
Dhanya
 
Nathan PratsNathan Prats
Hello Dhanya, 

It sounds like a good idea.
Unfortunately, I won't use this query inside a trigger but in Force.com Explorer to  fill an Excel Spreadsheet. 
Dhanya NDhanya N
Hi Nathan,

If you just want to see the details to fill an Excel Spreadsheet then you can create report of type 'Tasks and Events' and drag the details which you want.