Does anyone know how I can change the FileExtension for a new ContentVersion in a test class?

Im currently trying to create a test class for my ContentDocumentLink Trigger, in which I have an if depending on the FileExtension of the ContentVersion. But when creating a ContentVersion I cant fill this field. When i try to fill it before the insert I just get an error (Field is not writeable: ContentVersion.FileExtension). I also tried putting the FileExtension in the title, but when testing manually the Title is cut off seperating title and FileExtension.


It's determined by the PathOnClient field as populated when you create the ContentVersion.

As an example, in one of my orgs I uploaded a file called App.logo.png through the API, populating its file name in the PathOnClient field and App.logo in the Title field. The system inferred png as the value for FileExtension.


ContentVersion's FileExtension and FileType fields are not writeable, it is determined by the PathOnClient, so you will have to provide value in PathOnClient field to determine the extension.

So, your code should be

ContentVersion cv = new ContentVersion();
cv.Title = 'Test Image';
cv.PathOnClient = 'Test_Image.jpg';
cv.VersionData = Blob.valueOf('Test Image Data');
cv.IsMajorVersion = true;
insert cv;

Although I was not able to find any documentation which says, that FileExtension field is not writeable, but you can get this information by looking at field properties which mentions that this field is neither creatable nor updateable. Also, the above documentation just mentions that FileType is determined by PathOnClient field whereas FileExtension is also determined by PathOnClient.

ContentVersion FileExtension

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.