4

My objective is to pull all emails that have a contact or lead as outbound.

As a result, I have the following three queries:

Q1 = "select ID,leadsource from Lead where LeadSource LIKE '%Outbound%'"
Q2 = "select ID,leadsource from Contact where LeadSource LIKE '%Outbound%''"
Q3 = "Select ActivityDate,Owner.Name,Type,Subject,who.email,whoID,ID from Task where Type LIKE '%Email%' AND IsDeleted = False"

Currently, I'm querying Q3 and then filtering the resulting lists for only records that have the 'whoID' within the lists of Q1 and Q2?

Is there a way to combine these three queries?

I've tried the following:

"select Count(ID) from Task where IsDeleted = False AND Type != 'Call - Unscheduled' AND who.ID IN (Select ID From Contact WHERE Lead_Source_Type__c = 'Outbound')"

But I get the following error:

'MALFORMED_QUERY' "MALFORMED_QUERY: Type != 'Call - Unscheduled' AND who.ID IN (Select ID From Contact ^ ERROR at Row:1:Column:89 The left operand 'who.ID' cannot have more than one level of relationships"

2 Answers 2

1

The problem with your query is in the WHO.ID statement. Change WHO.ID to WHOID.

I tested the following in my sandbox and got a list of tasks.

for(task t: database.query('select id from task where whoid in (select id from contact)')){
      system.debug('***** This is the task ' + t.id);
    }

You'll have to enter your parameters to get the results you want but it compiles and returns results.

0

I am afraid you might not be able to join all the three queries. But you can achieve your requirement by using below code,

Set<Id> setId = new Set<Id>();

//Get all lead ids u need
for(Lead lead:[select ID,leadsource from Lead where LeadSource LIKE '%Outbound%']){
   setId.add(lead.id);
}

//Get all contact ids
for(Contact contact: [select ID,leadsource from Contact where LeadSource LIKE '%Outbound%']){
   setId.add(lead.id); 
}

List<Task> lstTask = [Select ActivityDate,Owner.Name,Type,Subject,who.email,whoID,ID from Task where Type LIKE '%Email%' AND IsDeleted = False and whoid in :setId];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .