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
Arnold BrownArnold Brown 

Getting Variable does not exist two lines after creating a list variable. Argh.

I just want to iterate through the list I create and set a field stating whether there are attachments on the object to no  when conditions are met via a before delete trigger.  I'm getitng variable does not exist


if(parentObjId.startsWith('a0x')&& Trigger.isdelete)
             List<CRM_Outreach__c> CRMNoList = [select id, Attachment_Added__c from CRM_Outreach__c where id =: Trigger.New[0].ParentId];  
             // query to see if there are still attachments under the object
             // If there were less than two, i.e. 1 attachment, then there will be zero after the after delete trigger.
           if(CRMNolist.size()<2){
               for (CRM_Outreach__c CRM : CRMNoList)
             {
                   CRM.Attachment_Added__c = 'No';

Best Answer chosen by Arnold Brown
Head In CloudHead In Cloud
Hi Arnold,

You are getting this error because the line where you created the list variable is inside the "IF" statement, so it is a local variable. You can overcome this by using the curley braces "{ }" like this:
f(parentObjId.startsWith('a0x')&& Trigger.isdelete){
             List<CRM_Outreach__c> CRMNoList = [select id, Attachment_Added__c from             CRM_Outreach__c where id =: Trigger.New[0].ParentId];  
             // query to see if there are still attachments under the object
             // If there were less than two, i.e. 1 attachment, then there will be zero after the after delete trigger.
           if(CRMNolist.size()<2){
               for (CRM_Outreach__c CRM : CRMNoList)
             {
                   CRM.Attachment_Added__c = 'No';

.......

}