3

I have a custom object Relationship_Owner__C with a lookup relationship to Contacts. I need to query all the contacts that belong to the relationship owner, and all the tasks assigned to the contact in order to get a count of unique subject lines.

When I tried to break it into two queries I got a SOQL 101 error. But now I seem to be stuck on making it work at all. Any help would be great.

trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {

    LIST<Task> taskList =[SELECT ID,
                             (SELECT Id,
                              Subject
                              FROM Tasks)
                            FROM Contact
                            WHERE New_Relationship_owner1__c IN :Trigger.New];
    system.debug('Influencers found =:' + taskList.size());
   //If the list size is greater than 0 get the tasks assigned to the influencers 
If(taskList.size() !=0){
    String subject = 'none';
    Integer uniqueCount = 0;

New Code

trigger CountUniqueEmails on relationship_owner__c (after insert, after update){ Map contactMap = new Map([SELECT Id, New_Relationship_owner1__c FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List taskList = [SELECT Id, WhoId,Subject FROM Task WHERE WhoId IN :contactMap.keySet()];

Map> subjectLineMap = new Map>();

for(Task t: taskList) {
    Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;

    if(!subjectLIneMap.containsKey(relationshipOwnerId)){
        subjectLineMap.put(relationshipOwnerId, new Set<String>());
    }
    subjectLineMap.get(relationshipOwnerId).add(t.Subject);
    system.debug('unique subject count=' + subjectLineMap.size());
}

}

2 Answers 2

6

Direct Answer

Your query's result type will be a List<sObject>, where sObject is the primary object in the query. In this case,

SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact...

the primary object is Contact. For that reason, the return value will be List<Contact>.

Note also that you cannot use Trigger.new as a bind variable for an Id query. You can use Trigger.newMap.keySet(), because it's the right data type:

SELECT ID, (SELECT Id, Subject FROM Tasks) FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()

Another Approach

If what you want, though, is the unique subject lines of Tasks attached to Contacts which have specific relationship owners, but you do not need them grouped by the individual Contact, you can break the query into two to go straight to the Task level. It would look something like this:

Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id FROM Contact WHERE New_Relationship_owner1__c IN :Trigger.newMap.keySet()]);
List<Task> taskList = [SELECT Id, WhoId, Subject FROM Task WHERE WhoId IN :contactMap.keySet()];
Map<Id, Set> subjectLineMap = new Map<Id, Set>();

Then you can iterate over those Tasks:

for (Task t: taskList) {
    Id relationshipOwnerId = contactMap.get(t.WhoId).New_Relationship_owner1__c;

    if (!subjectLineMap.containsKey(relationshipOwnerId)) {
        subjectLineMap.put(relationshipOwnerId, new Set<String>());
    }

    subjectLineMap.get(relationshipOwnerId).add(t.Subject);
}

Then your subjectLineMap ends up with one Set of the unique Subject lines, across all contacts, for each Relationship Owner. You can inspect the size of the set for an owner's Id to see how many unique subject lines there were (note that it's case sensitive).

0
2

you are querying FROM Contact so the list must be of type list'(Contact)'..

if you want to check to see if any tasks are related to a contact you need to iterate over the list of contacts and see if tasks exists.

for( Contact con : [SELECT ID,
                         (SELECT Id,
                          Subject
                          FROM Tasks)
                        FROM Contact
                        WHERE New_Relationship_owner1__c IN :Trigger.New])
{
if ( con.tasks.isEmpty() ) 
{
// no tasks assigned to contact
}
else
{
// tasks exists for this contact
}
} 

You must log in to answer this question.

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