Scenario: 6
You're a Salesforce developer for a retail company that wants to implement a trigger to enforce a validation rule on the Opportunity object. The requirement is to prevent the stage of an Opportunity from being set to 'Closed Won' if the Opportunity amount exceeds a certain threshold defined by the company's sales policy. Additionally, whenever an Opportunity is closed as 'Closed Won', the system should automatically update the associated Account's total revenue. How would you approach this scenario using Apex triggers?
Code:
// Trigger: PreventClosedWonTrigger
// Object: Opportunity
// Purpose: Prevent setting the stage of an Opportunity to 'Closed Won' if the amount exceeds a threshold and update associated Account's total revenue on 'Closed Won'
trigger PreventClosedWonTrigger on Opportunity (before update) {
// Method to prevent setting stage to 'Closed Won' if amount exceeds threshold and update associated Account's total revenue
public static void preventClosedWonAndUpdateRevenue(List<Opportunity> updatedOpportunities, Map<Id, Opportunity> oldMap) {
// Collection to store Account IDs to be updated
Set<Id> accountIdsToUpdate = new Set<Id>();
// Iterate through updated Opportunities to check for 'Closed Won' stage and amount threshold
for (Opportunity opp : updatedOpportunities) {
if (opp.StageName == 'Closed Won' && opp.Amount > 10000) { // Assume threshold is $10,000
opp.addError('Opportunity amount exceeds threshold. Cannot set stage to "Closed Won".');
} else if (opp.StageName == 'Closed Won' && oldMap.get(opp.Id).StageName != 'Closed Won') {
accountIdsToUpdate.add(opp.AccountId);
}
}
// Query for related Accounts to update total revenue
List<Account> accountsToUpdate = [SELECT Id, Total_Revenue__c,
(SELECT Amount FROM Opportunities WHERE StageName = 'Closed Won')
FROM Account
WHERE Id IN :accountIdsToUpdate];
// Update total revenue of associated Accounts
for (Account acc : accountsToUpdate) {
Decimal totalRevenue = 0;
for (Opportunity opp : acc.Opportunities) {
totalRevenue += opp.Amount;
}
acc.Total_Revenue__c = totalRevenue;
}
// Update associated Account records
update accountsToUpdate;
}
// Trigger handler method
public static void beforeUpdate(List<Opportunity> updatedOpportunities, Map<Id, Opportunity> oldMap) {
preventClosedWonAndUpdateRevenue(updatedOpportunities, oldMap);
}
}
Evaluation:
- The candidate provides a comprehensive Apex trigger named
PreventClosedWonTrigger
that executes before the update of Opportunity records. - They effectively use SOQL queries and DML statements within the trigger to query related Account records and update their total revenue based on closed-won Opportunities.
- The code includes logic to prevent setting the stage of an Opportunity to 'Closed Won' if the amount exceeds a specified threshold, and it throws an error message to indicate the violation of the validation rule.
- The trigger handler method
beforeUpdate
calls thepreventClosedWonAndUpdateRevenue
method, maintaining separation of concerns and promoting code maintainability.
- Stay tuned for more tutorials and join our community SFDC Telugu!
Apex Programs PDF Free Downloads For Channel Members Join Channel MembershipFor more detailed explanations, practical examples, and resources, refer your friends to SFDC Telugu:
- YouTube Channel: SFDC Telugu
- Website: www.sfdctelugu.in
- Store: SFDC Telugu Store
- www.sfdctelugu.in
0 Comments