Skip to main content

 An organization wants to automatically maintain an Account's Customer_Tier__c based on its Opportunities.  

 

Rules: 

  •  If Account has at least one Closed Won Opportunity with Amount >= $100,000, set Customer_Tier__c = 'Platinum'. 
  •  If it has a Closed Won Opportunity with Amount between $50,000–$99,999, set it to Gold. 
  •  Otherwise, set it to Silver. 
  •  The logic must work when Opportunities are inserted, updated, deleted, or undeleted. 
  •  It must handle bulk transactions. 
  •  No SOQL/DML inside loops. 
  •  The solution should use a Trigger Handler pattern.

Q.  How would you design this trigger and handler? Which trigger events would you use, and how would you efficiently recalculate the Account tier when an Opportunity is deleted or its Amount/Stage changes? 

  

 

#Salesforce Developer  #Apex  #Salesforce

3 件の回答
  1. 昨日、6:19

    @Deepak Sharma

    I would use an after-trigger for all four events because the Account tier depends on the current set of Opportunities. Insert and undelete can introduce a qualifying Opportunity, update can change Amount, Stage, or Account, and delete can remove the Opportunity that previously determined the tier.  

    For update, I collect both the old and new Account IDs so that an Opportunity moving between Accounts recalculates both Accounts.  

    The handler passes a Set of Account IDs to a service class, which performs bulk SOQL, calculates the highest applicable tier in memory, and performs a single bulk Account update. This avoids SOQL and DML inside loops and handles bulk transactions safely.  

0/9000