When I try to create a Content Document (File) in apex, it creates one with no facility to revise or add new version. However if I use the UI to create a file it gives me a link to add new version. Here's the code I use to create the file:

    ContentVersion contentVersion = new ContentVersion(
     Title = 'Test anonymous 4',
     PathOnClient = 'Test anonymous 4.txt',
     VersionData = EncodingUtil.Base64Decode('Test Anonymous 1'),
     IsMajorVersion = true

insert contentVersion;

I'm using API version 42 for my class and page and I'm running this as a system admin. I have tried executing the code in 'execute anonymous' window and it does the same. Is there anything I've missed out?

File created using Apex

File created using UI

File created using Apex File created using UI

  • Are you sure you are seeing both the image as same user keeping the same access levels? Feb 28 '18 at 11:45

I tried with the below code and I can see the upload new version link in the UI.

ContentVersion v = new ContentVersion();
v.versionData = Blob.valueOf('Test String');
v.title = 'Test Upload';
v.pathOnClient ='/mypath.txt';
v.IsMajorVersion = true;
insert v;
System.debug('File Id '+v.id);


enter image description here

  • Saroj, your code has the same result as before for me.
    – Swatz
    Feb 28 '18 at 15:17

I found out that I needed to set the Origin to 'H' on my Content Version to created a revisable file. Very weird.

  • this is odd and hard to believe; I've never had to do this
    – cropredy
    Mar 1 '18 at 0:21
  • This is really wired. I also never had to do this, you can see from my answer.
    – Saroj Bera
    Mar 1 '18 at 6:36

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.