You need to sign in to do that
Don't have an account?
pooja biswas
child to parent trigger
Hi
I am updating account field rating='Hot' whenever Opportunity stageName='Closed won'.
I would like to understand 2 things
a) account field rating is not updated when I edit any opportunity record and set the StageName='Closed Won'
b) How to use Map function in order to avoid loop.correct me if I am wrong.
Thanks
pooja
I am updating account field rating='Hot' whenever Opportunity stageName='Closed won'.
I would like to understand 2 things
a) account field rating is not updated when I edit any opportunity record and set the StageName='Closed Won'
b) How to use Map function in order to avoid loop.correct me if I am wrong.
public class updateaccountrating { public void updaterating(List<Opportunity> oppor) { // 1. Get opportunity accountID and store in a list. Set<ID> opporAccountID = new Set<ID>(); for(Opportunity o:oppor) { opporAccountID.add(o.AccountID); } List<Account> aFinal=new List<Account>(); List<Account> acc=[select ID,rating from account where ID IN :opporAccountID]; for(Opportunity o:oppor) { for(Account a:acc) { if (o.StageName == 'Closed Won') a.rating='Hot'; aFinal.add(a); } } if (aFinal.size() > 0 && aFinal != NULL) Database.Insert(aFinal,false); } } trigger trg_updateaccountrating on Opportunity (before Insert,before Update) { if (trigger.IsBefore) { if (trigger.IsUpdate) { updateaccountrating a = new updateaccountrating(); a.updaterating(Trigger.New); } } }
Thanks
pooja
The two things you want to understand,
a) account field rating is not updated when I edit any opportunity record and set the StageName='Closed Won
Its because you used Database.Insert(aFinal,false); in the class. Change it to Database.Update (aFinal,false); It will work definitely.
b) How to use Map function in order to avoid loop.correct me if I am wrong.
Please fing below modified code using Map.
Please let me know if that helps you.
Best Regards,
BALAJI
All Answers
1. You do not have to fire SOQL to get all accounts, you already have account ID from Opportunity, use it to create account instance.
2. You only need to loop through all Opportunities only once.
I have updated your handler function with the above, see if it works (note: it is uncompiled version)
The two things you want to understand,
a) account field rating is not updated when I edit any opportunity record and set the StageName='Closed Won
Its because you used Database.Insert(aFinal,false); in the class. Change it to Database.Update (aFinal,false); It will work definitely.
b) How to use Map function in order to avoid loop.correct me if I am wrong.
Please fing below modified code using Map.
Please let me know if that helps you.
Best Regards,
BALAJI
I did not understand this statement.
Account lAccObj = lIDToAccountMap.get(lOpp.AccountId);
why is lAccObj NULL?
Thanks for info.
I am clear now.
This resolves my issue.