+ Start a Discussion
David Roberts 4David Roberts 4 

how to add a document version

I can create a file using:
String strDebug = 'some debug information \n';
       
        
        //ContentDocument
        //SELECT title,  LatestPublishedVersionId FROM ContentDocument where title = 'debuglog.txt'
        ContentVersion file = new ContentVersion(
            title = 'debuglog.txt',
            versionData = Blob.valueOf( strDebug ),
            pathOnClient = '/debuglog.txt'
        );
        
        insert file;

but how do I overwrite it (add a new version) so I don't get multiple 'debuglog.txt' files?
Best Answer chosen by David Roberts 4
David Roberts 4David Roberts 4
Hi Deepali,

Thanks for your suggestion.
It gave me clues to find the solution:
I first queried the CurrentVersion for a file with the title 'Debug Log' retrieveing the ContentDocumentId (parent container).
I then create a new version telling it about the parent ContentDocumentId.
(I should probably cater for no parent found...).
 
Id contentDocumentId = [SELECT Id, ContentDocumentId , title,filetype,fileextension, IsLatest  FROM ContentVersion where title = 'debug log' LIMIT 1].ContentDocumentId;
        

ContentVersion fileVersion = new ContentVersion(
    title = 'debug log',
    versionData = Blob.valueOf( strDebug ),
    pathOnClient = '/debuglog.txt',
    ContentDocumentId = contentDocumentId
);

insert fileVersion;

Regards,
Dave.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi David,

Select the object on which the file is present and click on that file. It will open the location of the file.
1. On the left side, you'll see an option to 'upload new version' or if you are using the lightning version select this option from the right side drop-down button.
2. Select that option and select the new file that you want to replace.
3. You're done. Now the old file is overwritten.
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
David Roberts 4David Roberts 4
Thanks for your comment, Deepali, but I want to do this in code. Regards, Dave.
Deepali KulshresthaDeepali Kulshrestha
Hi David,

Use this code to create a new version of an existing file:
public class FileManager {

  public static Id createFile(String fileName, String base64Data,
                             String fileType, String fileExt, Id DocumentId){

    //base64Data  = EncodingUtil.urlDecode(base64Data, 'UTF-8')                                 ;

    ContentVersion cv = new ContentVersion();
    cv.ContentLocation = 'S'; // 'S' = a Salesforce File

    cv.ContentDocumentId = contentDocumentId;        

    cv.VersionData = EncodingUtil.base64Decode(base64Data);
    cv.Title = fileName;
    cv.PathOnClient = fileName + '.' + fileExt;

    //Utils.DatabaseInsert(cv);
    insert cv;

    System.debug('The contentDocumentId for ' + fileName + ' is ' + cv.ContentDocumentId);

    return cv.Id;
 }


 Here the DocumentId is the ContentDocumentId of the document of which you want to change the version. 
 All the other attributes such as base64Data and fileType etc. can be changed.
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
David Roberts 4David Roberts 4
Hi Deepali,

Thanks for your suggestion.
It gave me clues to find the solution:
I first queried the CurrentVersion for a file with the title 'Debug Log' retrieveing the ContentDocumentId (parent container).
I then create a new version telling it about the parent ContentDocumentId.
(I should probably cater for no parent found...).
 
Id contentDocumentId = [SELECT Id, ContentDocumentId , title,filetype,fileextension, IsLatest  FROM ContentVersion where title = 'debug log' LIMIT 1].ContentDocumentId;
        

ContentVersion fileVersion = new ContentVersion(
    title = 'debug log',
    versionData = Blob.valueOf( strDebug ),
    pathOnClient = '/debuglog.txt',
    ContentDocumentId = contentDocumentId
);

insert fileVersion;

Regards,
Dave.
This was selected as the best answer