2

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) {}
}
  • Try adding debugging to make sure [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/… – BarCotter Aug 14 '14 at 8:56
  • Also your trigger is calling the executeBatch for each loop in for(Custom_object__c c:Trigger.new). You should be calling it once after the for loop has completed – BarCotter Aug 14 '14 at 8:58
  • Please do not edit your question when you have a new question. Instead create a new question using the 'Ask Question' button. – Alex Tennant Aug 14 '14 at 16:25
1

Try changing the signature of the start method to the following. Note how the scope parameter is a List<sObject> rather than a List<Custom_object__c>, giving it the correct Database.Batchable execute method.

public void execute(Database.BatchableContext context, List<sObject> scope) {
    if (scope.isEmpty()) {
        return;
    }

    for(Custom_object__c c : scope) {
        clientIds.add(c.Client__c);      

        //...
    }
}

Then cast the sObjects from the scope to Custom_object__c.

Richard Durrant also had a helpful answer around your trigger that was deleted for some reason. I'll put the details here as you probably can't see the deleted answer.

As your trigger appears in the question, it will never do anything in the after update case due to the if(trigger.Isinsert) condition. So you might want to remove the after update or the IsInsert check.

You should also move the Database.executeBatch(new BatchableClass(clientIds)); out of the for loop on trigger.new. You will be starting one batch per Custom_object__c inserted in the transaction with each subsequent batch having the Ids on all the previous inserted records.

| improve this answer | |
  • I am getting First error: Too many DML rows: 10001 exception now – SalesforceAspiration Aug 14 '14 at 10:53
  • @SalesforceAspiration Is the execute method getting called now with the execute signature change? If you are getting a new error you can open a new question to address that. – Daniel Ballinger Aug 14 '14 at 19:23

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.