18

I need to pass parameter to the batch apex class. This is apex class:

global class batchNotesInsert implements Database.Batchable<sObject> {

  global Database.QueryLocator start(Database.BatchableContext BC) {

        String query = 'select id,Parent.name,Parent.Type,Title,body from note';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Note> scope) {

         List<ShowNotes__c> shn = new List<ShowNotes__c>();
         for(Note a : scope)
         {

             shn.add
             (
                 new ShowNotes__c
                 (
                     Name = a.Parent.name,
                     Title__c = a.Title,
                     Content__c = a.body,
                     ParentOfNotes__c = a.Parent.Type
                 )
             );                  
         }

         insert shn;   

    }   

    global void finish(Database.BatchableContext BC) {
    }
}

Controller Code:

public class callNotesbatchcls { 

  Public PageReference callingMethod() 
  { 
       batchNotesInsert shn = new batchNotesInsert(); 
       database.executeBatch(shn); return null; 
   } 
} 

Thank you.

8
  • where you want to pass parameter, from where plz elaborate more ?
    – Ratan Paul
    Dec 22, 2015 at 12:30
  • Its still not clear where and which parameter do you need to pass. Please, provide those details and/or errors that you meet during attempts of doing it
    – kurunve
    Dec 22, 2015 at 12:31
  • Controller Code: public class callNotesbatchcls { Public PageReference callingMethod() { batchNotesInsert shn = new batchNotesInsert(); database.executeBatch(shn); return null; } }
    – Jana Shree
    Dec 22, 2015 at 12:34
  • It shows error:"Error: Compile Error: Method does not exist or incorrect signature: database.executeBatch() at line 6 column 8"
    – Jana Shree
    Dec 22, 2015 at 12:44
  • it should be database.executeBatch(shn ) my typo my bad
    – Ratan Paul
    Dec 22, 2015 at 12:46

2 Answers 2

27

Pass in constructor.

public class callNotesbatchcls { 

    Public PageReference callingMethod() 
    { 
        batchNotesInsert shn = new batchNotesInsert('Testing here'); 
        database.executeBatch(shn ); return null; 
    } 
} 

batch

global class batchNotesInsert implements Database.Batchable<sObject>, Database.Stateful {

    private String strParameter;

    public batchNotesInsert(String strParam) {
        strParameter = strParam;
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'select id,Parent.name,Parent.Type,Title,body from note';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Note> scope) {
        system.debug('==========check string====='+strParameter);    
    }   

    global void finish(Database.BatchableContext BC) {
    }
}

Yes Like @Eric and @crop1645 said.

If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.

If you do not use database.stateful then any changes made to the parameter during the execute method are not persisted across batches and the next execute method will be the original value. You can test this by adding strParameter += '--Added String' and you will see that the debugged value for each execute method is the original value

If we are modifying strParameter in execute method then still it will have its original value. maintain the updated value in strParameter we need database.stateful

Note: If you are not modifying strParameter value in execute method then no need to use database.stateful

13
  • 1
    hey Ratan - strParameter = '' is unnecessary.
    – cropredy
    Dec 22, 2015 at 14:33
  • Yes not needed.
    – Ratan Paul
    Dec 22, 2015 at 14:34
  • Doesn't it have to implement Database.Stateful for the above to work?
    – Adrian Larson
    Dec 22, 2015 at 16:28
  • 2
    hmm, my first reaction would be to agree with @AdrianLarson and @SebastianKessel as the doc states (re: per transaction - ea execute () is a xact) If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values. See developer.salesforce.com/docs/atlas.en-us.apexcode.meta/….
    – cropredy
    Dec 22, 2015 at 18:43
  • 2
    @Ratan - The original value of strParameter is maintained across batches. If you do not use database.stateful then any changes made to the parameter during the execute method are not persisted across batches and the next execute method will be the original value. You can test this by adding strParameter += '--Added String' and you will see that the debugged value for each execute method is the original value.
    – Eric
    Dec 23, 2015 at 2:45
-2

It works when you use the this keyword:

  private String strParameter;

   public batchNotesInsert(String strParam) {
      this.strParameter = strParam;
  }
3
  • 2
    I'm not totally sure how this answer responds to the question.
    – Adrian Larson
    May 14, 2018 at 22:37
  • I had same problem and deployed @Ratan Paul answer. The only issue was missing this May 15, 2018 at 22:47
  • 1
    The this keyword is optional nearly 100% of the time, the only exception being when you are trying to set an attribute whose name is the same as that of the parameter (e.g. public void foo(String bar) { this.bar = bar; }).
    – Adrian Larson
    May 15, 2018 at 22:48

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.