0

I have a lightning component that uses lightning file upload to tag a file at the ContentVersion level when it is uploaded. However, when bulk uploading files, only one of the documents gets tagged.

Any ideas on how I can force all to be tagged?

Component:

<aura:component controller="PODS_SuppDocContentVersion" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" > 

    <aura:attribute name="options" type="List" default="[
    {'label': 'Supporting Document', 'value': 'option1'},
    ]"/>
    <aura:attribute name="value" type="List" default="option1"/>


   <div class="slds-box slds-theme_default"> 
    <div class="slds-media__body"> 
        <h1 class="slds-card__header-title slds-truncate slds-align-middle--xx-small" title="Add Supporting Document"><b>Upload Supporting Document</b></h1> 

<!-- Renders the File Upload and disabled checkbox -->         
  </div>     
    <lightning:fileUpload label=" "  multiple="true" accept=".pdf, .docx, .doc" recordId="{!v.recordId}" onuploadfinished="{!c.handleUploadFinished}"  /> 
     <lightning:checkboxGroup name="Checkbox Group Disabled"
                             label=" "
                             options="{! v.options }"
                             value="{! v.value }"
                             disabled="true"/>
    </div> 

</aura:component>

Controller:

    ({ 
    handleUploadFinished : function(component, event, helper) { 

  var action = component.get("c.updateContentVersion");
  action.setParams({recId : component.get("v.recordId") });
  $A.enqueueAction(action)

        // Get the list of uploaded files 
        var uploadedFiles = event.getParam("files"); 

        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'File Upload Complete',
            message: "Your supporting document, "+uploadedFiles[0].name+", has been uploaded successfully.",
            messageTemplate: 'Record {0} created! See it {1}!',
          //  duration:' 5000',
        //  key: 'info_alt',
            type: 'success',
            mode: 'sticky'
        });
        toastEvent.fire();
    }


})

Apex:

public with sharing class PODS_SuppDocContentVersion{

@AuraEnabled
    public static void updateContentVersion(String recId){
        ContentVersion[] cvs = [SELECT ID, FirstPublishLocationId, Document_Type__c FROM ContentVersion WHERE FirstPublishLocationId = :recId ORDER BY CreatedDate DESC];
                                Cvs[0].Document_Type__c = 'Supporting Document';
                                                update cvs[0];

    }              
}

1 Answer 1

0

Your Apex server controller explicitly operates on exactly one record.

ContentVersion[] cvs = [SELECT ID, FirstPublishLocationId, Document_Type__c FROM ContentVersion WHERE FirstPublishLocationId = :recId ORDER BY CreatedDate DESC];
 Cvs[0].Document_Type__c = 'Supporting Document';
 update cvs[0];

If you want to update all of the uploaded documents, you would need to iterate over cvs and update each one, then update the list.

If you choose to do that, you'll probably want to place an additional filter on your query, such as a CreatedDate filter and a CreatedById filter, to ensure you only grab the correct versions.

Better still, grab the ContentDocumentId values offered in the callback:

({
    handleUploadFinished: function (cmp, event) {
        // Get the list of uploaded files
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);
    }
})

Pass these to your server controller and then query your ContentVersion records based on their ContentDocumentId being one of the created documents.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .