Passing Parameter to batch apex

  • 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.

    where you want to pass parameter, from where plz elaborate more ?

    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

    Controller Code: public class callNotesbatchcls { Public PageReference callingMethod() { batchNotesInsert shn = new batchNotesInsert(); database.executeBatch(shn); return null; } }

    It shows error:"Error: Compile Error: Method does not exist or incorrect signature: database.executeBatch() at line 6 column 8"

    it should be `database.executeBatch(shn )` my typo my bad

    Sir, no action is performed on visualforce page.

    @JanaShree Yes you need to just call `callingMethod` method .

  • Ratan Paul

    Ratan Paul Correct answer

    6 years ago

    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

    hey Ratan - `strParameter = ''` is unnecessary.

    Yes not needed.

    Doesn't it have to implement `Database.Stateful` for the above to work?

    @AdrianLarson no need `Database.Stateful`

    I think @AdrianLarson is right and you need Stateful

    @SebastianKessel I double checked in my org. You can also check in your org. Without 'Database.Stateful' we still get the values.

    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 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm.

    Isn't it just to maintain the variable between batches? So you would have the variable set during the first batch but subsequent batches you would not?

    @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.

    Could anyone please help me sir.. how can i use this code..? I'm totally confused.

    @JanaShree Are you trying to execute this batch from VF page? and want to pass any variable from VF page

    Yes sir, But still i have problem. When i click button on VF page, there is no action preformed. Now what i want to do.. please help me for this..

    @JanaShree You need to call controller method onclick of button there execute `database.execute(shn)`

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM