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
andyaldis1.3969086903835708E12andyaldis1.3969086903835708E12 

Batch update archived Tasks and Events

I wrote several batch classes to update fields on tasks and events with information with several custom objects.  Originally I wrote the classes to only update those tasks and events which were not archived, but recently the decision was made to update all tasks and events including archived tasks and events.  My original query to update the tasks and events was simple 'Select ID from Event'  which pulled all non archived events, can I rewrite my query to 'select Id from Evetn where is Archived = true or isArchived = false to pull both types?  An example of one of the batch classes is below.

global class ActivitySidecarRefactorEventCampaign implements Database.Batchable<sObject> {

     global Id primaryCampaign {get; set;}
     global Id campaign2 {get; set;}
     global Id campaign3 {get; set;}
     global Id campaign4 {get; set;}
     global Id campaign5 {get; set;}
     global string activityId {get; set;}
     global Id eventId {get; set;}
     Global Boolean insertList {get; set;}
      global Boolean activityRelation = false;
     global String query = 'select Id from Event Where IsArchived = true or IsArchived = false';

        global Database.QueryLocator start(Database.BatchableContext BC) {
            return Database.getQueryLocator(query);
        }

           global void execute(Database.BatchableContext BC, List<event> scope) {
                System.Debug('!! Batch size for event '+scope.size());
                    List<event> eventList = New List<event>();
                    List<Activity_Relation__c> activityRelationList =  New List<Activity_Relation__c>();
                    for(event t: Scope){
                    List<Activity_Campaign__c> activityCampaign = [Select Campaign__c, Activity_Id__c, Activity_Sidecar__r.Primary_Campaign__c
                                                                                                                From Activity_Campaign__c
                                                                                                                Where Activity_Sidecar__r.Is_Task__c = false AND Activity_Id__c = :t.Id order by createdDate asc Limit 6];
                    System.debug('$$ activityCampaign is '+activityCampaign.size());
                    IF(activityCampaign.Size() > 0){
              insertList = true;
                        eventId = activityCampaign[0].Activity_Id__c;
                        activityId = activityCampaign[0].Activity_Id__c;
                        IF(activityCampaign[0].Activity_Sidecar__r.Primary_Campaign__c == Null){
                            primaryCampaign = activityCampaign[0].Campaign__c;
                                IF(activityCampaign.Size() > 1){
                                    activityRelation = true;
                                    campaign2 = activityCampaign[1].Campaign__c;}
                                    IF(activityCampaign.Size() > 2){campaign3 = activityCampaign[2].Campaign__c;}
                                            IF(activityCampaign.Size() > 3){campaign4 = activityCampaign[3].Campaign__c;}
                                                    IF(activityCampaign.Size() > 4){campaign5 = activityCampaign[4].Campaign__c;}
                      }else{
                primaryCampaign = activityCampaign[0].Activity_Sidecar__r.Primary_Campaign__c;
                            IF(activityCampaign.Size() > 1){
                                    activityRelation = true;
                                    campaign2 = activityCampaign[0].Campaign__c;}
                                IF(activityCampaign.Size() > 2){campaign3 = activityCampaign[1].Campaign__c;}
                                        IF(activityCampaign.Size() > 3){campaign4 = activityCampaign[2].Campaign__c;}
                                                IF(activityCampaign.Size() > 4){campaign5 = activityCampaign[3].Campaign__c;}
                    }
                    System.Debug('@@ Activity Campaign fields are '+primaryCampaign+' '+campaign2+' '+ campaign3+' '+ campaign4+' '+ campaign5);
                    IF(insertList == true){
              event ev = new event();
                      ev.Id = eventId;
                      ev.Primary_Campaign__c = primaryCampaign;
                      eventList.add(ev);
              System.debug(eventList);
                IF(activityRelation == True){
                        Activity_Relation__c ar = new Activity_Relation__c();
                        ar.Activity_Id__c = eventId;
                      ar.Campaign_2__c = campaign2;
                      ar.Campaign_3__c = campaign3;
                      ar.Campaign_4__c = campaign4;
                      ar.Campaign_5__c = campaign5;
                      activityRelationList.add(ar);
              system.debug(activityRelationList);}
          }
                }
                }
                System.debug('## event List size is '+eventList.size());
                System.debug('## activity relation List size is '+activityRelationList.size());
                try {
            system.debug('$$ Inserting Activity Relations record - try statement');
                    insert activityRelationList;
            System.debug('$$ updating event list - try statement');
            update eventList;
                } Catch(DmlException d){
                    system.debug('^^ DML Exception is '+d.getMessage());
                for (Integer i = 0; i < d.getNumDml(); i++) {
           System.debug('^^ DML Exception Message is '+d.getDmlMessage(i));
               System.debug('^^ DML Exception row is '+d.getDmlIndex(i));
                 System.debug('^^ DML Exception Id is '+d.getDmlId(i));
                 System.debug('^^ DML Exception Index is '+d.getDmlIndex(i));
                 System.debug('^^ DML Excetion Field name is '+d.getDmlFieldNames(i));
                 System.debug('^^ DML Exception Id is '+d.getDmlFields(i));
                 System.debug('^^ DML Exception status is '+d.getDmlStatusCode(i));
                 System.debug('^^ DML Excetion type is '+d.getDmlType(i));
         }
                } finally {
            System.Debug('&& updating event list - finally statement');
            Database.update(eventList, false);
            system.debug('&& updating activity relatiion list - finally statement');
                Database.insert(activityRelationList, false);


                }{

                }
        }

        global void finish(Database.BatchableContext BC) {
        AsyncApexJob a = [Select Id, Status, ExtendedStatus, NumberOfErrors, JobItemsProcessed, ApexClassId, CompletedDate, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()];
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {'aaldis@test.com'};
                mail.setToAddresses(toAddresses);
                mail.setSubject('Batch ActivitySidecareRefactorCampaign '+a.ApexClassId+' is ' + a.Status);
                mail.setPlainTextBody('Batch ActivitySidecareRefactorCampaign '+a.ApexClassId+' is ' + a.Status+' at '+a.CompletedDate+'.  There were '+ a.TotalJobItems +' records processed with '+ a.NumberOfErrors + ' failures.');
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                            Database.executeBatch(new ActivitySidecarRefactorTaskProdUpdate(), 32);




        }