You need to sign in to do that
Don't have an account?

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.
Thanks.
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.
Try Below code,
Hope This Help, Let me know in case of any doubt
--
Thanks,
Swayam
@salesforceguy
It was due to typo :( , Updated Code
Hope This Help, Let me know in case of any doubt
--
Thanks,
Swayam
@salesforceguy
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"
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 ,
Hope, It will work
--
Thanks,
Swayam
@salesforceguy
I don't understand. Where is the system.trigger.old?
Thanks for your help
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
Hope, This helps,
--
Thanks,
Swayam
@salesforceguy
I mean, I can't find the system.trigger old on your code.
Thanks for the help.
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"
Any ideas
thanks