function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew marshal 3Andrew marshal 3 

Write a trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.

Write a trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.
Best Answer chosen by Andrew marshal 3
SubratSubrat (Salesforce Developers) 
Hello Andrew ,

Here's an example trigger that will update the Account rating to "Hot" whenever an Opportunity stage is changed to "Closed Won".
trigger UpdateAccountRating on Opportunity (after update) {
    Set<Id> accountIds = new Set<Id>();
    for (Opportunity opp : Trigger.new) {
        if (opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
            accountIds.add(opp.AccountId);
        }
    }
    
    List<Account> accountsToUpdate = new List<Account>();
    for (Account acc : [SELECT Id, Rating FROM Account WHERE Id IN :accountIds]) {
        acc.Rating = 'Hot';
        accountsToUpdate.add(acc);
    }
    
    if (!accountsToUpdate.isEmpty()) {
        update accountsToUpdate;
    }
}

If it helps please mark this as Best Answer.
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello Andrew ,

Here's an example trigger that will update the Account rating to "Hot" whenever an Opportunity stage is changed to "Closed Won".
trigger UpdateAccountRating on Opportunity (after update) {
    Set<Id> accountIds = new Set<Id>();
    for (Opportunity opp : Trigger.new) {
        if (opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
            accountIds.add(opp.AccountId);
        }
    }
    
    List<Account> accountsToUpdate = new List<Account>();
    for (Account acc : [SELECT Id, Rating FROM Account WHERE Id IN :accountIds]) {
        acc.Rating = 'Hot';
        accountsToUpdate.add(acc);
    }
    
    if (!accountsToUpdate.isEmpty()) {
        update accountsToUpdate;
    }
}

If it helps please mark this as Best Answer.
Thank you.
This was selected as the best answer
Arun Kumar 1141Arun Kumar 1141

Hello Andrew,
Please find the below trigger for the question you asked. This trigger will cover your scenario of after and before as well.
//trigger controller class
trigger AccountRatingUpdate on Opportunity (before insert, after insert, before update, after update) {
    if(trigger.isAfter){
        if(trigger.isinsert){
            OpportunityHandler.accountRatingUpdateWhenInsert(trigger.new);
        }
        if(trigger.isUpdate){
            OpportunityHandler.accountRatingUpdateWhenUpdate(trigger.newMap,trigger.oldMap);
        }
    }
    if(trigger.isbefore){
        if(trigger.isinsert){
            OpportunityHandler.accountRatingUpdateWhenInsert(trigger.new);
        }
        if(trigger.isUpdate){
            OpportunityHandler.accountRatingUpdateWhenUpdate(trigger.newMap,trigger.oldMap);
        }
    }
    
}
----------------------------------------------------------------------------------------------------------------------------------------------

//trigger Controller class

public class OpportunityHandler {
    public static void accountRatingUpdateWhenInsert(List<Opportunity> opportunityNewList){
        List<Opportunity> opportunityList = [SELECT ID, StageName, AccountId 
                                             FROM Opportunity 
                                             WHERE StageName = 'Closed Won' 
                                             AND ID IN:opportunityNewList];
        
        Set<Id> accId = new Set<Id>();
        if(!opportunityList.isEmpty()){
            for(Opportunity opp : opportunityList){
                accId.add(opp.AccountId);
            }
        }
        List<Account> accList = [SELECT ID, Rating FROM Account WHERE ID IN:accId];
        for(Account acc : accList){
            acc.Rating = 'Hot'; 
        }
        update accList;
    }
    public static void accountRatingUpdateWhenUpdate(Map<Id,Opportunity> OpportunityNewMap,Map<Id,Opportunity> OpportunityOldMap){
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity opp : OpportunityNewMap.Values()){
            if(opp.StageName != OpportunityOldMap.get(opp.Id).StageName){
                oppList.add(opp);
            }
        }
        List<Opportunity> opportunityList = [SELECT ID , StageName, AccountId 
                                             FROM Opportunity 
                                             WHERE StageName = 'Closed Won' 
                                             AND ID IN:oppList];
        
        Set<Id> accId = new Set<Id>();
        if(!opportunityList.isEmpty()){
            for(Opportunity opp : opportunityList){
                accId.add(opp.AccountId);
            }
        }
        List<Account> accList = [SELECT ID, Rating FROM Account WHERE ID IN:accId];
        for(Account acc : accList){
            acc.Rating = 'Hot'; 
        }
        update accList;        
    }
}

If it helps please mark this as the Best Answer.
Thank you.

Prateek Prasoon 25Prateek Prasoon 25
trigger UpdateAccountRating on Opportunity (after update) {
    Map<Id, Opportunity> oldOpps = Trigger.oldMap;
    Map<Id, Opportunity> newOpps = Trigger.newMap;
    List<Account> acctsToUpdate = new List<Account>();
    
    for (Opportunity opp : newOpps.values()) {
        if (oldOpps.get(opp.Id).StageName != 'Closed Won' && opp.StageName == 'Closed Won') {
            acctsToUpdate.add(new Account(Id = opp.AccountId, Rating = 'Hot'));
        }
    }
    
    if (!acctsToUpdate.isEmpty()) {
        update acctsToUpdate;
    }
}

If you find this answer helpful. Please mark it as the best answer.
Anand kurubaAnand kuruba
Hello Andrew ,
Here's an example trigger whenever a opportunity stage is changed to closed won , change account rating to Hot.

trigger UpdateAccountRating on Opportunity (after update) {
    // Create a set of Account IDs to update
    Set<Id> accountIdsToUpdate = new Set<Id>();
    
    // Loop through the updated Opportunities
    for (Opportunity opp : Trigger.new) {
        // Check if the Stage has changed to "Closed Won"
        if (opp.StageName == 'Closed Won' && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
            // Add the Account ID to the set of Accounts to update
            accountIdsToUpdate.add(opp.AccountId);
        }
    }
    
    // Query for the Accounts to update
    List<Account> accountsToUpdate = [SELECT Id, Rating FROM Account WHERE Id IN :accountIdsToUpdate];
    
    // Loop through the Accounts and update the Rating
    for (Account acct : accountsToUpdate) {
        acct.Rating = 'Hot';
    }
// Update the Accounts
    update accountsToUpdate;
}

If it helps please mark this as the Best Answer.
Thank you