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

2
  • how is the surrounding class defined? (use edit to amend the code
    – cropredy
    Commented Nov 14, 2019 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
    Commented Nov 16, 2019 at 18:17

1 Answer 1

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

3
  • Invocable Method Considerations: The invocable method must be static and public or global, and its class must be an outer class
    – SunnyG
    Commented Nov 14, 2019 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
    Commented Nov 15, 2019 at 0:05
  • Hi Arabinda. Yes, that's what i ended up doing. Thank you!
    – SunnyG
    Commented Nov 16, 2019 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .