function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumithasumitha 

Need help Batch: Apex class - Exceute Method is not working

Hi All,

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
 
Best Answer chosen by sumitha
Linga_RaminLinga_Ramin
Hi Sumitha,

   In Start() method you are Querying for 'A__c' and in Execute() method you are calling 'B__c'.So that's why Execute() method not working.
Now in Execute() method change 'B__c' to 'A__c' in Execute() method and that would work

All Answers

Linga_RaminLinga_Ramin
Hi Sumitha,

   In Start() method you are Querying for 'A__c' and in Execute() method you are calling 'B__c'.So that's why Execute() method not working.
Now in Execute() method change 'B__c' to 'A__c' in Execute() method and that would work
This was selected as the best answer
sumithasumitha
Thank you for the solution.Now it is working :-)