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;
}