+ Start a Discussion
Mike SimmenMike Simmen 

When Opportunity stage is updated, an Account field is updated

I am somewhat new to Apex Triggers, but I have been working on a way to update the account associated with an opportunity when the stage is updated and a second requirement is met, the closed date (standard opportunity field) is greater than the prospecting date (custom account field).  Here is my code.  The problem is nothing happens at all and I don't see why.  Could you please help.  Thanks.

trigger updateAccountProspectStatus on opportunity (after insert, after update){
    list<Id> accIds = new list<Id>();
    list<Account> accounts = new list<account>();
    for(opportunity o:trigger.new){
        accIds.add(o.accountId);
    }
    for(account a:[select Id, Prospecting_Date__c, Prospecting_Status__c, Prospecting_Target__c from account where Id IN :accIds]){
        for(opportunity opp:trigger.new){
            if(opp.CloseDate >a.Prospecting_Date__c){
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Aircraft Info Sent'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Pending'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Product Presentation Complete-L450/L500'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Completed'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Business Points Negotiated'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Proposal Submitted'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Won'){
                   a.Prospecting_Status__c = 'Opportunity Closed Won';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Lost'){
                   a.Prospecting_Status__c = 'Opportunity Closed Lost';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Drop'){
                   a.Prospecting_Status__c = 'Opportunity Closed Drop';
                   a.Prospecting_Target__c = FALSE;
                }
            }else{
               a.Prospecting_Status__c = NULL;
    }
    }
    }
    update accounts;
}
Best Answer chosen by Mike Simmen
Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because you are updating an empty accounts list.

Try with below code hope it will help you.

trigger updateAccountProspectStatus on opportunity (after insert, after update){
    list<Id> accIds = new list<Id>();
    list<Account> accounts = new list<account>();
    for(opportunity o:trigger.new){
        accIds.add(o.accountId);
    }
    for(account a:[select Id, Prospecting_Date__c, Prospecting_Status__c, Prospecting_Target__c from account where Id IN :accIds]){
        for(opportunity opp:trigger.new){
            if(opp.CloseDate >a.Prospecting_Date__c){
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Aircraft Info Sent'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Pending'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Product Presentation Complete-L450/L500'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Completed'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Business Points Negotiated'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Proposal Submitted'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Won'){
                   a.Prospecting_Status__c = 'Opportunity Closed Won';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Lost'){
                   a.Prospecting_Status__c = 'Opportunity Closed Lost';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Drop'){
                   a.Prospecting_Status__c = 'Opportunity Closed Drop';
                   a.Prospecting_Target__c = FALSE;
                }
            }else{
               a.Prospecting_Status__c = NULL;
			}
		accounts.add(a) ;
    }
    }
    update accounts;
}

Please mark it as best solution to your problem if it does solve your problem.

All Answers

AshwaniAshwani
Can you check in debug logs if trigger is being fired on not.
Deepak Kumar ShyoranDeepak Kumar Shyoran
This is because you are updating an empty accounts list.

Try with below code hope it will help you.

trigger updateAccountProspectStatus on opportunity (after insert, after update){
    list<Id> accIds = new list<Id>();
    list<Account> accounts = new list<account>();
    for(opportunity o:trigger.new){
        accIds.add(o.accountId);
    }
    for(account a:[select Id, Prospecting_Date__c, Prospecting_Status__c, Prospecting_Target__c from account where Id IN :accIds]){
        for(opportunity opp:trigger.new){
            if(opp.CloseDate >a.Prospecting_Date__c){
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Aircraft Info Sent'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Qualification/Interest'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Pending'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Product Presentation Complete-L450/L500'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Demo Completed'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Business Points Negotiated'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Proposal Submitted'){
                   a.Prospecting_Status__c = 'Opportunity Open';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Won'){
                   a.Prospecting_Status__c = 'Opportunity Closed Won';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Lost'){
                   a.Prospecting_Status__c = 'Opportunity Closed Lost';
                   a.Prospecting_Target__c = FALSE;
                }
                if(opp.StageName == 'Closed Drop'){
                   a.Prospecting_Status__c = 'Opportunity Closed Drop';
                   a.Prospecting_Target__c = FALSE;
                }
            }else{
               a.Prospecting_Status__c = NULL;
			}
		accounts.add(a) ;
    }
    }
    update accounts;
}

Please mark it as best solution to your problem if it does solve your problem.
This was selected as the best answer
kaustav goswamikaustav goswami
Important : PLEASE note that in your code you have not checked whether the opportunity you are dealing with is actually a child of the account record that you have. That parent to child checking is important otherwise you will end up in updating a wrong account.

There has to be a check like this - 

if(opp.AccountId == a.Id){
      // then proceed with other validations.
}

If you want to minimize the number of iterations then you can use a map in stead of iterating over two lists. That will make your code more efficient.

And of course as mentioned by Deepak please use the list of accounts to add the records and then update that list.

Please let me know if this helps or I can provide you with a sample code that will have these implemented.

Thanks,
Kaustav