0

I have below schedulable batch class. I need to call this from an apex trigger on Compliance_Incidents_abv__c. Can someone let me know how can I do that?

Batch Class:

global class ComplianceIncidents_expireNotify implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
    Date d= Date.today().addDays(-14);
    String soql = 'SELECT Id, Name, Publish_Date_abv__c FROM Compliance_Incident_abv__c WHERE Publish_Date_abv__c >=: d';
    return Database.getQueryLocator(soql);
}

global void execute(Database.BatchableContext bc, List<Compliance_Incident_abv__c > recs) {
    List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();

     Map<Id,Compliance_Incident_abv__c > OneMap= new Map<Id,Compliance_Incident_abv__c>();
     Map<Id,string> RMMap= new Map<Id,string>();
    for (Compliance_Incident_abv__c myContact : [select ID,ManagerEmail_abv__c,Name,RepEmail_abv__c,Rep_abv__r.Email,Rep_abv__c,Rep_abv__r.Name, Rep_abv__r.ManagerId,Rep_abv__r.Manager.Name,Rep_abv__r.Manager.ManagerID,Rep_abv__r.Manager.Manager.Email from Compliance_Incident_abv__c  where ID in:recs]) {

    OneMap.put(mycontact.id , mycontact);
    RMMap.put(mycontact.id , mycontact.Rep_abv__r.Manager.Manager.Email);
    system.debug('OneMap@'+OneMap);

    }
    for(Compliance_Incident_abv__c mycontact: [SELECT Id, Name, Publish_Date_abv__c FROM Compliance_Incident_abv__c WHERE Id in:recs]) {
        List<String> toAddresses = new List<String>(); 
        List<String> ccTo= new List<String>();           
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        toAddresses.add(OneMap.get(mycontact.id).ManagerEmail_abv__c);
        mail.setToAddresses(toAddresses);
        //ccTo.add(RMMap.get(mycontact.ID));
        ccTo.add(RMMap.get('saikat.neogy@cognizant.com'));
        mail.setCcAddresses(ccTo);
        mail.setSubject('Action Required: REMINDER ABS incidents in iREP due  TODAY');
        String messageBody = '<br><br>Dear '+ OneMap.get(mycontact.id).Rep_abv__r.Manager.Name;
        messageBody +='<br><br> What you need to know:';
        messageBody +='<br>You have pending ABS Incidents assigned to you in iREP that are due today. Per policy, these must be closed within 14 days of publication.';
        messageBody +='<br>What you need to do:';
        messageBody +='<br>•       Review the incidents';
        messageBody +='<br>•       Coach your representative';
        messageBody +='<br>•       Attest to the incident, submit, and sync iREP today ';
        messageBody +='<br>If you need assistance, please call the Field Help Desk at 1-800-344-6776.';

        mail.setHtmlBody(messageBody); 
        mailList.add(mail);          
    } 
    Messaging.sendEmail(mailList);
}

global void finish(Database.BatchableContext bc) {
}
}
1

Below are the few posts which you can refer to do the same:

http://sfdcsrini.blogspot.com/2014/06/how-to-execute-batch-apex-using-apex.html

http://theblogreaders.com/how-to-execute-batch-apex-using-apex-trigger/#.VfEvhxHBwXA

http://www.cloudforce4u.com/2013/11/call-batch-apex-from-trigger.html

There are few limitations too which have been mentioned in the below link which you can refer:

Is it advisable to call Batch from trigger

Regards! Ruchi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.