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
StaceyWStaceyW 

Trigger only fires on first record in bulk upload

Hi All,

This is driving me a little crazy! I have written a simple trigger which creates or updates the PriceBookEntry in the Standard Pricebook when the Product is inserted or updated. It works when doing it manually but as soon as I upsert using the Data Loader or Jitterbit, it only fires on the first record.

I can't seem to figure out why and was hoping someone may be able to take a look at the code and identify the issue.

Any help would be very much appreciated :)

Code:

trigger CreatePBE on Product2 (after insert, after update) {

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
Set<ID> ids = new Set<ID>();
list<PricebookEntry> entries=new list<PricebookEntry>();

for (Product2 p : Trigger.new) {ids.add(p.Id);}
   
if(trigger.isinsert){
    for (Product2 p : Trigger.new) {
    entries.add(
    new PricebookEntry(
    Pricebook2Id=s.ID,
    Product2Id=p.Id,
    UnitPrice=p.Item_Cost__c,
    IsActive=p.IsActive,
    UseStandardPrice=FALSE)
    );
    }
    insert entries;
    }

  
List <PriceBookEntry> pbe = [select ID, UnitPrice FROM PricebookEntry WHERE Product2ID IN :ids AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE];
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
PriceBookEntry.UnitPrice=p.Item_Cost__c;
       }
Update pbe;
}
}
Best Answer chosen by StaceyW
kiranmutturukiranmutturu
try with this code base

map<string,PricebookEntry> mPBE = new map<string,PricebookEntry>();

for(PricebookEntry pbe : [select ID, Product2ID, UnitPrice FROM PricebookEntry WHERE Product2ID IN :ids AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE])
{
   mPBE.put(pbe.Product2ID, pbe);
}
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
mPBE.get(p.ID).UnitPrice = p.Item_Cost__c;
       }
Update mPBE.values();
}


All Answers

Ramu_SFDCRamu_SFDC
Can you please elaborate what you  are tyring to accomplish as the last for loop seems incorrect.
StaceyWStaceyW
Thanks for the response!

So basically when a product is upserted through the Data Loader it will update the Item Cost on the Product. That then needs to update the Unit Price of the Price Book Entry (standard pricebook) for that product to the new price. It does work when I update or insert a product manually, but when I use the data loader, only the price book entry for the first one is changed. All the others in the bulk upsert do not seem to cause the trigger to fire. There is no error messgae, the Item cost is updated on the product, there just isn't a change for the Price Book Entry.

The last part of the code used to be:

PriceBookEntry pbe = [select ID, UnitPrice FROM PricebookEntry WHERE Product2ID IN :ids AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE LIMIT 1];
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
       pbe.UnitPrice=p.Item_Cost__c;
       }
Update pbe;
}
}

But I thought maybe that was causing the problem and changed it 
I hope that makes sense. Please let me know if you need any more information.
dev_sfdc1dev_sfdc1
Hi weeball,
According to my knowledge, here in your query you have hardcoded something with the limit 1 so this may be the problem think so change limit to try again..

Thank You.
kiranmutturukiranmutturu
try with this code base

map<string,PricebookEntry> mPBE = new map<string,PricebookEntry>();

for(PricebookEntry pbe : [select ID, Product2ID, UnitPrice FROM PricebookEntry WHERE Product2ID IN :ids AND Pricebook2ID = '01s20000000FVFV' AND IsActive = TRUE])
{
   mPBE.put(pbe.Product2ID, pbe);
}
  
if(trigger.isupdate){
    for (Product2 p : Trigger.new) {
mPBE.get(p.ID).UnitPrice = p.Item_Cost__c;
       }
Update mPBE.values();
}


This was selected as the best answer
StaceyWStaceyW
Thank you, Kiran! That worked perfectly :)