December 3, 2020

How to attach a file to a record using Salesforce Lightning Component?

Add the below Lightning Component to the Lightning Record page.

Sample Code:

Aura Component:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
   
    <lightning:card>
        <lightning:fileUpload label = "Attach Receipt(s)"
                              name = "fileUploader"
                              multiple = "true"
                              accept = ".pdf,.png,.txt"
                              recordId = "{!v.recordId}"
                              onuploadfinished = "{!c.handleUploadFinished}" />
    </lightning:card>
   
</aura:component>


JavaScript Controller:

({
   
    handleUploadFinished: function (cmp, event) {
       
        var uploadedFiles = event.getParam("files");
        var showToast = $A.get("e.force:showToast");
        showToast.setParams({
            title : 'Files Load',
            message : 'Files Loaded Successfully.',
            type : 'success',
            mode : 'pester'
        });
        showToast.fire();
        $A.get('e.force:refreshView').fire(); 
       
    }
   
})


Output:


No comments:

Post a Comment