1

All:

I want my trigger to copy Attachments (File) uploaded to Account`s chatter feed and re-post it to a group. For some reason, code below is failing. Can someone pls suggest why this is happening? I have also tried using after trigger but getting same error.

Error:

Apex trigger ContentPostTest caused an unexpected exception, contact your administrator: ContentPostTest: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, ContentData is a required field for feed posts of type ContentPost: Content Data: [ContentData]: Trigger.ContentPostTest: line 46, column 1

trigger ContentPostTest on FeedItem (after insert) {

List <FeedItem> Feedlist = New List <FeedItem> ();

 Map <id,Account> AccMaps = New Map <id, Account> () ;
Set <Id> idsets = New Set <Id> ();

      For (Feeditem fe : Trigger.New) {
          String idStr = fe.Parentid;
           if(idStr.startsWith('001')){
                     idsets.add(idStr);    


      }
    }

       List <Account> accs = [SELECT ID, Owner.Name, RecordTypeid, Name FROM Account WHERE id IN :idsets ];

           for (Account  acc : accs) {

            AccMaps.put(acc.id,acc);      




        // Map <id,ContentDocumentFeed> NFMaps = New Map <id, ContentDocumentFeed> ([SELECT ID, ContentFileName, ContentData FROM ContentDocumentFeed]);


                   for (Feeditem f : Trigger.New) {

                   if (AccMaps.get(f.parentid).Recordtypeid == '012300000000tTr') {  

            FeedItem FI = New FeedItem ();
            Fi.Type = 'ContentPost';
            FI.ParentId = '0F91b0000008TxyCAU' ; 
            FI.Body = F.Body;
         Fi.ContentData = F.ContentData ;
        Fi.ContentFileName = F.ContentFileName;
           Fi.CreatedByid = UserInfo.getUserId() ;
            Feedlist.add(FI);


        }
        }
        }
         insert Feedlist;  

}
0

Do you need to duplicate the file when posting to the group, or can you share the existing file? Sharing the existing file would ensure the group and account always reference the latest version, and it would decrease storage usage.

If sharing is okay, I would suggest reading the RelatedRecordId from the feeditem posted to the account, and populating this field in the feedItem you're posting to a group. When you use this field, you wouldn't specify the contentData or contentFileName fields.

Note that the RelatedRecordId is for a ContentVersion (id 068...) not ContentDocument (069...).

FeedItem

If you need to copy, instead of share the file to the group, you can try reading the versionData field on ContentVersion to get the bytes, and using that to populate contentData on feeditem.

1

Your trigger is processing feed items of all types rather than only working on ones of type "ContentPost" that will have ContentData set to a non-null value.

Add this extra if:

for (Feeditem f : Trigger.New) {
    if (f.Type == 'ContentPost') {
        if (AccMaps.get(f.parentid).Recordtypeid == '012300000000tTr') {
            ...
        }
    }
}
  • I tried adding an extra "if" statement, but still getting same error. Code works OK when new file is uploaded/posted on account chatter feed but doesn't work on existing files. – Che SFDC Nov 6 '15 at 9:18
  • @CheSFDC Weird as the documentation says about ContentData "The file uploaded to the feed. Required if Type is ContentPost. Encoded file data. Cannot be 0 bytes. Can be any format. Setting this field automatically sets Type to ContentPost.". – Keith C Nov 6 '15 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.