Can someone give me an example of how to attach a file to Lead using FeedItem in SOAP for version 36 since ContentData and ContentFileName fields are obsolete?

My previous code was

FeedItem post = new FeedItem();

post.Body = message;
post.ParentId = AccountID;
post.Type = "ContentPost";
post.ContentData = content;
post.ContentFileName = FileName;
sObject[] posts = new sObject[] { post };
SaveResult[] results=Binding.create(posts);

I know I should use FeedAttachment but I have no success.


To create the attachment in the post, first upload the file in the UI and use its content version id.

//create and insert post

FeedItem post = new FeedItem();

post.Body = 'HelloThere';

post.ParentId = 'ID_OF_LEAD_ENTITY' ;

post.Title = 'FileName';

insert post;

//create and associate a content attachment to the post

FeedAttachment feedAttachment = new FeedAttachment();

feedAttachment.FeedEntityId = post.Id;

feedAttachment.RecordId = 'ID_OF_CONTENT_VERSION' ;

feedAttachment.Title = 'FileName';

feedAttachment.Type = 'CONTENT'; 

insert feedAttachment;

Every client is uploading files by himself over web form, so I must automate the process by letting them upload files directly to their Lead.

I came up with solution that worked for me at the end for upload but another problem occurred. When converting those leads with attachments, files are not mapped to their appropriate Account/Contant. But when I attach a file directly on Salesforce, files are mapped.

I guess I'm missing something in my code that doesn't allow file to be mapped further upon conversion.

             FeedItem feedItem = new FeedItem();
                feedItem.Body = message;
                feedItem.ParentId = LeadID;
                feedItem.Title = FileName;

                sObject[] posts = new sObject[] { feedItem };
                SaveResult[] results = Binding.create(posts);

                ContentVersion contentVersion = new ContentVersion();
                contentVersion.Title = FileName;
                contentVersion.VersionData = content;
                contentVersion.PathOnClient = FileName;

                sObject[] posts2 = new sObject[] { contentVersion };
                SaveResult[] results2 = Binding.create(posts2);

                FeedAttachment feedAttachment = new FeedAttachment();
                feedAttachment.FeedEntityId = results[0].id;
                feedAttachment.RecordId = results2[0].id;
                feedAttachment.Title = FileName;
                feedAttachment.Type = "CONTENT";

                sObject[] posts3 = new sObject[] { feedAttachment };
                SaveResult[] results3 = Binding.create(posts3);

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.