You need to sign in to do that
Don't have an account?
Maximum Depth error
Hi Folks I am getitng Maximum Depth error and I am new to apex..please help me
When ever Opportunity stage is changed to ‘closed won’ update Account custom field closed to TRUE and change stages of all other Opportunity related to same account to ‘Closed Lost’.
trigger UpdateAccOnOppAll on Opportunity(after insert, after update) {
if (triggerCount.fla ==TRUE)
{
Opportunity opp=trigger.new[0];
List<account> acc = new List<account>();
acc =[SELECT Id, Name from account where id= :opp.accountid];
List< Opportunity> ALlOpp = new List<Opportunity>();
List<Opportunity> OppAll= new List<Opportunity>();
OppAll=[select id, Accountid, Name from Opportunity where accountid IN :Acc AND id != :trigger.new[0].id and StageName <>'Closed Won'];
List<account> AccountToUpdate=new List<account>();
List<Opportunity> OpportunityToUpdate=new List<Opportunity>();
for (Opportunity OppyuToUpdate : trigger.new)
{
If (OppyuToUpdate.StageName=='Closed Won' )
{
for (account accountInList:acc)
{
accountInList.closed__c= TRUE;
AccountToUpdate.add(accountInList);
}
}
}
for (Account AccToOpp : acc)
{
for (Opportunity OpptyAll:OppAll)
{
if( OpptyAll.id != trigger.new[0].id)
OpptyAll.StageName= 'Closed Lost';
OpportunityToUpdate.add(OpptyAll);
}
}
if(AccountToUpdate.size()>0)
Database.update(AccountToUpdate);
if(OpportunityToUpdate.size()>0)
Database.update(OpportunityToUpdate);
TriggerCount.fla=False;
}
}
This error usually means that your updates to the opportunities cause further updates to the same opportunities ad infinitum.
Looking at your code, I think you can check the account to see if the Closed__c field is set to true - if it is, that means you have already processed the opportunities so there is no need to continue.
However, you have some additional problems with your trigger:
(1) It only looks at the account on the first record of trigger.new, so if you update multiple records through the data loader, for example, only the first will be processed.
(2) You pull back all opportunities for the account but then iterate trigger.new - this will contain opportunities from multiple accounts
Thanks a lot Bob, I cna undertand that but this part from code is not working..where I want to update all opportunity with 'Closed Lost'' except those which i updated as closed won for same acocunt
for (Account AccToOpp : acc)
{
for (Opportunity OpptyAll:OppAll)
{
if( OpptyAll.id != trigger.new[0].id)
OpptyAll.StageName= 'Closed Lost';
OpportunityToUpdate.add(OpptyAll);
}
}