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
salesforce_hoonigansalesforce_hoonigan 

Trigger on attachment updates the Parent Record

Hi Experts,

I need your assistance updating TIN_Uploaded__c (Checkbox) on Lead if the Attachment (Notes and Attachment) Name starts with "TIN". This box should be unchecked if the Attachment is deleted. Also I have another box that will do the same behavior SLA_Uploaded__c but I don't know how to nest them in the code. I've tried building the code but I'm getting error "Variable does not exist: attach.Name" in Line 14.
 
trigger TestAttachmentCheck on Attachment (after insert, after update, after delete, after undelete) {
    List <Lead> LeadList = new List<Lead>();
    Set <Id> LeadIds = new Set <Id>();
    
    for(Attachment attach : trigger.New){
         //Check if added attachment is related to Lead or not
         if(attach.ParentId.getSobjectType() == Lead.SobjectType){
              LeadIds.add(attach.ParentId);
         }
    }
    LeadList = [select id, CIN_Uploaded__c from Lead where id in : LeadIds];
   	for(Lead lead : LeadList){    
    	if((string.valueOf(attach.Name)).startswith('.xls')){
            lead.CIN_Uploaded__c  = true;
        }
        else{
            lead.CIN_Uploaded__c = false;
        }
    }
        update LeadList;
}
I would appreciate any help.

Thanks.
 
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Try Below code,
trigger TestAttachmentCheck on Attachment (after insert, after update, after delete, after undelete) {
    List <Lead> LeadList = new List<Lead>();
	 List <Lead> LeadListToUdpdate = new List<Lead>();
    Set <Id> LeadIds = new Set <Id>();
    
    for(Attachment attach : trigger.New){
         //Check if added attachment is related to Lead or not
         if(attach.ParentId.getSobjectType() == Lead.SobjectType){
		    if(string.valueOf(attach.Name)).startswith('.xls'))
               LeadIds.add(attach.ParentId);
         }
    }
	
	
    LeadList = [select id, CIN_Uploaded__c from Lead where id in : LeadIds];
   	for(Lead lead : LeadList){    
  	
		   Lead leadToUpdate = new leadToUpdate();
		   leadToUpdate.Id = lead.Id;
		   leadToUpdate.CIN_Uploaded__c  = true;
		   LeadListToUdpdate.add(leadToUpdate);
				
	}
       
        update LeadListToUdpdate;
}

Hope This Help, Let me know in case of any doubt

--
Thanks,
Swayam
@salesforceguy
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

It was due to typo :( , Updated Code 
trigger TestAttachmentCheck on Attachment (after insert, after update, after delete, after undelete) {
    List <Lead> LeadList = new List<Lead>();
	 List <Lead> LeadListToUdpdate = new List<Lead>();
    Set <Id> LeadIds = new Set <Id>();
    
    for(Attachment attach : trigger.New){
         //Check if added attachment is related to Lead or not
         if(attach.ParentId.getSobjectType() == Lead.SobjectType){
		    if(string.valueOf(attach.Name)).startswith('.xls'))
               LeadIds.add(attach.ParentId);
         }
    }
	
	
    LeadList = [select id, CIN_Uploaded__c from Lead where id in : LeadIds];
   	for(Lead lead : LeadList){    
  	
		   Lead leadToUpdate = new Lead ();
		   leadToUpdate.Id = lead.Id;
		   leadToUpdate.CIN_Uploaded__c  = true;
		   LeadListToUdpdate.add(leadToUpdate);
				
	}
       
        update LeadListToUdpdate;
}


Hope This Help, Let me know in case of any doubt

--
Thanks,
Swayam
@salesforceguy
 
salesforce_hoonigansalesforce_hoonigan
Hi @Swayam,

Doesn't work for me. Its stating upon deletion

"TestAttachmentCheck: execution of AfterDelete
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.TestAttachmentCheck: line 6, column 1"
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey , 

The context variable Trigger.new is only available in "after insert" and "after update" triggers. Check out the "Trigger context variables" section in the Apex Developer's Guide. You probably want to use System.Trigger.old for the "after delete" case

You can use this ,
 
trigger TestAttachmentCheck on Attachment (after insert, after update, after delete, after undelete) {
    List <Lead> LeadList = new List<Lead>();
	 List <Lead> LeadListToUdpdate = new List<Lead>();
    Set <Id> LeadIds = new Set <Id>();
    
	if(trigger.isInsert || trigger.isUpdate)
	{
    for(Attachment attach : trigger.New){
         //Check if added attachment is related to Lead or not
         if(attach.ParentId.getSobjectType() == Lead.SobjectType){
		    if(string.valueOf(attach.Name)).startswith('.xls'))
               LeadIds.add(attach.ParentId);
         }
    }
	}
	
	if(trigger.isDelete || trigger.isUndelete)
	{
    for(Attachment attach : trigger.old){
         //Check if added attachment is related to Lead or not
         if(attach.ParentId.getSobjectType() == Lead.SobjectType){
		    if(string.valueOf(attach.Name)).startswith('.xls'))
               LeadIds.add(attach.ParentId);
         }
    }
	}
	
    LeadList = [select id, CIN_Uploaded__c from Lead where id in : LeadIds];
   	for(Lead lead : LeadList){    
  	
		   Lead leadToUpdate = new leadToUpdate();
		   leadToUpdate.Id = lead.Id;
		   leadToUpdate.CIN_Uploaded__c  = true;
		   LeadListToUdpdate.add(leadToUpdate);
				
	}
       
        update LeadListToUdpdate;
}

Hope, It will work 


--
Thanks,
Swayam
@salesforceguy

 
salesforce_hoonigansalesforce_hoonigan
Hi @Swayam,

I don't understand. Where is the system.trigger.old?

Thanks for your help
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey,

Salesforce provide a way to get the value of existing record in trigger using context variable, In you case you are calling trigger after delete operation, so it does not have any new value, trigger. new will be Null and it throws error, below is sample matrix which show how to use trigger.new, trigger.old based on event
 
Trigger Event   Trigger.New Trigger.Old
Before Insert   Yes         No
Before Update   Yes         Yes
Before Delete   No          Yes
Before UnDelete No          Yes
After Insert    Yes         No
After Update    Yes         Yes
After Delete    No          Yes
Hope, This helps,

--
Thanks,
Swayam 
@salesforceguy
 
salesforce_hoonigansalesforce_hoonigan
Hi Swayam,

I mean, I can't find the system.trigger old on your code.

Thanks for the help.
Swayam@SalesforceGuySwayam@SalesforceGuy
refer line no. 19  in code
Sean NolansSean Nolans
@Swayam@SalesforceGuy

I having been trying to use your example here but I am getting hit with 
"Error: Compile Error: Unexpected token '.'. at line 11 column 44"


User-added image

Any ideas  


thanks