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
Linda 98Linda 98 

More bulkification of apex trigger...please help!!!!!!

Hi,

I am trying to bulkify this trigger..But when i load more produtcs i still get error ...So i need to bulkify this trigger more...Please help!!!!

trigger StandardPriceupdate on Product2 (after insert,after update) {
 
  List<Pricebook2> listPricebook = new List<Pricebook2>();
  List<PricebookEntry> listPricebookEntry = new List<PricebookEntry>(); 
  List<PricebookEntry> listupdPBentry = new List<PricebookEntry>();
  List<PricebookEntry> listinsPBentry = new List<PricebookEntry>();
  List<Product2> listProducts = new List<Product2>();
  Public boolean PricebookCheck = False;
 
   listPricebook = [SELECT IsActive, Description, IsStandard, Id, Name FROM Pricebook2 where Name =: 'Standard Price Book'];
   listPricebookEntry = [SELECT UnitPrice, Id, Pricebook2Id, ProductCode, Product2Id, Name, SystemModstamp, UseStandardPrice, IsActive FROM       PricebookEntry where Pricebook2Id =: listPricebook[0].Id];

   for(Product2 objProduct : System.Trigger.New) {
      listProducts.add(objProduct);      
   }
   
    for(Product2 Prod : listProducts){
      for(PricebookEntry PbEntry : listPricebookEntry){
            if(PbEntry.Product2Id == Prod.id){
             PbEntry.Unitprice = Prod.Std_PB_List_Price__c;
             listupdPBentry.add(PbEntry);
             PricebookCheck = True;
             }           
        }
        if(PricebookCheck != True){
            PricebookEntry insPBentry = new PricebookEntry (IsActive = True, UnitPrice = Prod.Std_PB_List_Price__c, Pricebook2Id =listPricebook[0].Id, Product2Id =Prod.Id);
            listinsPBentry.add(insPBentry);       
        }
        PricebookCheck = False;      
    }     
    
    if(listupdPBentry.size()>0){
     update listupdPBentry;
    }
    if(listinsPBentry.size()>0){
     insert listinsPBentry;
    }

}
RadnipRadnip
What error are you getting? Also its best practise to put your code outside the trigger. Create a class and then call the method in the class from the trigger. Also the checking of the list sizes at the end isn't needed, Salesforce is smart enough to know if the list is empty not to perform a DML call.
Boom B OpFocusBoom B OpFocus
I probably try to use Map of PricebookEntry, indexed by Product2Id.

So it can be something like this:

Map<string, PricebookEntry> mapPBEsbyProduct = new Map<string, PricebookEntry>;

for (PricebookEntry pbe : [SELECT UnitPrice, Id, Pricebook2Id, ProductCode, Product2Id, Name, SystemModstamp, UseStandardPrice, IsActive FROM       PricebookEntry where Pricebook2Id =: listPricebook[0].Id]) {

     mapPBEsbyProduct.put(pbe.Product2Id, pbe);

}


Then we can avoid using "listPricebookEntry" inside the for loop of the listProducts.

It will look like this:

for(Product2 Prod : listProducts){
      if (mapPBEsbyProduct.containsKey(Prod.Id) {

           PricebookEntry PbEntry = mapPBEsbyProduct.get(Product.Id);

           if(PbEntry.Product2Id == Prod.id){
             PbEntry.Unitprice = Prod.Std_PB_List_Price__c;
             listupdPBentry.add(PbEntry);
             PricebookCheck = True;
          }
     }
.
.
.


Hope this helps.


Kiran  KurellaKiran Kurella
I recommend using a map as Boom recommended. You can also optimize the code by retrieving only the fields and records required for this operation (i.e., retrieve PricebookEntry records for a specific set of Products instead of pulling everything).

for (PricebookEntry pbe : [SELECT UnitPrice, Id, Pricebook2Id, Product2Id, Name, UseStandardPrice, IsActive FROM  PricebookEntry where Pricebook2Id =: listPricebook[0].Id and Product2Id in : trigger.newMap.keySet()]) {