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
UserSFDXUserSFDX 

Create a trigger on account if opp is

Create a trigger on account to Convert account rating to hot when it has more than 10 opportunities and half of the opportunity are closed won.
Best Answer chosen by UserSFDX
SubratSubrat (Salesforce Developers) 
Hello ,

Please try with below trigger and let me know furhter :
 
trigger ConvertAccountRatingToHot on Account (after insert, after update) {
    // List to hold Account records to be updated
    List<Account> acctsToUpdate = new List<Account>();
    
    // Iterate through all Accounts in trigger context
    for(Account a : Trigger.new){
        
        // Query for all Opportunities related to the Account
        List<Opportunity> opps = [SELECT Id, IsClosed, AccountId FROM Opportunity WHERE AccountId = :a.Id];
        
        // Count the total number of Opportunities and closed won Opportunities
        Integer totalOpps = opps.size();
        Integer wonOpps = 0;
        for(Opportunity o : opps){
            if(o.IsClosed && o.StageName == 'Closed Won'){
                wonOpps++;
            }
        }
        
        // Check if the Account meets the criteria to update the rating
        if(totalOpps > 10 && wonOpps >= totalOpps/2){
            // Update the Account Rating to "Hot"
            a.Rating = 'Hot';
            acctsToUpdate.add(a);
        }
    }
    
    // Update all Accounts that meet the criteria
    update acctsToUpdate;
}

Hope the above information helps !
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello ,

Please try with below trigger and let me know furhter :
 
trigger ConvertAccountRatingToHot on Account (after insert, after update) {
    // List to hold Account records to be updated
    List<Account> acctsToUpdate = new List<Account>();
    
    // Iterate through all Accounts in trigger context
    for(Account a : Trigger.new){
        
        // Query for all Opportunities related to the Account
        List<Opportunity> opps = [SELECT Id, IsClosed, AccountId FROM Opportunity WHERE AccountId = :a.Id];
        
        // Count the total number of Opportunities and closed won Opportunities
        Integer totalOpps = opps.size();
        Integer wonOpps = 0;
        for(Opportunity o : opps){
            if(o.IsClosed && o.StageName == 'Closed Won'){
                wonOpps++;
            }
        }
        
        // Check if the Account meets the criteria to update the rating
        if(totalOpps > 10 && wonOpps >= totalOpps/2){
            // Update the Account Rating to "Hot"
            a.Rating = 'Hot';
            acctsToUpdate.add(a);
        }
    }
    
    // Update all Accounts that meet the criteria
    update acctsToUpdate;
}

Hope the above information helps !
Thank you.
This was selected as the best answer
Arun Kumar 1141Arun Kumar 1141
Hi UserSFDX,
Try below Code : 
trigger updateAccountRating on Account (after insert, after update) {
    UpdateAccountRating.updateIt(Trigger.New);
}

public class UpdateAccountRating {
    public static void updateIt(List<Account> accountList){
        List<Account> accList = new List<Account>();
        Set<Id> accIDs = new Set<Id>();
        for(Account acc : accountList){
            accIDs.add(acc.Id);
        }
        List<Opportunity> oppList = [SELECT Id, IsClosed, AccountId FROM Opportunity WHERE AccountId = :accIDs];
        for(Account acc : accountList){
            Integer oppCount = 0;
            for(Opportunity opp : oppList){
                if(opp.StageName == 'Closed Won'){
                    oppCount++;
                }
            }
            if(oppCount >= oppList.size()/2 && oppList.size() > 10){
                acc.Rating = 'Hot';
                accList.add(acc);
            }
        }
        update accList;
    }
}


Hope this will help.
Thanks!