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
Sam LauSam Lau 

Attachment Trigger before delete to update record

Hi All,

I'm trying create a deletion trigger where I need to update two record attributes with attachment name and size.  I'm still getting use to APEX, and had come across a few examples to help me with the following trigger.   We're still utilizing the Attachment object as well.   Any assistance would be appreciated.   Currently I'm getting many errors on the SOQL query for TaskLst which I can't figure out at the moment.   Thanks!

Sam

trigger AttachmentHCLTask on Attachment (before delete) 

    String tempParentId;
    String tempAttName;
    Integer tempAttSize;
    Set<Id> setParentId = new Set<Id>();
    List<BMCServiceDesk__Task__c> Tasklst = new List<BMCServiceDesk__Task__c>();
    
 for (Attachment HCLTask : trigger.new ) {
            tempParentId = HCLTask.Id;
             tempAttName = HCLTask.Name;
             tempAttSize = HCLTask.BodyLength;
     
            if (tempParentId.left(10) =='a290H00000') {
                System.debug('Debug : found a290H00000');
                System.debug('Debug : Attachment id ' + HCLTask.Id );
                setParentId.add(HCLTask.Id);
            }
        }
    Tasklst = [select Id from BMCServiceDesk__Task__c where Id = setParentId AND HCL_Task_Creation_Sent__c = true];
     
     For(BMCServiceDesk__Task__c e : Tasklst)
     {
        e.HCL_Attachment_Remove_File_Name__c = tempAttName;
        e.HCL_Attachment_Remove_File_Size__c = tempAttSize;
     }

     update Tasklst;
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sam,

Can you change your SOQL query as below and check.

you are query the id of the task object based on Atttachmentid. I dont think it is correct. What is the relation between Task object and Attachment object?
Tasklst = [select Id,HCL_Task_Creation_Sent__c from BMCServiceDesk__Task__c where Id  in :setParentId AND HCL_Task_Creation_Sent__c = true];

Thanks,
 
Sam LauSam Lau
That helped clear the items within the Problems tab, so thanks Sai for the help with the SOQL.  I've performed some testing after correcting and found that the logs are only captured when I append an attachment file to the Task (non-native Task) record.   When I delete the attachment from the Task related list, no logs appear in addition to the desired field updates.  I also tried switching Before Delete to After Delete which did not make any difference.  I'm wondering if I'm missing something major within this deletion trigger.