+ Start a Discussion
sumithasumitha 

Batch Apex Class - How to pass the String and Use into Query inside the Execute Function(Batch method).

Hi All,
I Have an Visual force page with some Set of List and String.
I have given those List and string While calling the Batch apex
Extension_Batch  batch = new CloneNSTExtension_Batch(List name,String name);
         batch.executeBatch(batch);
My clarification is how to take the String name inside the excute method in batch apex.Because,those string(which have value from the  normal apex class) I have added as the Condition value in query.

Please advice.

Thanks in advance
Sumitha P
Best Answer chosen by sumitha
Greg HGreg H
You can't pass arguments into the execute method in that way. You can, however, pass variables into the constructor, which can then be used by your execute method. Depending on your needs it might make sense specify Database.Stateful in the class definition.
-greg

All Answers

Greg HGreg H
You can't pass arguments into the execute method in that way. You can, however, pass variables into the constructor, which can then be used by your execute method. Depending on your needs it might make sense specify Database.Stateful in the class definition.
-greg
This was selected as the best answer
sumithasumitha
Thank you greg got your point.
 
-Sumitha
sumithasumitha
Hi guys,

I have attached the code  where I have passed the variable through contructor in Batch Apex class

I have written Batch apex with constructor and execute and written system.debug for checking.
Where it is not seeing the system.debug statement inside Execute method in debug logs.

I need help on execution method in Batch Apex.I am unaware why the execute method is not working on my side.

Apex class:
Extension_Batch  batch = new Extension_Batch(Newprdt,newRecord);
         Database.executeBatch(batch,200);
Batch Apex class:
global class Extension_Batch implements Database.Batchable<sObject>
{
 global List<B__c> Newprdt123;
 global  A__c newRecord123;   
 global Extension_Batch(List<B__c> Newprdt,A__c newRecord)
{
   //this block is working fine

    system.debug('Newprdt_batch'+Newprdt);
    system.debug('newRecord'+newRecord);
   Newprdt123 = Newprdt;
   newRecord123 = newRecord;
    system.debug('Newprdt123'+Newprdt123);
    system.debug('newRecord123'+newRecord123);
}
// Start Method
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
        system.debug('instart');
     return Database.getQueryLocator('Select id,OwnerId,IsDeleted,RecordTypeId,CreatedDate,CreatedById,LastModifiedDate, LastModifiedById  from A__c');
    }
   // Execute method
    global void execute(Database.BatchableContext BC,List<B__c> Newprdt123) 
    { 
        
       system.debug(' '+Newprdt123);  
       system.debug('newRecord123'+newRecord123); 
        
     for(B__c prdt :Newprdt123)
       {
         for(PricebookEntry pbe2 :[SELECT Id,Name,isactive,PriceBook2Id,Pricebook2.id,Product2.CurrencyIsoCode,
                                     PriceBook2.Name,PriceBook2.isactive,Product2.Name,Product2.ProductCode,
                                         Product2.family,UnitPrice,Product2.Description,product2.isactive FROM PriceBookentry 
                                         where Product2.ProductCode =:prdt.Name and isactive=true
                                         and Pricebook2.id=:newRecord123.PriceList__c])
           {
            if( prdt.List_Price_Per_Unit__c <> pbe2.UnitPrice & pbe2.Product2.ProductCode == prdt.Name)
               {
                  prdt.List_Price_Per_Unit__c = pbe2.UnitPrice;
               }
             update prdt;  
              
           }
          
                
       List<Pricebookentry> pbe3=[SELECT Id,Name,isactive,PriceBook2Id,Product2.ProductCode,PriceBook2.Name,PriceBook2.isactive,Product2.Name,
                                         Product2.family,Product2.Description,Unitprice,product2.isactive FROM PriceBookentry 
                                         where Product2.ProductCode =:prdt.Name and isactive=false and Pricebook2.id=:newRecord123.PriceList__c];
       
           if(pbe3.size() ==1)
           {
          for(Pricebookentry pbe1:pbe3)
          {
                            
       List<B__c> stpr =[Select id,IsDeleted,Product__r.id,Name,CreatedDate,CreatedById,LastModifiedDate,LastModifiedById,SystemModstamp,LastActivityDate From B__c 
                                   where Number1__c=:newRecord123.id and Name=: pbe1.product2.ProductCode];
              
            
              delete stpr;
         
          }
       }
           
       }
    }       
    // Finish Method
    global void finish(Database.BatchableContext BC) 
    {
       system.debug('Batch apex completed');
    }

}






Thanks in Advance.
Sumitha P