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

How to associate a file to chatter stream from Apex,

Hi everyone,


I'm trying to load a file and associate it to an object's chatter feed through VisualForce. I've done this before in another org but i'm having some issues now. Has anyone got this error message before:


First Publish Location: Content cannot be inserted as a child of record a0UK0000000UVe1 (this is Id of the Custom Object record I'm trying to associate the file to).


Here's how i'm creating the file:


//Create custom object record
Document__c doc = new Document__c();
doc.Name= 'File wrapper';
doc.Description__c='Sample description';
insert doc; 

//Create ContentVersion and associate to custom object record
ContentVersion myContent = new ContentVersion();
myContent.VersionData = fileBody;
myContent.Description = doc.Description__c; 
myContent.PathOnClient = fileName;
myContent.Origin = 'H';
myContent.FirstPublishLocationId =;

insert myContent;


Any ideas on why isn't this working anymore? 


If I don't specify a FirstPublishLocationId everything works fine, also I made sure that Chatter is enabled for my custom object and Content is enabled for the org.