You need to sign in to do that
Don't have an account?
Linda 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;
}
}
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;
}
}
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.
for (PricebookEntry pbe : [SELECT UnitPrice, Id, Pricebook2Id, Product2Id, Name, UseStandardPrice, IsActive FROM PricebookEntry where Pricebook2Id =: listPricebook[0].Id and Product2Id in : trigger.newMap.keySet()]) {