How can I combine both the total oppty line items (Total__c) (roll-up field in opportunity) and Miscellaneous_Fee__c (currency field in opportunity) in just one field using apex trigger? This miscellaneous fee is per opportunity, not per line item.

For practice, I used both the Opportunity and Opportunity Line Item objects. Here are some fields from these two:

Opportunity: Name, Date__c, Miscellaneous_Fee__c, Total__c

Opportunity Line Item: Product_Name__c, Price__c
  • have you seen my response? – Santanu Boral Jul 17 '16 at 5:35

A good way for doing that is to keep both fields (you don't have to put them on the layout) and use a formula field that displays (Total__c + Miscellaneous_Fee__c).

If you want a trigger, you need to check if the Miscellaneous_Fee__c has been changed and them put the sum on another field:

for(Opportunity opp : Trigger.new){
    if(opp.Miscellaneous_Fee__c != Trigger.OldMap.get(opp.Id).Miscellaneous_Fee__c){
        opp.Total_Sum_Field__c = opp.Miscellaneous_Fee__c + opp.Totals__c;

You can also use a workflow for that.

| improve this answer | |
  • Hi thanks for your response. I've created another formula field Total_Sum_Field__c in Opportunity, then tried referencing it on the trigger. But there's this error: Field is not writeable: Opportunity.Total_Sum_Field__c – lol Jul 16 '16 at 18:51
  • Fernondo provided two solutions either you could go with formula which should be "Total__c + Miscellaneous_Fee__c" or you can create normal currency field which can be updated from trigger. You are mixing two solutions. Go with any of the suggested. – Kiran Machhewar Jul 17 '16 at 4:26

First of All, If Opportunity line items exists under an Opportunity then the Opportunity Amount is the summation of Opportunity Line Item Pricing values.

So, you do not need to write any trigger for calculation, or no need to create separate roll up summary field for calculation.

Workaround no.1

Make Total__c as Formula (Currency) field and calculate as follows:

Total__c = Amount + Miscellaneous_Fee__c

Workaround no.2

Make Total__c as Currency field and update that by workflow field update with the above calculation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.