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
Tapas TuloTapas Tulo 

An account has many opportunity,there is a PickList field on opportunnity called status(value-approved,rejected,hold).

An account has many opportunity,there is a PickList field on opportunnity  called status(value-approved,rejected,hold).

 Scenario -
  whenever one opportunity status get updated to APPROVED other should be rejected.
  
   Here My Code-
    Apex Class:-
      public class AccountOpty {
  
  List<Account> acctToUpdate = new List<Account>();
  List<Opportunity> opps = new List<Opportunity>(); 
  Set<Id> associatedAccId = new Set<Id>() ;
    public static void findOpty(list<Opportunity> oppList){
        for(opportunity opp:oppList){
        associatedAccId.add(opp.AccountId) ;
        }
       //opps=[select id,AccountId From Opportunity ];
    }//End OF findOpty ethod

}//End Of Class

My Trigger:-
trigger AccountOptyAfterTrigger on Opportunity (after Update) {
    AccountOpty.findOpty(trigger.new);

         }
after executing my apex class am getting the below Error

Error: Compile Error: Variable does not exist: AccountId at line 8 column 29
BALAJI CHBALAJI CH
Hi Tapas Tulo,

We cannot use outside declared variables in Statux classes. Either the variable should be declared as Static or it should be declared inside the Static method.
Please find below modified Apex Class:
public class AccountOpty {
  
  List<Account> acctToUpdate = new List<Account>();
  List<Opportunity> opps = new List<Opportunity>(); 
  
   public static void findOpty(list<Opportunity> oppList){
        Set<Id> associatedAccId = new Set<Id>() ;
        for(opportunity opp:oppList){
        associatedAccId.add(opp.AccountId) ;
        }
       //opps=[select id,AccountId From Opportunity ];
    }//End OF findOpty ethod

}//End Of Class

OR
 
public class AccountOpty {
  
  List<Account> acctToUpdate = new List<Account>();
  List<Opportunity> opps = new List<Opportunity>(); 
  public static Set<Id> associatedAccId = new Set<Id>() ;
   
   public static void findOpty(list<Opportunity> oppList){
        for(opportunity opp:oppList){
        associatedAccId.add(opp.AccountId) ;
        }
       //opps=[select id,AccountId From Opportunity ];
    }//End OF findOpty ethod

}//End Of Class

Let us know if that helps you.

Best Regards,
BALAJI
Tapas TuloTapas Tulo
hi BALAJI CH,
  
    Thank You For quick Your Responce.
   after i execute Your Code am getting the below Error(almost same as mine).
    Error: Compile Error: Variable does not exist: AccountId at line 9 column 29 
BALAJI CHBALAJI CH
Seems Strange. AccountId is the Standard field in Opportunity.
Are you running the code as System Administrator profile ?
Tapas TuloTapas Tulo
NO stage is a custom field and yes i am on system adminstration Profile.
BALAJI CHBALAJI CH
Okay, Stage is a Custome Field.
But AccountId is a Standard Field in Opportunity. 

Can you try this at line 9
associatedAccId.add(opp.Account.Id) ;

 
yogesh_sharmayogesh_sharma

Hi Tapas,

Try this one:

Trigger:

trigger AccountOptyAfterTrigger on Opportunity (After Update){
     //Trigger will get fired on after Update
   if(trigger.isAfter && trigger.isUpdate) {

        list<Opportunity> lstOpp= new list<Opportuntiy>();

        for(Opportunity objOpp : Trigger.New){
            Opportunity objOldOpportuntiy = Trigger.OldMap.get(objOpp.Id);
            if((objOpp.Status != objOldOpportuntiy.Status) &&
                                          (objOpp == 'Approved')){
                lstOpp.add(objOpp);
            }// End If
         }// End For

        if(lstOpp.size()>0 && lstOpp!=null){
            AccountOpty.findOpty(lstOpp);
        }//End If

   }// End If
}// End AccountOptyAfterTrigger


Handler::::


List<Account> acctToUpdate = new List<Account>();
  List<Opportunity> opps = new List<Opportunity>(); 
  Set<Id> associatedAccId = new Set<Id>() ;
  set<Id> setOppId = new set<Id>();
   
 public static void findOpty(list<Opportunity> oppList){
        for(opportunity opp:oppList){
        associatedAccId.add(opp.AccountId) ;
        setOppId.add(opp.Id);
        }
      
for(Opportuntiy objOpp : [select Id, Stage from Opportuntiy where AccountID IN : associatedAccId and ID Not IN : setOppId]){
         objOpp.status = 'Rejected';   
}

try{
    if(objOpp!=null && objOpp.size()>0)
     update objOpp;
}catch(DMLException){
   
}
    }//End OF findOpty ethod

}//End Of Class


I hope it helps you. Please mark this as best answer if it helps you, so that it would be helpful ton others.

Thanks,

Yogesh Sharma