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
Sangeet kaseraSangeet kasera 

How to insert a feedItem with attachment linked by using RelatedRecordId in FeedItem

Hello Everyone,

I have a requirment to insert  a feed with attachment link when any email is received in salesforce from EmailMessage [ Like if any customer is sending an email with attachment link so Feed should be inserted and attachment will link in feed ] like below image.
       User-added image

I have a requirment this by using a field -RelatedRecordId from FeedItem Object.

Please help me to get the answer....

Regards,
Sangeet
 
Best Answer chosen by Sangeet kasera
Vishwajeet kumarVishwajeet kumar
Hello,
i see, i think RelatedRecordId field do not support list of Ids. 
So you can either :
1. Create FeeItem for each ContentVersion record in an email, mutiple post, not ideal. OR 
2. Try Feed Attachment with ContentVersion. Create FeeAttachment for each ContentVersion using ContentVersion Id and link them to one FeedItem.
Sample code for ContentVersion Feed Attachment : 

FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = feedItem.Id;                          //Id of FeedItem
feedAttachment.RecordId = 'ID_OF_CONTENT_VERSION'; 
feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT'; 
insert feedAttachment;

sorry for pushing for FeedAttachment but i do not see it working any other way with list of Attachment/ContentVersion.

Thanks

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
For getting to Case emails you can implement a email service using custom email handler and email address realted to it. Ask external service providers to send emails on this email service email address, so code can be trigger on email receive. In email service email handler you can insert a FeedItem for attachment links.

Example :  Attachment link Feed

FeedItem post = new FeedItem();
post.ParentId = caseRecord.Id;                              // Record Id eg. Case id
post.Body = 'Enter post text here';
post.LinkUrl = 'http://abxcyz.com/cat.png';             //URL of the Attachment Link
insert post;


Thanks
Sangeet kaseraSangeet kasera
Hello Vishwajeet,

I have created a emailService Address and sending an email, but i don't know how to add attachment to Feed, [ Like if any customer is sending an email using given email address with attachment so Feed should be inserted and attachment will show in feed ]. 

As image is attached in question.
And using this line [ post.LinkUrl = 'http://abxcyz.com/cat.png'; ] how can i handle attachment attached in email.

As SOAP Api of FeedItem, I found RelatedRecordId is the field for contentDocId but i don't know how to refer this with feed.

Regads,
Sangeet
Vishwajeet kumarVishwajeet kumar
Hello,
Ok looks like your requirement is post Email attachment in a Feed not just to post a link Feed.
inboundEmail.binaryAttachments should give you all attachments list from email. inboundEmail.binaryAttachment[i].name, inboundEmail.binaryAttachment[i].body will give you name, body of file respectively for ith file if you loop through.

Refer to this link (https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_feedattachment.htm), you can insert FeedItem for case details(or blank) and do FeedAttachment for each files in email.

Thanks
Sangeet kaseraSangeet kasera
Hello Vishawajeet,

Actually i don't want to use feedAttachment, As i told you to use RelatedRecordId field of FeedItem, so for that i used and tried a below code-
    FeedItem post = new FeedItem();
         post.ParentId = emailObj.ParentId;
         post.Visibility = 'AllUsers';
         post.IsRichText = true;
             if(emailObj.HasAttachment){
                   post.type = 'ContentPost';
                   post.RelatedRecordId = cnvtId[0];
                    }
           post.Body = commentBody;
     Database.Insert(post, false);

But Currently here i used  post.RelatedRecordId = cnvtId[0]; which add only single record to feed, I have list of contentVersion Id in cnvtId  so now want to know how to pass list of contentVersion Id in Feed.

This code is adding a feed with single attachment like below-
User-added image

Thanks,
Sangeet
Vishwajeet kumarVishwajeet kumar
Hello,
i see, i think RelatedRecordId field do not support list of Ids. 
So you can either :
1. Create FeeItem for each ContentVersion record in an email, mutiple post, not ideal. OR 
2. Try Feed Attachment with ContentVersion. Create FeeAttachment for each ContentVersion using ContentVersion Id and link them to one FeedItem.
Sample code for ContentVersion Feed Attachment : 

FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = feedItem.Id;                          //Id of FeedItem
feedAttachment.RecordId = 'ID_OF_CONTENT_VERSION'; 
feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT'; 
insert feedAttachment;

sorry for pushing for FeedAttachment but i do not see it working any other way with list of Attachment/ContentVersion.

Thanks
This was selected as the best answer
Sangeet kaseraSangeet kasera
Hello Vishwajeet,

Thanks for your time, i appreciate it. i also found same that we can't pass list of contentVersion Ids to RelatedRecordId. if we have multiple contentVersion Ids so we need to create multiple feedItem for that.

Now as per your suggession i will work on FeedAttachment.

by the way, Thanks for your time Man. 

Regards,
Sangeet
Satish Kumar LalamSatish Kumar Lalam
Hi Sangeet / Vishwajeet,
 I am also working on the similar requrement where I wrote the below trigger on Emailmessage object. 

trigger emailToPostChatter on EmailMessage (After insert) {
    List<FeedItem> itemList = new List<FeedItem>();
    List<ContentVersion> conVersionList = new List<ContentVersion>();
    List<FeedAttachment> atchList = new List<FeedAttachment>();
    
    for (EmailMessage message : trigger.new) {
       
        FeedItem post = new FeedItem();
        post.ParentId = message.ParentId;
        post.Visibility = 'AllUsers';
        post.Body = message.FromName + '\n' + message.TextBody;
        system.debug('Body = '+post.Body);
        itemList.add(post);
        insert post;
        
        system.debug('EmailMessage Id= '+ message.Id);
        system.debug('message.ContentDocumentIds = '+message.ContentDocumentIds);
        system.debug('Post ID 1= '+ post.Id);
        system.debug('message.HasAttachment = '+message.HasAttachment);
        if(message.HasAttachment){
            system.debug('EmailMessage Id 2 = '+ message.Id);
            Id EmailId = message.Id;
            List<ContentDocumentLink> cntlinks = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =: EmailId];
            system.debug('cntlinks = '+cntlinks.size());
            for(ContentDocumentLink cntdlk : cntlinks){
                ContentVersion contv = [select Id from ContentVersion where ContentDocumentId =:cntdlk.ContentDocumentId];
                system.debug('contv ='+contv);
                FeedAttachment feedAttachment = new FeedAttachment();
                feedAttachment.FeedEntityId = post.Id;                          //Id of FeedItem
                feedAttachment.RecordId = contv.Id; 
                feedAttachment.Title = 'FileName';
                feedAttachment.Type = 'CONTENT'; 
                //insert feedAttachment;
            }
            
        }
    }
    
    
}

But the issue I am facing is List<ContentDocumentLink> cntlinks = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =: EmailId]; is returning null while the message.Id is holding ID of email message. But when I execute the same SOQL from from anonymous window I am able to get list of Content versions. Please advice on this.