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
Ravali Putta 1Ravali Putta 1 

Trigger is not firing on Attachment and ContentDocument objects?

Hi,
My requirement is whenever file or note attched to a record I have post(upload) that file in chatter group. I have written a trigger on Attachment object ,It is working. And also written on ContentDocument object, this is also not firing. I don't know much about ContentDocument object. I have searched for this but I do not get proper solution.

Any helpwould be greatly appreciated....many thanks,

My trigger - same on Attchment and ContentDocument

trigger postattachment on ContentDocument (after insert) {

    Deal_Support_Request__c[] d = [select Name from Deal_Support_Request__c limit 1];
    String obj;
    System.debug('yessssssssssss'+obj);
       if(d.size()>0)
    {
        obj = d[0].id;
        obj=obj.substring(0,2);
        System.debug('');
    }
    for(ContentDocument att: Trigger.New)
        {    String parentObjId = att.ParentId;
         system.debug('------------entered for---------');
            if(parentObjId.startsWith(obj))
            {
                system.debug('------------entered if---------');
                FeedItem post = new FeedItem();
                post.ParentId = att.ParentId; //eg. Opportunity id, custom object id..
                post.Body = 'Attachment added';
                insert post;
                System.debug('post-------->'+post);
                FeedAttachment fatt = new FeedAttachment();
                fatt.FeedEntityId = post.id;
                fatt.RecordId = att.id;
                fatt.Title=att.Title;
                fatt.Type='Content';
               
insert fatt;
              
                
               
            }
        }

}
Anuj PatelAnuj Patel
Hey,

I faced the issue when I was working on the trigger and testing it in Classic.
I will give you the scenario when Content Trigger will be fired.
  • When you will post attachment on the record's Feed in Salesforce Classic 
  • When you will attach a file through related component of File in Lightning to a record

Now, What I am assuming would have happened with your case is, you tried attaching document using new attachment button on the record related list in salesforce classic. 

You can post a file through record's feed -- that will show up in the related list as File and if you will attach a document through New Attachment then it will appear as an attachement type in Related list.

So, Test the trigger with scenarios I have sent you. 
Please let me know if you need further help. 
Select as best answer if it helps.

Thanks,

Anuj.