Skip to main content
added 97 characters in body
Source Link
Jarvis
  • 2.1k
  • 3
  • 26
  • 47

How can I reduce the scope size so that less number of batches are processed? Someone from the client team doesn't want the unrelated attachments to be returned in the scope

How can I reduce the scope size so that less number of batches are processed?

How can I reduce the scope size so that less number of batches are processed? Someone from the client team doesn't want the unrelated attachments to be returned in the scope

formatting
Source Link
Jarvis
  • 2.1k
  • 3
  • 26
  • 47

In the start method I'm receiving 12k attachments which are EmailMessage records but not related to EmailMessageComm__c records wherei.e.( RelatedToID is not of SobjectType Comm__c)

In the start method I'm receiving 12k attachments which are not related to EmailMessage records where RelatedToID is Comm__c

In the start method I'm receiving 12k attachments which are EmailMessage records but not related to Comm__c i.e.( RelatedToID is not of SobjectType Comm__c)

Source Link
Jarvis
  • 2.1k
  • 3
  • 26
  • 47

How to reduce the size of scope of this batch class

Due to a platform limitation that trigger on attachment won't be invoked when an attachment is sent via email I wrote a batch class to clone and parent email attachments from EmailMessage to a custom object Comm__c

global class BatchforOutboundEmailAttachments implements Database.Batchable<sObject>, Schedulable {    

    global Database.QueryLocator start(Database.BatchableContext bc){

        return Database.getQueryLocator([SELECT Name, Body, ParentID from Attachment Where Parent.Type = 'EmailMessage']);    
    }    

    global void execute(Database.BatchableContext bc, Attachment[] scope){

        set<id> parents = new set<id>();
        List<Attachment> newFiles = new List<Attachment>();
        Map<Id,EmailMessage> emailMap = new Map<Id,EmailMessage>();
        List<Attachment> oldFiles = new List<Attachment>();    

            for(Attachment att : scope){
                parents.add(att.ParentID);
            }

            if(!parents.isEmpty()){

       emailMap = new Map<ID, EmailMessage>([SELECT RelatedToId FROM EmailMessage WHERE Id in :Parents]); 

            for (Attachment att : scope) {
              if(emailMap.get(att.ParentId) != Null && emailMap.get(att.ParentId).RelatedToId != NULL){
                if (emailMap.get(att.ParentId).RelatedToId.getSObjectType() == Comm__c.SObjectType) {

                    Attachment newFile = att.clone();
                    newFile.ParentId = emailMap.get(att.ParentId).RelatedToId;
                    newFiles.add(newFile);
                    oldFiles.add(att);

                }
             }
            }     


       try{
            insert newFiles;
            delete oldFiles;
          }

           catch(DMLexception de){}    
        }

    }

    global void finish(Database.BatchableContext bc){
     }

    global void execute(SchedulableContext sc){

         ID jobId = Database.executeBatch(this,100); 

        }
}

In the start method I'm receiving 12k attachments which are not related to EmailMessage records where RelatedToID is Comm__c

How can I reduce the scope size so that less number of batches are processed?