Activity Forums Salesforce® Discussions How can we call Batch Classes from Triggers in salesforce Reply To: How can we call Batch Classes from Triggers in salesforce

  • sushant

    Member
    January 6, 2017 at 12:55 pm

    Hi,
    In this post i am trying to give an example of how to execute Batch Process from Apex Trigger. Here i am calling Batch class from the trigger.

    Trigger:

    trigger UpdateAreaInfoUser on User (after update) {
    Map<id, User> owners = new Map<id, User>();

    for (Integer i=0;i<Trigger.new.size();i++) {
    if (Trigger.new[i].Team__c!=Trigger.old[i].Team__c) {
    owners.put(Trigger.new[i].Id, Trigger.new[i]);
    }
    }

    // You can execute batch apex using trigger using below codes
    if (owners.size() > 0) {
    Database.executeBatch(new UpdateAccountArea(owners));
    }

    }

    This 'UpdateAccountArea' is a batch class.

    Hope this helps you