I have called a batch class from a trigger.In debug log start method is getting called.But execute is not getting called.
trigger:
trigger trgInsertRecord on Custom_object__c(after insert,after update)
{
Set<Id> clientIds = new Set<Id>();
for(Custom_object__c c:Trigger.new) {
clientIds.add(c.Client__c);
if(trigger.Isinsert) {
Database.executeBatch(new BatchableClass(clientIds));
}
}
}
batchable class:
global class BatchableClass implements Database.Batchable<sObject> {
Set<Id> clientIds = new Set<Id>();
global BatchableClass(Set<Id> clientIdSet) {
clientIds = clientIdSet;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([Select Id,Client__c from Custom_object__c where Client__c IN:clientIds ]);
}
global void execute(Database.BatchableContext BC, List<Custom_object__c> scope) {
if (scope.isEmpty())
return;
for(Custom_object__c c:scope) {
clientIds.add(c.Client__c);
//some logic to get objlist
insert objlist;
}
}
}
global void finish(Database.BatchableContext BC) {}
}
[Select Id,Client__c from Custom_object__c where Client__c IN:clientIds ]
is returning rows. For more info on debugging see: salesforce.stackexchange.com/questions/36582/…executeBatch
for each loop infor(Custom_object__c c:Trigger.new)
. You should be calling it once after the for loop has completed