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
masthan khanmasthan khan 

Trigger+ when ever opportunity stagename =closedwon automatically update the account field rating=hot

Hai,
   I am trying following sceanrio
Trgger  when ever opportunity stagename =closedwon automatically update the account field rating=hot.

the account field rating is not updating automatically.

trigger stage_closedwon_account_hot on Opportunity (after insert, after update) {
   set<id> ids=new set<id>();
    list<account> accs=new List<account>();
    for(opportunity a:trigger.new){
        if(a.stagename=='closed won'){
        id keys=a.account.id;
           ids.add(keys); 
        }
    }
    accs=[select id,rating from account where id=:ids];
    
    for(account ac:accs){
        ac.rating='hot';
    
    }update accs;
}
Regards
Masthan
Steven NsubugaSteven Nsubuga
Use IN not =
trigger stage_closedwon_account_hot on Opportunity (after insert, after update) {
	set<id> ids=new set<id>();
    list<account> accs=new List<account>();
    for(opportunity a:trigger.new){
        if(a.stagename=='closed won'){
        id keys=a.account.id;
           ids.add(keys); 
        }
    }
    accs=[select id,rating from account where id IN :ids];
    
    for(account ac:accs){
        ac.rating='hot';
    
    }
	update accs;
}

 
masthan khanmasthan khan
Hai,
Mr.Steven Nsubuga it not working the account rating is not getting updated.
with regards
Masthan.
Steven NsubugaSteven Nsubuga
Use this 
trigger stage_closedwon_account_hot on Opportunity (after insert, after update) {
    set<id> ids=new set<id>();
    list<account> accs=new List<account>();
    for(opportunity a:trigger.new){
        if(a.stagename=='Closed Won'){
        id keys=a.accountid;
           ids.add(keys); 
        }
    }
    accs=[select id, rating from account where id IN :ids];
    
    for(account ac:accs){
        ac.rating='Hot';
    
    }
    update accs;
}
Waqar Hussain SFWaqar Hussain SF
Hi Masthan Khan,

The issue is with line below in your code
id keys=a.account.id;

Here account Id will always get null because we can not get account.Id from trigger.new.
try below code
id keys=a.AccountId;

 
kamala swarnalathakamala swarnalatha
Hi,

Try this below code :

trigger stage_closedwon_account_hot on Opportunity (after insert, after update) {
    set<id> ids=new set<id>();
    list<account> accs=new List<account>();
    for(opportunity a:trigger.new){
        if(a.stagename=='Closed Won'){
        id keys=a.accountid;
           ids.add(keys); 
        }
    }
    accs=[select id, rating from account where id IN :ids];
    
    for(account ac:accs){
   integer i=0;
 ac.Id=accs[i].id;
        ac.rating='Hot';
    i++;
    }
    update accs;
}

Thanks,
K.Kamala,Jenefa
Sweet Potato Tec.