// Write a trigger to set “Close Date” to today on “Closed-Won” Opportunities.

trigger UpdateCloseDateOnClosedWonOpportunities on Opportunity (before update) {
    // Create a list to hold the opportunities that need to be updated
    List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();
    
    for (Opportunity opp : Trigger.new) {
        // Check if the opportunity is transitioning to 'Closed-Won'
        if (opp.StageName == 'Closed-Won' && opp.CloseDate != Date.today()) {
            // Set the 'Close Date' to today
            opp.CloseDate = Date.today();
            opportunitiesToUpdate.add(opp);
        }
    }

    // Update the opportunities with the new 'Close Date'
    if (!opportunitiesToUpdate.isEmpty()) {
        update opportunitiesToUpdate;
    }
}

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.