You need to sign in to do that
Don't have an account?
Big Ears
Interestingly, the error I am getting implies that I've defined a validation formula that's upsetting the system. However, there are no validation rules that get their information from any of the fields being affected by the update. Also, the error arises when I try to update the Opportunity Product from family (A), having reduced it by the appropriate amount.
I'm really confused.
Opportunities & Opportunity Products - Apex Triggers and Roll up Formula Fields
Hey guys.
I have a Before Update trigger on the Opportunity Object that runs an Apex class which:
1. Checks if the Opportunity Status has been changed to a particular stage.
If it has, it then:
2. Checks the Opportunity Products (specifically if there are certain families of products in there)
If Opportunity Products from a specific family (A) are there, without a particular Opportunity Product from another family (B):
3. It automatically works out how much the Opportunity Product from family (B) should cost, given the overall sale cost of the Opportunity
4. Reduces the overall cost of the products of family (A) by the cost of the product from family (B)
5. Updates the Products from Family (A)
6. Inserts the product from Family (B)
At the end of the process, the overall Opportunity sale price remains the same, with the new product inserted. It was working a treat, until I added some roll-up formula fields that gather information about the products (An overall sales cost, an overall list price and a formula working out the overall Reduction against List Price)
When I tried that, I got the following error:
Code:
Apex trigger MyOpportunityUpdate caused an unexpected exception, contact your administrator: MyOpportunityUpdate: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00oS0000000KsjwIAC; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Object (id = 006S0000002JnAw) is currently being updated in trigger MyOpportunityUpdate: [Id]: Class.MyRFFCreator.CreateRFF: line 35, column 29
Interestingly, the error I am getting implies that I've defined a validation formula that's upsetting the system. However, there are no validation rules that get their information from any of the fields being affected by the update. Also, the error arises when I try to update the Opportunity Product from family (A), having reduced it by the appropriate amount.
The section of the APEX class that's causing the error is as follows:
Code:
for (OpportunityLineItem cli : FamilyA){ OpportunityLineItem LineItemToChange = [select id, UnitPrice, Quantity, TotalPrice, (select id, ScheduleDate, Revenue from OpportunityLineItemSchedules order by ScheduleDate asc nulls last limit 1)from OpportunityLineItem where id = :cli.id]; OpportunityLineItemSchedule LineItemScheduleToChange = LineItemToChange.OpportunityLineItemSchedules; LineItemScheduleToChange.Revenue = LineItemScheduleToChange.Revenue - NewRFF.UnitPrice; LineItemScheduleToChange.Description = 'This has been auto-edited by Apex'; update LineItemScheduleToChange; THIS LINE CAUSES THE ERROR }
I'm really confused.
Is the problem that changing a formula field counts as an update, and therefore the trigger is being recursively triggered?
Or that by altering the value of the revenue arising from the Opportunity Product, I'm causing a validation problem with the roll-up field? That would surprise me, as I've been able to add/remove and change Opportunity Products and the Roll-up formula fields deal with it with ease.
Or am I just making an obvious error and asking Salesforce/Apex to do something which it isn't able to?
Thank you for all your help, in advance. I hope I've been clear enough.
Your SELECT statement is not returning a field called OpportunityLineItemSchedules, so trying to update it will probably be quite hard. Nor have I ever heard of such a field, so might it be a custom field requiring __c or __r handling?
The error message says that the object is currently being updated in a trigger, which suggests that you are calling UPDATE on an object (an Opportunity?) that is already the subject of your trigger. Indeed the Id displayed starts with '006', which indicates an Opportunity record. This also suggests that you are not updating the object you think you are.
Might I suggest testing the SELECT statement in a SOQL tool to see if you're getting what you expect?
Thanks John,
Unfortunately, introducing "@future" into the class itself means that I get an error of: Unsupported Parameter type LIST:SOBJECT:Opportunity
I'm a complete dunce around @future, and haven't found the documentation to be useful, unfortunately. Also, the Opportunity Roll up fields aren't referenced in the trigger or apex class (because they're automatic) so I can't ask Salesforce to update those with the @future key word, surely?
Is there any way of just telling Salesforce to ignore any update triggers that are activated by a change in the Roll up fields? If I'm honest, I'm surprised that they're tripping the trigger at all.
Any additional help is greatly appreciated. You will be owed good karma and excellent beer.
Andy
Might not work, but worth a try.
I don't think you can definitively say that the error is being caused by roll-up fields.
Oh, and it could be worth adding Description to your select statement since you are setting it but not retrieving it.
Dear John,
Edited: Thank you for your suggestions! It's actually begun working, using an after trigger and a version of the "Singleton Pattern" making the system recognise that updates due to the Roll-Up fields can be ignored.
Interestingly, however, when I lower the revenue amount in the OpportunityLineItemSchedules and then run an update call, the reduction is placed in a second schedule line, below the first i.e.:
Becomes:
Message Edited by Big Ears on 11-13-2008 09:44 AM
Message Edited by Big Ears on 11-24-2008 03:55 AM
I have a related issue, with regards to creating a list in a trigger, and if that new insert list has more than 100 records, call a @future method to do the update. I am having trouble figuring out the syntax to send the list to the method from the trigger...I believe.
Here is the post (towards the end)
http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=6756&page=2
Need help with the following code..... I keep getting an error sayin my xzipcode field is an invalid token
trigger LeadAssignmentTrigger on Lead (before insert)
{
List<Lead> leadsToUpdate = new List<Lead>();
for (Lead lead : Trigger.new)
{
if (lead.PostalCode != NULL)
{
// Find the state for the current zip code
List <ZipCode__c> zip = [select State__c from ZipCode__c
WHERE lead.PostalCode.startsWith xZipCode__c = TRUE];
// if you found one
if (zip.size() > 0)
{
//assign the ZipCode to the correct Stae
lead.State = zip[0].State__c;
leadsToUpdate.add(lead);
}
}
}
}
Within a SOQL Where statement, the first expression must be a field and the second a value, eg:
Does anyone happen to have any idea on how to write a trigger to prevent duplicate accounts in trigger or have any source code they would like to share? It would be greatly appreciated