You need to sign in to do that
Don't have an account?
Apex Trigger - Trouble with saving an updated list
Hello, I am new to apex. I'm trying to make a trigger that updates the unit price on an opportunity's products marked with a specific discount when it is set to a certain stage. For testing purposes I have made a trigger that tries to update the marked prices anytime the opportunity is updated.
The problem I am running into is that I can't get the updated prices to save. I put debug statements in so I could see what was happening and everything looks ok to me. I have posted my trigger code and the debug log results below. Any help would be appreciated.
CODE:
trigger DEVProrateTrigger on Opportunity (before insert, before update) {
//GET TODAYS DATE AND SUBTRACT IT FROM 30 TO GET THE INTEGER TO DIVIDE THE PRICE BY, "PRICEDIV"
Date now = date.today();
integer day = now.day();
integer month = 30;
integer pricediv = month - day;
//GET A LIST OF THE PRODUCTS FOR THE OPPORTUNITY BEING UPDATED "PRODLIST"
List<OpportunityLineItem> prodList = [select ID, OpportunityId, Price_Adjustment_Reason_Code__c, UnitPrice from OpportunityLineItem where OpportunityId in: Trigger.new];
//DEBUG STATMENT #1 TO SHOW ME THE PULLED LIST OF PRODUCTS, "PRODLIST"
System.Debug(prodList);
//LOOP THROUGH PULLED "PRODLIST" TO FIND ANY WITH A PRICE ADJUSTMENT OF PRORATE AND DIVDE THEIR UNIT PRICE BY THE INTEGER "PRICEDEV"
for(OpportunityLineItem a : prodList){
if(a.Price_Adjustment_Reason_Code__c == 'PI = Prorated Item'){
a.UnitPrice = (a.UnitPrice / pricediv);
}
}
//DEBUG STATEMENT #2 TO SHOW ME "PRODLIST" WITH THE UPDATED VALUES
System.Debug(prodList);
//UPDATE/SAVE THE UPDATED "PRODLIST" TO THE DATABASE
Database.Saveresult[] results =Database.update(prodList);
//DEBUG STATEMENT #3 TO SHOW ME THAT "PRODLIST" WAS UPDATED/SAVED CORRECTLY
System.Debug(results);
}
DEBUG LOG STATEMENTS
#1 - 13:45:16.176 (176394000)|USER_DEBUG|[10]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=50.00}) -This statement shows the correct data was pulled.
#2 - 13:45:16.177 (177312000)|USER_DEBUG|[17]|DEBUG|(OpportunityLineItem:{Price_Adjustment_Reason_Code__c=PI = Prorated Item, OpportunityId=006W0000002VguoIAC, Id=00kW0000002zs2iIAA, UnitPrice=2.38095238095238095238095238095238}) -This statement shows that the unitprice in the list was updated from the loop.
#3 - 13:45:16.347 (347895000)|USER_DEBUG|[20]|DEBUG|(Database.SaveResult[getErrors=();getId=00kW0000002zs2iIAA;isSuccess=true;]) -This shows that the updated list with the updated price was saved to the database correctly.
I found out what the problem was. It looks like when you have schedules turned on for products it overides or does not allow you edit the unitprice field. I am sure this is noted somewhere, but I had no idea. Thanks for the help.
All Answers
Have you checked any workflow firing just after this execution?
Thanks for the suggestion. I have checked and disabled all workflow rules to see if one them was the culprit. The issue still remains.
I had the idea to try to update the field "UnitPrice" on the opportunitylineitem object via dataloader. It lets me update it and shows that it was successful via the success log, yet like the my apex trigger the update is not saved and the original value remains.
I have no idea what could be preventing it from being updated or how to find it.
Do you have the raw log for this entire operation ?
Sure. Thanks.
I found out what the problem was. It looks like when you have schedules turned on for products it overides or does not allow you edit the unitprice field. I am sure this is noted somewhere, but I had no idea. Thanks for the help.