Apex Trigger Practice (5) – Rollup Amount from Opportunity to Account

COMPONENTS

  • On Standard Object – Account, there are following fields
    • Total Revenue (Total_Revenue__c) (Number (16,2))
  • On standard Object Opportunity, which is child of Account Object, there are following fields
    • StageName: Having value “Closed Won”
    • Type: Having values Renewal, Upsell, New Business
    • RenewalAmount__c : (Number (16,2))
    • UpsellAmount__c : (Number (16,2))
    • NewBusiness__c : (Number (16,2))

REQUIREMENT

  • For all opportunities under Account where StageName = Closed Won:
    • If Type = New Business then rollup value of “NewBusiness__c” to field “Total_Revenue__c” of Account.
    • If Type = Upsell then rollup value of “UpsellAmount__c” to field “Total_Revenue__c” of Account.
    • If Type = Renewal then rollup value of “RenewalAmount__c” to field “Total_Revenue__c” of Account.

TIPS

  • Code must be bulkified.
  • All corner cases like changes of StageName to Closed Won or something else, delete and undelete event, Account changes on Opportunity must be taken care.