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
Eric Anderson 54Eric Anderson 54 

Can a trigger be executed from Attachments in custom objects

Hi there,

I have a custom (parent) object that has two child objects related to it. I have also allowed (Notes &) 'Attachements' to be enabled for the (parent) custom object. One of the Child objects tracks 'Time' invovled in the particular entry in the (Parent) custom object. The other child object tracks 'Suspects' who are involved in a particular entry of the (Parent) custom object.The user has requested that when a 'Suspect' is entered into child object which are related to the parent object, that a entry be made into the 'Time' child object. Similarly, they have requested that whenever an 'Attachment' is added to the (Parent) custom object, that  another entry be made into the 'Time' child object.

I've tried both the 'Before Insert' and 'After Insert' type of trigger for the Attachments, and neither one seems to execute.
 
trigger Attachement_After_Add_Del on Attachment (before insert) {
    List<Request_Time_Entry__c> entryList = new List<Request_Time_Entry__c>();
    for(Attachment AttachmentObj : Trigger.new){
    system.debug('The Attachment Trigger fired.');
    Request_Time_Entry__c entry = new Request_Time_Entry__c();
      entry.Hours_Worked__c = '0';
      entry.Minutes_Worked__c = '15';
      string ParentID = AttachmentObj.parentId;
      entry.Work_Description__c = 'Added Suspect';
      entry.Name = ParentID.substring(0, 15);
      entry.Date_Worked__C = system.Today();
      entry.Related_Object__c = ParentID.substring(0, 15);
      entryList.add(entry);
     }
     if(entryList.size()>0){
      insert entryList;
     }
}


trigger Suspect_After_Add on Suspect__c (after insert) {
    List<Request_Time_Entry__c> entryList = new List<Request_Time_Entry__c>();
    for(Suspect__c SuspectObj : Trigger.new){
    system.debug('The Suspect Trigger fired.');
    Request_Time_Entry__c entry = new Request_Time_Entry__c();
      entry.Hours_Worked__c = '0';
      entry.Minutes_Worked__c = '15';
      string ParentID = SuspectObj.Investigation__c;
      entry.Work_Description__c = 'Added Suspect';
      entry.Name = ParentID.substring(0, 15);
      entry.Date_Worked__C = system.Today();
      entry.Related_Object__c = ParentID.substring(0, 15);
      entryList.add(entry);
     }
     if(entryList.size()>0){
      insert entryList;
     }
}



My 'Suspects' trigger works just fine. I have a 'System.debug' statement in it and it is showing up in my log just fine, including the entry being made in the 'Time' Child object.

My Attachment trigger does not work as best I can tell. There is no entry in the 'Time' Child object, and there is no entry in the Logs either. 

Does anyone have any insight for me on what the problem might be? Is there some obscure setting somewhere that I need to look for that will allow 'Attachments' of Custom objects to fire triggers?

I'd welcome any input I can get on this subject.

Thanks!

Eric Anderson
 
Kai Herng LauKai Herng Lau
Hi Eric,

Are you able to see the debug log for your attachment trigger? If yes, can you try to put in more debug to find out in which line is not working
Eric Anderson 54Eric Anderson 54
Kai,

I CAN see the 'Debug Log' for the Suspects child object, but I 'CAN NOT' see any log for the 'Attachment' trigger.

Is there something I need to do to get 'Attachements' to show up on the Debug log?

Let me know. Thanks!

- Eric -
Kai Herng LauKai Herng Lau
Hi Eric,

May I know your tirgger point is in Suspect object or Attachment object? Can you try to do a quick testing, to insert an attachment into the parent record to check if the 'Debug Log' is running.

 
Eric Anderson 54Eric Anderson 54
Kal,

The trigger is on the 'Attachement' object. I have tried uploading a file through the 'Notes & Attachements' related list as well as the 'Files' related list, and neither causes the trigger to fire. I know that the logs are running because with a trigger on the 'Attachements' of the parent object and a trigger on a child object called suspects  (of the same parent) I can do the following workflow. 

- Add row to Suspect child object
- Add attachement to the 'Notes & Attachements' child object
- Add row to the Suspect child object

I only see two trigger firings in the log, both are to the 'Suspect' child object. There is no indication in the log that the 'Notes & Attachements' related list is causing it's trigger to fire.   

Please let me know if you have any suggestions.

Thanks!