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
Bryn JonesBryn Jones 

Has a Contact had an activity ith a certain subject?

I am trying to create a field that says whether a Contact has ever had an Activity with a certain value in the Subject Field of all Activities. 

For example: I want to know if the contact has ever had an Activity logged with the Subject Field "First Contact". So if Mary Doe has been called and the call was logged with the Subject Field as "First Contact" and later was called and the call was logged with the Subjct Field as "Sales Presentaion", on her Contact Page the field would say TRUE as she has had a activity with "First Contact".

But if John Doe was called and the Subject Field as logged as "Sales Presentation", and then called again and the Activity was logged with the Subject Field as "Closing" then the field would be "FALSE" as none of the Activities done with him had the Subject Field as "First Contact"

Thanks.
Bryn
Shailesh DeshpandeShailesh Deshpande
HI Bryn,

You can use a trigger to achieve this.  Below trigger should work for you. You might want to check the old values and new values before performing the check instead of just checking the subject. But this should help you get started.
 
trigger CheckSubject on Task(after insert, after update){

    Set<Id> contactIds = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();
    
    for(Task t: Trigger.new){
        
        // check the subject and if the task/activity is related to Contact(Id always starts with 003)
        if(t.Subject == 'First Contact' && String.valueof(t.WhoId).startswith('003')){
            contactIds.add(t.WhoId);// add the ids to set           
        }
        
    }
    
    // i am assuming your boolean field to be "Contacted__c". Please rename with correct name in query
    for(Contact c: [Select Id, Contacted__cfrom Contact where Id in: contactIds]){
        c.Contacted__c= true;
        contactsToUpdate.add(c);
    }

    update contactsToUpdate;

}

Thanks,
Shailesh.