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
Dharmesh MaheshwariDharmesh Maheshwari 

I have a custom object name as Project__c. On that object there is a checkbox. I want to do automatically checked as soon as an attachment with the word WBS in it is attached. Below is My Code But It Shows Error : Variable does not exist 'name'. Line 9


trigger WBSAttachment on Project__c (before insert, before update) 
{
    
    
    Project__c[] lstProject = [SELECT Id, WBS_Attached__c, (SELECT Id, Name, ContentType FROM Attachments)  FROM Project__c where id IN :Trigger.newMap.keySet()];
    for(Project__c objProject : lstProject)
    {
        Attachment[] lstAttc = objProject.Attachments;
        if(lstAttc.size()>0 && objProject.Attachments.Name.contains('WBS'))
        {
            System.debug('Need to set Syllabus Attached to true for Opportunity Id: ' + objProject.id);
            System.debug('just testing this: ' + Trigger.newMap.get(objProject.Id).Id);
            
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = true;
        }
        else
        {
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = false;
        }                  
    }
    
}
Best Answer chosen by Dharmesh Maheshwari
JayantJayant
You are trying to retrieve the Name from list rather than a record. You should iterate over the attachment list and check the Name field on individual attachment records.

Replace following - 
Attachment[] lstAttc = objProject.Attachments;
        if(lstAttc.size()>0 && objProject.Attachments.Name.contains('WBS'))
        {
            System.debug('Need to set Syllabus Attached to true for Opportunity Id: ' + objProject.id);
            System.debug('just testing this: ' + Trigger.newMap.get(objProject.Id).Id);
            
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = true;
        }
        else
        {
            Trigger.newMap.get(objProject.Id).WBS_Attached__c = false;
        }  


with - 
for(Attachment a : objProject.Attachments) {
    if(a.Name.contains('WBS') ) {
        Trigger.newMap.get(objProject.Id).WBS_Attached__c = true;
    }
    else {   
      Trigger.newMap.get(objProject.Id).WBS_Attached__c = false;
    }
}


See if you can reduce the number of records being evaluated for e.g. maybe querying only Projects that have WBS_Attached__c = false.
You may also want to evaluate if it would make more sense to have the trigger on Attachment object rather than Project__c.