0

I have a Batch Apex which is invoked from process builder. I can see Ids are coming into the variable (please see debug log) but same variable inside the query gives "Variable does not exist" error. I am not sure what am i doing wrong here??

public class CaseSetOwnerBatch implements Database.Batchable<sObject> {    
List<String> csIds;

    public CaseSetOwnerBatch(List<String> csIds) {
        this.csIds = csIds;
    }

    @InvocableMethod(label='Set Owner to Parent Case Owner' description='This method will be execute CaseSetOwnerBatch')
    public static void executeBatchMethod(List<Id> caseIds) {
        System.debug('Case Ids: -- -- '+caseIds);
        CaseSetOwnerBatch setOwner = new CaseSetOwnerBatch(caseIds);
        Database.executeBatch(setOwner,1);
    }


    public static Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id, OwnerId, LastModifiedById FROM Case WHERE Id IN :csIds';
        return Database.getQueryLocator(query);
    }
public void execute(Database.BatchableContext BC, List<Case> csList) {  
        for (Case c : csList) {
            c.OwnerId = c.LastModifiedById;
        }
        TriggerHandler.bypass('CaseTriggerHandler');
        Database.update(csList, false);
    }   
    public void finish(Database.BatchableContext BC) {
    }
}

enter image description here

enter image description here

  • how is the surrounding class defined? (use edit to amend the code – cropredy Nov 14 at 19:18
  • @cropredy I added the complete class. But i moved InvocableMethod to a different class now. It seems to be working that way. – SunnyG Nov 16 at 18:17
1

Remove static from the method signature. It will work as expected.

Go through batch apex document: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

  • Invocable Method Considerations: The invocable method must be static and public or global, and its class must be an outer class – SunnyG Nov 14 at 11:38
  • @SunnyG, but you don't need start method to be static. Add your invocable method to some other class and execute the batch from that. – Arabinda Nov 15 at 0:05
  • Hi Arabinda. Yes, that's what i ended up doing. Thank you! – SunnyG Nov 16 at 17:37

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.