You need to sign in to do that
Don't have an account?
Nathan Prats 22
Apex Rollup summary
Hi,
I'm trying to update a field named Primary Partner Gross Margin on the opportunity object based on opportunity line items.
The code compiles, but it doesn't update the field on update. Any idea why ?
I'm trying to update a field named Primary Partner Gross Margin on the opportunity object based on opportunity line items.
The code compiles, but it doesn't update the field on update. Any idea why ?
trigger OpportunityTriggers on Opportunity (before update) { // Quand 1 Opp a un Primary Partner Account Reseller, remplir le champs "Primary Partner Gross Margin" = Sum OpportunityLineItems Software * Primary_Partner_Margin__c // C'est forcément before car on a besoin d'update des records, et quand on est en after, les records sont lock // Prendre la liste des opportunités pour lesquelles on va peupler Primary_Partner_Gross_Margin__c For (Opportunity opp : [SELECT Id,Primary_Partner_Gross_Margin__c,Primary_Partner_Margin__c FROM Opportunity WHERE Primary_Partner_Role__c ='Reseller' AND Deal_with_a_partner__c = 'Yes' AND Id IN :Trigger.old ]) { // Updater le champs Primary_Partner_Gross_Margin__c Double GrossMargin = [SELECT TotalPrice FROM OpportunityLineItem WHERE PricebookEntry.product2.Family='Software' AND OpportunityId =: opp.Id ][0].TotalPrice; opp.Primary_Partner_Gross_Margin__c=GrossMargin*opp.Primary_Partner_Margin__c; opp.Name = 'Worked'; } }
Please write a trigger on opportunity line items. Here's the code you are looking for
Hope it solves your problem.
Please mark this as best answer if it solves.
Thanks,
Balaji J
All Answers
Please write a trigger on opportunity line items. Here's the code you are looking for
Hope it solves your problem.
Please mark this as best answer if it solves.
Thanks,
Balaji J
I changed my code like this. It seems to work I did a few tests.
Why is it better to do a trigger on line items over the opportunity object ? I don't understand.
First thing is you are calculating Opportunity.Primary_Partner_Gross_Margin__c field value based on the TotalPrice value in the OpportunityLineItems.
If you write a trigger on opportunity object, the trigger won't fire when you insert/update an opportunityLineItem and the Primary_Partner_Gross_Margin__c field on Opportunity does not contain the correct value. To get the right value, you have to do manual update on oppotunity record.
So,Please write a trigger on opportunity object to get the right value without doing any manual things.