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
Brooks Johnson 6Brooks Johnson 6 

Help With Trigger to Count Unique Tasks

trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
    for (Relationship_Owner__c   rOwner : trigger.new){
        
        //get list of on tasks belonging to owner that contain "Pardot"    
    LIST<Task> taskList = [SELECT ID,
                           Subject
                           FROM Task
                           WHERE Subject LIKE 'Pardot'
                          AND WhoId = :rOwner.Id];
        system.debug(taskList.size() + 'Matching tasks found');
        
        //if matches are found        
        if(!tasklist.isEmpty()){
            String pardotSubject = 'none';
            Integer uniqueCount = 0;
            //loop through matches and increment count if unqique
            for( Task t : taskList){
                if( pardotSubject != t.Subject){
                    uniqueCount = uniqueCount +1; 
                    system.debug('Unique count' + uniqueCount);
                }
                rOwner.Unique_Emails_Sent__c = uniqueCount;
                insert rOwner;               
                
            }            
            
        }
    } 
    
    
    //need a list of all tasks that contain pardot that belong to the relationship owner
    //Loop through tasks  to a get a count of unique subjects
    //Update Relationship Owner. 

}
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
    for (Relationship_Owner__c   rOwner : trigger.new){
        
        //get list of on tasks belonging to owner that contain "Pardot"    
    LIST<Task> taskList = [SELECT ID,
                           Subject
                           FROM Task
                           WHERE Subject LIKE 'Pardot'
                          AND WhoId = :rOwner.Id];
        system.debug(taskList.size() + 'Matching tasks found');
        
        //if matches are found        
        if(!tasklist.isEmpty()){
            String pardotSubject = 'none';
            Integer uniqueCount = 0;
            //loop through matches and increment count if unqique
            for( Task t : taskList){
                if( pardotSubject != t.Subject){
                    uniqueCount = uniqueCount +1; 
                    system.debug('Unique count' + uniqueCount);
                }
                rOwner.Unique_Emails_Sent__c = uniqueCount;
                insert rOwner;               
                
            }            
            
        }
    } 
    
    
    //need a list of all tasks that contain pardot that belong to the relationship owner
    //Loop through tasks  to a get a count of unique subjects
    //Update Relationship Owner. 

}

 
Raj VakatiRaj Vakati
Try this 
 
trigger CountUniqueEmails on relationship_owner__c (before insert, before update) {
    for (Relationship_Owner__c   rOwner : trigger.new){
        
        //get list of on tasks belonging to owner that contain "Pardot"    
    LIST<Task> taskList = [SELECT ID,
                           Subject
                           FROM Task
                           WHERE Subject LIKE 'Pardot'
                          AND WhoId = :rOwner.Id];
        system.debug(taskList.size() + 'Matching tasks found');
        
        //if matches are found        
        if(!tasklist.isEmpty()){
            String pardotSubject = 'none';
            Integer uniqueCount = 0;
			Map<String ,List<Integer>> intMap = new new Map<String ,List<Integer>>() ;
            //loop through matches and increment count if unqique
            for( Task t : taskList){
				
                if(intMap.conatinsKet(t.subject)){
					intMap.get(t.subject).add(1);
				}else{
					intMap.put(t.subject , new List<Integer>{1}) ; 
				}
            }  

 for( Task t : taskList){
	  rOwner.Unique_Emails_Sent__c = intMap.get(t.Subject).size();

	 }			
            
        }
    } 
	// Do the manipulation here 
	
	
    
    
    //need a list of all tasks that contain pardot that belong to the relationship owner
    //Loop through tasks  to a get a count of unique subjects
    //Update Relationship Owner. 

}

 
Brooks Johnson 6Brooks Johnson 6
Thank you. I will try it out in a bit. I need to learn how to use maps.
Brooks Johnson 6Brooks Johnson 6
Sorry my last reply never posted. I am getting an error on the 
if(infMap.contains(t.Subject))
Method does not exist or incorrect signature: void contains(String) from the type Map

And I am still stuck on my problem of how to traverse from Relationship Owner__c to Contact to Task.