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
mng0827mng0827 

Variable does not exist: AccountIds

Hi,

Can someone help me with this trigger? I'm getting this error upon saving it:

Error: Compile Error: Variable does not exist: AccountIds at line 9 column 13

Here's my trigger:

trigger updatePCFPMPRopp on Master_Tracker__c (after insert) {
    Master_Tracker__c mt = trigger.new[0];
   
        if(mt.Product_Code__c == 'PCFP' && (mt.GLAS_Offer_Status__c == 'Initiated' || mt.GLAS_Offer_Status__c == 'Offer in Review'))

        List<Id> AccountIds = new List<Id>();
        for (Integer i=0;i<Trigger.new.size();i++){
        {
            AccountIds.add(Trigger.new[i].Account_Name__c);
        }
        }
       
        List<Opportunity> OppList = new List<Opportunity>([SELECT Id,AccountId FROM Opportunity WHERE RecordTypeId = '012d0000000go7t' and Closed = false]);
            for (Opportunity opp: OppList){
            opp.StageName = 'Closed Won'; }
       
        update OppList;
}
Jim JamJim Jam
You're missing a { at end of your if statement before declaring AccountIds.
kaustav goswamikaustav goswami
Hi,

Declare the list called AccountIds at the very top. So that it is availble to the rest of the code.

The opening and closing braces are incorrect casuing a scope problem.

The final update takes place on a list of opportunities that has got nothing to do with the list of Account Ids that you have prepared. It is missing from the query.

If required please specify the entire logic that you are trying to implement. We can then have a logiccaly correct working solution.

For the time being please take a look at this code.

trigger updatePCFPMPRopp on Master_Tracker__c (after insert) {
    Master_Tracker__c mt = trigger.new[0];
List<Id> AccountIds = new List<Id>();
        if(mt.Product_Code__c == 'PCFP' && (mt.GLAS_Offer_Status__c == 'Initiated' || mt.GLAS_Offer_Status__c == 'Offer in Review')){
   for (Integer i=0;i<Trigger.new.size();i++){
    AccountIds.add(Trigger.new[i].Account_Name__c);
   }
  }
  // ---- here you should check if account id list has some values then include that in the query or some other logic
  // there is no relation between this portion of the code and the previous lines
        List<Opportunity> OppList = new List<Opportunity>([SELECT Id,AccountId FROM Opportunity WHERE RecordTypeId = '012d0000000go7t' and Closed = false]);
  for (Opportunity opp: OppList){
   opp.StageName = 'Closed Won';
  }
      
    update OppList;
}
mng0827mng0827
Thanks guys. I tried both suggestions and it allowed me to save the trigger. However, the trigger doesn't work. The opportunity should update to 'Closed Won' when the Master_Tracker__c record is created. The opportunity and custom object are related to the account. Can you help me with this?
Avidev9Avidev9
You can improve the code a bit something like

trigger updatePCFPMPRopp on Master_Tracker__c(after insert) {

    List < Id > AccountIds = new List < Id > ();

    for (Integer i = 0; i < Trigger.new.size(); i++) {
        Master_Tracker__c mt = trigger.new[i];
        if (mt.Product_Code__c == 'PCFP' && (mt.GLAS_Offer_Status__c == 'Initiated' || mt.GLAS_Offer_Status__c == 'Offer in Review')) {
            AccountIds.add(mt.Account_Name__c);
        }
    }


    List < Opportunity > OppList = new List < Opportunity > ([SELECT Id, AccountId FROM Opportunity WHERE RecordTypeId = '012d0000000go7t'
        and isClosed = false AND AccountId IN: AccountIds
    ]);
    for (Opportunity opp: OppList) {
        opp.StageName = 'Closed Won';
    }

    update OppList;
}
And probably you can consider using RecordType.DeveloperName instead of Id to filter the query ?