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
Sid LightningSid Lightning 

Update Price on Custom Object, when price on price book enteries changes ?

Hi,

I have written this apex class to update prices on Product Interest Line Item, when price on Price book enteries changes.. However, the new price is not reflcting.. Can someone please help
Also, I want the new price to come basis the pricebook selected on the Product Interest level.

So , If A pricebook has 20 , and B pricebook has 30 , and If selected pricebook is 20. I want to update the New price field with changes in Pricebook A.
,

Also, I need to understand is a batch required for this or it will work everytime a price gets changed. Reason of asking is , I need to send a mail, when price on Price book entry is lower than Product Interest line item



public class UpdateRecentPriceOnProductInterest {
    
     public void updateList()

{ List<Product_Interest_Line_Item__c> pliobj;

   pliobj=[select Id, name,Product_Code__c,    New_Price__c,CSP_Edit__c from Product_Interest_Line_Item__c];
 
  for(Product_Interest_Line_Item__c pli :pliobj)
  {

Map<Id,PricebookEntry> mapPe=new Map<Id,PricebookEntry>([select id, UnitPrice from PricebookEntry and Productcode = : pli.Product_code__c]);
  for(Id id : mapPe.keySet())
    {  

 pli.New_Price__c=mapPe.get(pli.Product_code__c).unitprice;
 }
}
   update pliobj;
}

}
Tushar Tyagi 45Tushar Tyagi 45
Hi, you can replace your code with the below code:



public class UpdateRecentPriceOnProductInterest {
    
    
public void updateList()

         List<Product_Interest_Line_Item__c> pliobj;
 
   pliobj=[select Id, name,Product_Code__c,New_Price__c,CSP_Edit__c from Product_Interest_Line_Item__c];
    list<Product_Interest_Line_Item__c> updateList=new list<Product_Interest_Line_Item__c>();
 
  for(Product_Interest_Line_Item__c pli :pliobj)
  {

   Map<Id,PricebookEntry> mapPe=new Map<Id,PricebookEntry>([select id, UnitPrice from PricebookEntry where Productcode = : pli.Product_code__c]);
  
   
      for(Id id : mapPe.keySet())
     {  
      pli.New_Price__c=mapPe.get(id).unitprice;
      updateList.add(pli);          
    }
}
    
   update updateList;
}

}
Tushar Tyagi 45Tushar Tyagi 45
Let me know if it's not working
Sid LightningSid Lightning
Hi tushar,

Thanks for your reply.. ! However how should i check for which pricebook , as in does it take the default pricebook associated to the record
Sid LightningSid Lightning
Hi Tushar,

Unfortunately , the code is not working.