0

I have button, when click on it, it'll generate a doc and stock it in Files

I want to > when i click on that button again, the doc will not be duplicated

Code

 ContentVersion contVer = new ContentVersion(Title='Goldstar Request  '+this.catrequest.Account__r.Name + ' ' + this.catreq.Name , VersionData = body, PathOnClient ='/Catalog.pdf', IsMajorVersion = false);
            insert contVer;

            ContentDocumentLink contentlink = new ContentDocumentLink();
            contentlink.LinkedEntityId = this.catreq.Id;
            contentlink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contVer.Id].ContentDocumentId;
            contentlink.ShareType = 'V';
            insert contentlink;             

            set<id> docIds = new set<id>();

            list<contentdocumentlink> links = [Select Id,contentdocument.id,contentdocument.LatestPublishedVersionId from ContentDocumentLink where LinkedEntityId = :this.catrequest.id];
            For(contentdocumentlink l: links){
                docIds.add(l.contentdocument.id);
            }

             if (!Links.isEmpty()) {
              ContentVersion version = new ContentVersion();
              version.ContentDocumentId  = links[0].ContentDocumentId;
              insert  version; 
             }


Screenshot[1]

Thanks in advance

  • Do you mean when you click on the button again filename should be different? – Hengky Ilawan May 23 at 0:40
  • No , when i click on the button again the new file generated should remplace the old file .. shouldn't be duplicated – Firas Taamallah May 23 at 1:11
0

If you want to maintain the file versions, simply insert another ContentVersion record with the same ContentDocumentId. It won't be creating a new file, instead it would create a newer version of the file. Do not create another ContentDocument and ContentDocumentLink.

Otherwise, upsert the ContentVersion will do.

Sample Code:

String title = 'Goldstar Request ' + this.catrequest.Account__r.Name + ' ' + this.catreq.Name;
ContentVersion contVer = new ContentVersion(
    Title = title,
    VersionData = body,
    PathOnClient ='/Catalog.pdf'
);
List<ContentDocumentLink> docLinks = [
        SELECT Id, ContentDocumentId 
        FROM ContentDocumentLink 
        WHERE ContentDocument.Title = :title 
            AND LinkedEntityId = :this.catrequest.id
    ];
// check if file already exists, 
// if yes, insert a newer ContentVersion by specifying the ContentDocumentId
if (!docLinks.isEmpty()) {
    contVer.ContentDocumentId = docLinks[0].ContentDocumentId;
}
insert(contVer);

// if file didn't exists before
// insert a new ContentDocumentLink
if (docLinks.isEmpty()) {
    ContentDocumentLink contentlink = new ContentDocumentLink();
    contentlink.LinkedEntityId = this.catrequest.Id;
    contentlink.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contVer.Id].ContentDocumentId;
    contentlink.ShareType = 'V';
    insert contentlink;
}

| improve this answer | |
  • like this ? list<contentdocumentlink> links = [Select Id,contentdocument.id from ContentDocumentLink where LinkedEntityId = :this.catrequest.id]; For(contentdocumentlink l: links){ docIds.add(l.contentdocument.id); } ContentVersion contVer1 = new ContentVersion(Title='Goldstar Request '+this.catrequest.Account__r.Name + ' ' + this.catreq.Name , VersionData = body, PathOnClient ='/Catalog.pdf'); upsert contVer1; – Firas Taamallah May 24 at 15:41
  • I added sample code. – Hengky Ilawan May 24 at 16:26
  • it doesn't work .. – Firas Taamallah May 25 at 15:36
  • Please note that the sample code I have provided is not the complete code, it is just to give you example how to not create a new file each time. You still need to somehow combine with original code. – Hengky Ilawan May 25 at 15:41
  • yes i know , as you mentioned i have created another contentVersion and i referenced the first contentversion but it doesn't work .. files still inserted as dupes – Firas Taamallah May 25 at 16:12
-1

you can use upsert operation, instead of insert operation

ContentVersion contVer = new ContentVersion(Title='Goldstar Request  '+this.catrequest.Account__r.Name + ' ' + this.catreq.Name , VersionData = body, PathOnClient ='/Catalog.pdf');
upsert contVer;

note: i am assuming u have null check in place , use size method before dml operation

| improve this answer | |
  • Have you actually tried this? I'm fairly sure it would just insert a new record instead of preventing duplicates. Upsert updates if the record(s) being operated on have an Id (or an external id that you specify as part of the upsert, e.g. upsert myRecs myExternalIdField;) – Derek F May 23 at 13:10
  • Field is not writeable: ContentVersion.ContentDocumentId , you cannot upsert ContentVersion – Firas Taamallah May 25 at 16:03

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.