Can I pass IDs from apex controller to apex batch class?

Can someone please show me some examples? Can't find in google search. I need just a reverse of below link. https://developer.salesforce.com/forums/?id=906F000000096KeIAI


For example, you have:

  1. Base (Apex Class) and a variable storeID (where you stored the ID/s)
  2. Group (Batch Class) and a variable passedID


inside the method calling the batch

Group grp = new Group(storeID);


public Group(*variable type* storeID)
passedID = storeID;

You can create variable in you batch class something like " global List<Id> currentId ;" and from your main class set the value of this variable using class instance and then use this variable in your batch class. It works fine for you. Here is a example

public class batchclass implements Database.Batchable<sObject>{

global List<Id> currentId ;

// Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){

                return Database.getQueryLocator('SELECT Id,Name FROM Opportunity WHERE Id in :currentId and Type in : oppTypes ');

and your main class

public class mainclass { 
public void somemethod() {
batchclass bat = new batchclass ();
bat.currentId =  opptyId ;
database.executeBatch(bat,2) ;

  • Can you please show me an example ? I am not getting it . Sorry! – Testing_SFDC Sep 18 '15 at 6:24
  • @SFDC_Learner update my answer. – Tushar Sharma Sep 18 '15 at 6:54

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.