+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Update Account Type field when Opportunity is approved

Hi, 

   I wrote a workflow to update account type when opportunity approval is approved ( approval in opportunity is tracled through a flag called status) but this is failing please suggest me I even wrote a trigger to update this is not firing when approval is approved when edited it is working 

 
trigger Account_Type_Update on Opportunity (After Update) 
{

Public List<Opportunity> Optys = New List<Opportunity>();
Public List<Account> Acct = new List<Account>(); 

  Optys = [SELECT Id,AccountID,Account.ID,Account.RecordTypeID, RecordType.name,RecordTypeID,Is_Approved__c 
            FROM Opportunity
            WHERE Id = :Trigger.newMap.keySet() ]; 
  
  
  for ( Opportunity  Opp : Optys )
   {
     if ( (Opp.RecordTypeID == '012250000008i5uAAA' || Opp.RecordTypeID ==  '01225000000ChlOAAS' || Opp.RecordTypeID == '01225000000ChlEAAS' ) && Opp.Is_Approved__c == True)
      {
        Acct = [SELECT RecordTypeID,Type FROM Account WHERE ID = :Opp.AccountID ]; 
        
          for ( Account Act : Acct )
           {
             Act.Type = 'Client';
             
             Update Act;
             }           
             
      
       }
     
     
   
     } 
   



}

Please suggest me 

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
ClintLeeClintLee
You have a few issues in this one.

1) The trigger is not bulkified.  You have SOQL and DML statements in a loop.
2) Your for-loop is probably not executing because you can't loop through a single object.

Try this:
 
trigger Account_Type_Update on Opportunity (After Update) 
{
    public List<Account> accountsToUpdate = new List<Account>(); 

    for(Opportunity opp : Trigger.New)
    {
        if ( (opp.RecordTypeID == '012250000008i5uAAA' || opp.RecordTypeID ==  '01225000000ChlOAAS' || opp.RecordTypeID == '01225000000ChlEAAS' ) && opp.Is_Approved__c)
        {
            Account acct = new Account(Id = opp.AccountId, Type = 'Client');
            accountsToUpdate.add(acct);
        }
    }

        update accountsToUpdate;
}

Hope that helps,

Clint
 

All Answers

ClintLeeClintLee
You have a few issues in this one.

1) The trigger is not bulkified.  You have SOQL and DML statements in a loop.
2) Your for-loop is probably not executing because you can't loop through a single object.

Try this:
 
trigger Account_Type_Update on Opportunity (After Update) 
{
    public List<Account> accountsToUpdate = new List<Account>(); 

    for(Opportunity opp : Trigger.New)
    {
        if ( (opp.RecordTypeID == '012250000008i5uAAA' || opp.RecordTypeID ==  '01225000000ChlOAAS' || opp.RecordTypeID == '01225000000ChlEAAS' ) && opp.Is_Approved__c)
        {
            Account acct = new Account(Id = opp.AccountId, Type = 'Client');
            accountsToUpdate.add(acct);
        }
    }

        update accountsToUpdate;
}

Hope that helps,

Clint
 
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Clint 

  Thans for your reply tried your method its not working any other suggestion


Thanks
Sudhir
ClintLeeClintLee
When you say 'not working' do you mean that the Account is not being updated or is it something else?

You've verified that the Opportunity you are updating has one of the Record Type Ids in your if-statement and that the Is_Approved__c field is checked?
sudhirn@merunetworks.comsudhirn@merunetworks.com
Its working without record type condition