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
pooja biswaspooja 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.
 
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
Best Answer chosen by pooja biswas
BALAJI CHBALAJI CH
Hi 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.
public class updateaccountrating
{
    public void updaterating(List<Opportunity> oppor)
    {
        Map<Id,Account> accountMap = new Map<Id,Account>();
        for(Opportunity o : oppor){
            
            if(o.StageName == 'Closed Won'){
                accountMap.put(o.Id, new Account(Id=o.AccountId, rating='Hot'));
            }
        }
        //Update all the appropriate accounts in a bulkified manner here.
        if(accountMap.size() > 0) 
        {
            update accountMap.values();
        }
        
    }
}

Please let me know if that helps you.

Best Regards,
BALAJI
 

All Answers

Rajiv Penagonda 12Rajiv Penagonda 12
Your code can be optimized at two areas:
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)
public void updaterating(List<Opportunity> argOpportunities) {
	Map<ID, Account> lIDToAccountMap = new Map<ID, Account>();
	
	for(Opportunity lOpp : argOpportunities) {
		if(lOpp.StageName == 'Closed Won') {
			Account lAccObj = lIDToAccountMap.get(lOpp.AccountId);
			
			if(lAccObj == null) {
				lIDToAccountMap.put(lOpp.AccountId, new Account(id=lOpp.AccountId, Rating = 'Hot'));
			}
		}
	}
	
	if(lIDToAccountMap.size() > 0) {
		upadte lIDToAccountMap.values();
	}
}
BALAJI CHBALAJI CH
Hi 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.
public class updateaccountrating
{
    public void updaterating(List<Opportunity> oppor)
    {
        Map<Id,Account> accountMap = new Map<Id,Account>();
        for(Opportunity o : oppor){
            
            if(o.StageName == 'Closed Won'){
                accountMap.put(o.Id, new Account(Id=o.AccountId, rating='Hot'));
            }
        }
        //Update all the appropriate accounts in a bulkified manner here.
        if(accountMap.size() > 0) 
        {
            update accountMap.values();
        }
        
    }
}

Please let me know if that helps you.

Best Regards,
BALAJI
 
This was selected as the best answer
pooja biswaspooja biswas
Hi

I did not understand this statement.
Account lAccObj = lIDToAccountMap.get(lOpp.AccountId);
why is lAccObj NULL?

 
pooja biswaspooja biswas
Hi
Thanks for info.
I am clear now.
This resolves my issue.