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
Che SFDCChe SFDC 

Help with Feeditem Trigger

All, I was wondering if someone can help me. Below is my trigger which is very simple. It will take Feed item from Account page and will repost it to Chatter group. Trigger works fine for Post and Link, but when I`m trying to attach exisitng File from content library and share it, it generates below error.

Field_Integrity_Exception, ContentFileName is required field for feed post of type ContentPost. Content File Name: [ContentFileName]

Can someone pls suggest a workaround?
trigger autoPostFromAccToGroup on FeedItem (before 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);      

    
        
        
                   for (Feeditem f : Trigger.New) {
                   
                   if (AccMaps.get(f.parentid).Recordtypeid == '012300000000uth') {  
                   
            FeedItem FI = New FeedItem ();
            FI.ParentId = '0F91b0000008ThRCAU' ; 
            FI.TYPE = F.TYPE; 
            Fi.IsRichText = True;
            FI.Body = F.Body;
            Fi.LINKURL = F.LINKURL;
            Fi.TITLE = F.TITLE;
            Fi.ContentFileName = F.ContentFileName;
            Fi.ContentData = F.ContentData ;
            Fi.ContentDescription= F.ContentDescription;
           Fi.CreatedByid = UserInfo.getUserId() ;


            Feedlist.add(FI);

        
        }
        }
        }
         insert Feedlist;  
        
}

 
Jaap BranderhorstJaap Branderhorst
Did you check if f.contentfilename is not null? Try to do this in an after insert trigger instead of a before trigger. General hint: only use before triggers to check complex conditions (in general) and do any DML in after triggers. Not certain if that will help you here but it might.
Che SFDCChe SFDC
Thank you Jaap! I tried. With after insert, I get this error,
"Field_Integrity_Exception, ContentFileName is required field for feed post of type ContentPost. Content File Name: [ContentFileName]". Wierd thing is, I only get this error while referning file from content library. I dont get any error when I upload new file.
Tejpal KumawatTejpal Kumawat
Hello Friend,

Use like this snippt from line 37 to 39 ..
if(Fi.ContentData != null){
    Fi.ContentFileName = F.ContentFileName;
    Fi.ContentData = F.ContentData ;
    Fi.ContentDescription= F.ContentDescription;
}
Regards
Tej Pal Kumawat
Skype : tejpalkumawat1991

If this answers your question mark Best Answer it as solution and then hit Like!
 
Jaap BranderhorstJaap Branderhorst
A content 'file' is not a Chatter file ;-) maybe that's the problem. Regards / met vriendelijke groet, Jaap Branderhorst Mobile: +31610001714 Apologies for typos and abbreviations.
Che SFDCChe SFDC
@ Tejpal Kumawat - Thank you, I tried but still doesnt work. I get the same error. any other suggestions?

@Jaap - Any idea how I can retrieve File content?