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
Hrishikesh KolheHrishikesh Kolhe 

Update Account Rating  to  'Hot'  on account when opportunity  stage equals to 'Closed Won' ELSE update account rating to 'Cold' How to deal with above scenario new one---->

Update Account Rating  to  'Hot'  on account when opportunity  stage equals to 'Closed Won' ELSE update account rating to 'Cold'

How to deal with above scenario new one---->

/*Update Account Rating  to  'Hot'  on account when opp stage equals to 'Closed Won' ELSE update account rating to 'Cold'*/
trigger Accountoppstage2 on Opportunity ( After insert, After update) {
    list<Account> acclist= new list<Account>();
     Set<Id> accID = new Set<Id>();
     Set<Id> ratID= new Set<Id>();
    
    for(Opportunity Opp: Trigger.new){
        if(Opp.StageName=='Closed Won'){
            AccID.add(Opp.AccountID);       
        }else{
            ratID.add(Opp.ratID);
        }
    }
    
    for(account acc:[Select Id,Rating from Account where ID In: accID]){
        acc.Rating= 'Hot';
        acclist.add(acc);
    }
     for(account acc:[Select Id,Rating from Account where ID In: ratID]){
        acc.Rating= 'Cold';
        acclist.add(acc);
    }
    Update acclist; 
    
   
}



And Facing below error...................>
Variable does not exist: ratID

Thanks in Advance!!!!!
Best Answer chosen by Hrishikesh Kolhe
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hrishikesh,

Can you try the below code.
 
trigger accstatusopptrigger on Opportunity(after update,after insert){
  Map<Id,String> accmap=new Map<Id,String>();
   for(Opportunity opp:trigger.new){
       if(opp.stagename=='Closed Won' )   {
         accmap.put(opp.AccountID,'Hot' )  ;
       } 
       else{
            accmap.put(opp.AccountID,'Cold' )  ;
       }
   }
   List<Account> acclist=[SELECT Id,Rating FROM Account WHERE Id IN:accmap.keyset()];
   List<Account> acclist2=new List<Account>();
   for(Account acc:acclist){
       acc.Rating=accmap.get(acc.Id);
        acclist2.add(acc);
    }
    try{
       update acclist2;
    }catch(Exception e){
      System.debug(e);
  }
}

If this solution helps, please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hrishikesh,

Can you try the below code.
 
trigger accstatusopptrigger on Opportunity(after update,after insert){
  Map<Id,String> accmap=new Map<Id,String>();
   for(Opportunity opp:trigger.new){
       if(opp.stagename=='Closed Won' )   {
         accmap.put(opp.AccountID,'Hot' )  ;
       } 
       else{
            accmap.put(opp.AccountID,'Cold' )  ;
       }
   }
   List<Account> acclist=[SELECT Id,Rating FROM Account WHERE Id IN:accmap.keyset()];
   List<Account> acclist2=new List<Account>();
   for(Account acc:acclist){
       acc.Rating=accmap.get(acc.Id);
        acclist2.add(acc);
    }
    try{
       update acclist2;
    }catch(Exception e){
      System.debug(e);
  }
}

If this solution helps, please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
CharuDuttCharuDutt
Hi Hrishikesh
Try Below Code
trigger accsttrigger on Opportunity(after update,after insert){
  Map<Id,String> accmap=new Map<Id,String>();
   for(Opportunity opp:trigger.new){
       if(opp.stagename=='Closed Won' )   {
         accmap.put(opp.AccountID,'Hot' )  ;
       } 
       else{
            accmap.put(opp.AccountID,'Cold' )  ;
       }
   }
   List<Account> acclist=[SELECT Id,Rating FROM Account WHERE Id IN:accmap.keyset()];
   List<Account> acclist2=new List<Account>();
   for(Account acc:acclist){
       if(accmap.containsKey(acc.Id)){
       acc.Rating=accmap.get(acc.Id);
        acclist2.add(acc);
        }
    }
    try{
       update acclist2;
    }catch(Exception e){
      System.debug(e);
  }
}
Please Mark It As Best Answer If It Helps
Thank You!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi CharuDutt,

I see you have posted exactly the same code as mine. Would you be able to take off your comment to avoid confusion of dupliactes for end users.

Thanks,

 
Naveen KNNaveen KN
Please also consider automation for this scenario. As the requirement is simple and we can achieve with the lightning flow builder (where you can use record triggered flow > after update context)