You need to sign in to do that
Don't have an account?
Josh Lauer
Trigger Before Update Error
Hi
I am a newbie and I wrote the trigger code below to test a field to see whetherit has been updated then if so call new code... when i runs i get the error below.. If anyone can help me to resolveit, would be greatly appreciated
THe code is meant to check Pricebook2id to see if it has changed, then if it has changed load all the products from the pricebook into the opportunity
Update failed. First exception on row 0 with id 00661000002hNvAAAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateProduct: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId] Class.PricebookPicklist.updateRecords:"
I am a newbie and I wrote the trigger code below to test a field to see whetherit has been updated then if so call new code... when i runs i get the error below.. If anyone can help me to resolveit, would be greatly appreciated
THe code is meant to check Pricebook2id to see if it has changed, then if it has changed load all the products from the pricebook into the opportunity
Update failed. First exception on row 0 with id 00661000002hNvAAAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateProduct: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId] Class.PricebookPicklist.updateRecords:"
trigger UpdateProduct on Opportunity (before update) { for(Opportunity o: Trigger.new) { if(system.trigger.OldMap.get(o.Id).Pricebook2Id != system.trigger.NewMap.get(o.Id).Pricebook2Id) { //Populate records PricebookPicklist obj = new PricebookPicklist(); obj.updateRecords(system.trigger.NewMap.get(o.Id).Id, system.trigger.NewMap.get(o.Id).Pricebook2Id); o.NextStep = 'Changed'; } else { o.NextStep = 'Unchanged'; } } } public PageReference updateRecords(String oppid, String id) { Opportunity opp = [Select pricebook2Id from Opportunity where Id = :oppid]; //Find opportunity page using the parameter List<PricebookEntry> pbeList = [Select Product2.ProductCode, UnitPrice, Product2ID From PricebookEntry where pricebook2Id = :id]; List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); for(PricebookEntry pbe: pbeList) { OpportunityLineItem oli = new OpportunityLineItem(); oli.OpportunityId = opp.id; oli.PricebookEntryId = pbe.id; oli.Quantity = 1; oli.UnitPrice = pbe.UnitPrice; oliList.add(oli); } insert oliList; return null; }
I have go through the code of your trigger and figure out what you want to achieve.
Please change your trigger code from below mentioned code :
As we can only update any other object which is not related to current object of trigger in AFTER event only so i have written the code to update OLI in after update trigger.
Hope this will help you and please let me know if you have any issues in it.
Thanks,
Abhishek
All Answers
I have go through the code of your trigger and figure out what you want to achieve.
Please change your trigger code from below mentioned code :
As we can only update any other object which is not related to current object of trigger in AFTER event only so i have written the code to update OLI in after update trigger.
Hope this will help you and please let me know if you have any issues in it.
Thanks,
Abhishek
I do have one more issue: I'm trying to also clear the old items so it can change pricebooks without the user needing to manually delete the old items. I have code that works in an Apex Class but can't figure out where it needs to go in the Apex Trigger. At the moment I just have it grab a list of the old OpportunityLineItems, iterate through and delete them. That should work, I just have no idea where the code needs to go in order to run successfully.
I have updated your trigger as per your requirement.
Please find the modified trigger below :
Hope this will help you :)
Abhishek.
Unfortunately no luck. I'm getting the following error message:
Update failed. First exception on row 0 with id 00661000002hNvAAAU; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: Pricebook2Id (cannot change pricebook on opportunity with line items): [Pricebook2Id]
I think the main issue is that the old product items are still in Opportunity and I need to clear them. The delete doesn't seem to be running through. Would it be easier to do this as a Skype call?