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
RahulRahul 

Hi Friends Can come one help me with this code.

I have picklist  filed named boolean  on contact object. which should be set to true if  whe subject of the task is 'Mass Email'. There are 2000+ emails sent. for contacts and their subjct is mass Email which is created in the Activity history. Now i need to find them and update the boolean value to True  where the subject is mass Email. I tried with the Following code but not working.

trigger toupdateboolean on task(before insert){
set<id> setid = new set<id>();
for(task tsk : trigger.new){
setid.add(tsk.whoid);
}
list<contact> lstcontact = new list<contact>();
for(Contact con :[select id,boolean__C,(select subject from tasks where subject='Mass Email') from contact where id=:setid]){
Contact cont = new contact();
cont.id=con.id;
cont.boolean__c='True';
lstcontact.add(cont);
}

update lstcontact;

}

but it Doesnot work. Can someone please help me?
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Sumit,

I would recommend having an After Trigger since the update is happening on another object. Hope you find below trigger helpful. It works on all different conditions such as insert, update, delete, undelete etc.. I have also tested it on my developer org.

Trigger Code:
 
Trigger TaskMassEmailCheck on Task (After Insert, After Update, After Delete, After UnDelete) {
	
    List<Id> contactIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Task tsk : Trigger.New){
            If(tsk.WhoId != NULL && tsk.WhoId.getSObjectType() == Contact.getSObjectType()){
                contactIds.add(tsk.WhoId);
            }
        }
    }
    
    If(Trigger.IsDelete){
        For(Task tsk : Trigger.Old){
            If(tsk.WhoId != NULL && tsk.WhoId.getSObjectType() == Contact.getSObjectType()){
                contactIds.add(tsk.WhoId);
            }
        }
    }
    
    
    List<Contact> conFinalListToUpdate = New List<Contact>();
    
    For(Contact EveryCon :[Select Id, Boolean__c, (Select Id, Subject FROM Tasks) FROM Contact WHERE Id =: contactIds])
    {
        For(Task EveryTask : EveryCon.Tasks)
        {
            If(EveryTask.subject == 'Mass Email')
            {
                EveryCon.Boolean__c = 'TRUE';
                break;
            }
            else{
                EveryCon.Boolean__c = 'FALSE';
            }
        }
        
        conFinalListToUpdate.add(EveryCon);
    }
    
    try{
        If(!conFinalListToUpdate.isEmpty()){
            update conFinalListToUpdate;
        }
    }
    Catch(Exception e){
        system.debug('Thrown Exception For TaskMassEmailCheck Is:  ' + e.getMessage());
    }
}

Here is a visual representation of Trigger:

User-added image

Once you create a task record with subject as Mass Email then it will populate TRUE on Boolean__c field.

User-added image



If this question solves the query then please mark it as Best Answer!
RahulRahul
Hi Harshil, Thanks for your Reply, this code is not working for the old Activity history Records, Where the 2000+ Email is already sent, There also the boolean__c value should be updated as true in the Contact.
HARSHIL U PARIKHHARSHIL U PARIKH
For the existing records you need to update them in order for trigger to work. There is no other work around to it.

There are two ways you can accomplish this update:

1) Via Data loader: (This is an EASY approach)
Export all existing tasks with ID only and them update them via data loader.

2) Via Anonymous Code Block in Developer Console :
- Create a field on Task object named Year_Created__c with formula as TEXT(YEAR( DATEVALUE(CreatedDate) ))
- Go to Developer Console - Debug - Open Execute Anonymous Window and the run the following code:
 
List<Task> alltasks = [Select ID FROM Task WHERE Year_Created__c = '2018' LIMIT 10000];

// List<Task> alltasks = [Select ID FROM Task WHERE Year_Created__c = '2018' LIMIT 10000];

// List<Task> alltasks = [Select ID FROM Task WHERE Year_Created__c = '2017' LIMIT 10000];

//List<Task> alltasks = [Select ID FROM Task WHERE Year_Created__c = '2016' LIMIT 10000];

// +++ however many years you have.

update allTasks;

Keep changing the year to 2017, 2017, 2016... upto however many you have. Once the task is updated, the trigger will fire and do the magic.

Hope this helps!