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
PamsPams 

Need help in batch class

Hi All,
I have below requirement. Please help me. I have two custom object.
    object name: Temp_product__c
    fields:Status__c(Picklist- 'Calculate price', 'remove price', 'processed','Skipped  Update')
             Product_name__c(lookup to product)\
I am creating new records in this object(Temp_product__c) using trigger when ever a product is created or updated with status either 'Clculate price' or 'remove price'
object name: entry_disc__c
    fields: offer_code__c(text)
             product_classification__c(picklist: 'connect', 'bound', 'digital')
             tier1_disc__c
             tier2_disc__c
             tier3_disc__c
            
    object name: product2 (Standard object)
    fields:   offer_code__c(text)
                item_type__c(text)
                net_price__c(currency)
                tier1_price__c(currency)
                tier2_price__c(currency)
                tier3_price__c(currency)
I want to process Temp_product__c's records using batch class with a status of either ‘Calculate Price’ or ‘Remove Price’ After each record is processed the status would be updated to “Processed” to indicate the record has been processed and if the resulting product record was updated.
  • For those Temp_product__c records with a status of ‘Remove Price Tiers’, the corresponding Product record should be updated.  The fields: tier1_price__c, tier2_price__c, tier3_price__c, should all be set to null/blank.
  • For those Temp_product__c table records with a status of 'Calculate price' pricing values will be calculated based on entry_disc__c object. This object will store discount values for tier1_disc__c, tier2_disc__c,tier3_disc__c corresponding to each offer_code__c and Product Classification. Specific users will have access to this object and will be responsible for maintaining the data in it.
Product Classification on the Product record is determined based on below logic
    
    1. if(item_type__c==c)
          connect
    2. if(item_type__c ==b)
          bound
    3. if(item_type__c==d)
          digital

Once the classification is determined, then the pricing will be calculated on entry_disc__c table.
Use the sponsor code and the product classification from the previous step to lookup the discount percent for each tier in the enrty object.
  • If no record is found, then set each of the price tiers(tier1_price__c,tier2_price__c,tier3_price__c) to null/blank on the product
  • If a record is found, then calculate each of the pricing tiers
  1. If tier1_disc__c = null, then set the tier1_price__c to null/blank, otherwise calculate price as net_price__c * (1 – tier1_disc__c)
  2. If tier2_disc__c = null, then set the tier2_price__c to null/blank, otherwise calculate price as net_price__c * (1 – tier2_disc__c)
  3. If tier3_disc__c = null, then set the tier3_price__c to null/blank, otherwise calculate price as net_price__c * (1 – tier3_disc__c)
If the Calculated price tier for all the fields are matching with the existing price tier data then skip the update and change the status of the temp record as “Skipped  Update”, otherwise update the new price tier values on the Product and change the status of the temp record to “Processed”.
BELOW IS MY CODE:
global class CalculatePrice implements Database.Batchable<sObject>{ 
    
    global database.querylocator start(Database.BatchableContext BC){
        String strStatus1 = 'Calculate price';
        String strStatus2 = 'Remove price';
        String query='';
        query ='SELECT Id, Product_Name__r.offer_code__c Product_Name__r.item_type__c,Product_Name__r.net_price__c, Product_Name__r.tier1_price__c,Product_Name__r.tier2_price__c,Product_Name__r.tier3_price__c FROM Temp_product__c where status__c=:strStatus1 or status__c =: strStatus2';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Temp_product__c> lstprod){
       list<entry_disc__c> entryRec = new list<entry_disc__c>();
       list<Temp_product__c> tempLst = new list<Temp_product__c> ();
       map<String,String> mappd = new map<String,String>();
       set<String> sponCode = new set<String>();
       String strProdClass ='Connect,Digital,Bound';
       
       if(lstprod.size() > 0){
            for(Temp_product__c p1 : lstprod){
                  entry_disc__c entryobj = new entry_disc__c();
                  if(p1.status__c=='Calculate price'){
                      if(p1.Product_Name__r.item_type__c=='c'){
                        entryobj.product_classification__c='Connect';  
                        entryRec.add(entryobj);
                      }
                   else if(p1.Product_Name__r.item_type__c=='d'){
                       entryobj.product_classification__c='digital';
                       entryRec.add(entryobj);
                   }
                   else if(p1.Product_Name__r.item_type__c=='b'){
                       entryobj.product_classification__c='Bound';
                       entryRec.add(entryobj);
                   }
                   else{}
                }
           }
            if(!entryRec.isEmpty())
            insert entryRec;
            
            for(Temp_product__c p2 : lstprod){
                
                sponCode.add(p2.Product_Name__r.offer_code__c);
                
            }
            
             for(entry_disc__c e : entryRec){
               if(sponCode.containsKey(e.offer_code__c) && (!string.isEmpty(e.product_classification__c) && strProdClass.contains(e.product_classification__c))){
                  }   
            }
        }
    }
    
    global void finish(Database.BatchableContext BC){

    }
}
I am not able to perform calculation on entry object. Please help me. Thank ypi in advance.

   
Matthew CokeMatthew Coke
debug statements help :)

also, what is the last bit of code doing? You have an if control statement but no actions to take if the logic check is met. What kid of calculation are you trying to do on the entry object?
PamsPams
Please see the above logic given in problem. In this if condition I want to perform calculations regarding price as per given formula. The problem is I am not able find those product related to offer_code__c(sponsor code, this field is on both object i.e temp object and product2). With the use of offer_code__c(sponsor code) and product_classification__c, I want to perform calculations on entry object as given in above requirement.