0

Updated the code here. I am trying to override the standard file upload button functionality. I have created a lwc on a VF page and overrode the 'Upload files' button in FILEs object.

As soon as we upload the file using lightning-input type='file' onChange takes to the below function

handleUploadFinished(event) { let fileList = event.detail.files; [...fileList].forEach(file => { let fileReader = new FileReader(); let recordId = this.recId;

    fileReader.onload = function() {
        let fileContents = fileReader.result;
        let base64Mark = 'base64,';
        let rrId= recordId; // local variable
        let dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;
        fileContents = fileContents.substring(dataStart);   
        alert(rrId+'---------------');    //this rrId alert works well
        uploadFile({
            parentId: rrId, // the value of rrId is not getting passed here
            fileName: file.name,
            base64Data: encodeURIComponent(fileContents)
        })
        .then(result => {
            alert('Sucess');
        })
        .catch(error => {
            alert('Error');
        });
    };
    fileReader.readAsDataURL(file);
});
    this.showFileUpload=false;
}

Referred one of the StackExchange ans and tried the below in apex. UploadFile method(Id recordID, param2,param3)

ContentVersion contentVersion = new ContentVersion();
contentVersion.ContentLocation = 'S';.
contentVersion.PathOnClient = fileName;
contentVersion.Origin = 'C';
contentVersion.OwnerId = UserInfo.getUserId();
contentVersion.Title = fileName;
contentVersion.VersionData = EncodingUtil.base64Decode(base64Data);
contentVersion.customField = true;
Insert contentVersion;
Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contentVersion.Id].ContentDocumentId;


contentDocumentLink.ContentDocumentId = contentDocumentId; 
contentDocumentLink.LinkedEntityId = recordId; 
contentDocumentLink.ShareType = 'I';
contentDocumentLink.Visibility = 'AllUsers'; 
Insert contentDocumentLink;

I am facing the error while inserting contentDocumentLink. Can someone please guide me here

4
  • 1
    Can u share your complete code? Have you checked that recordId not null?
    – q-jack
    Jul 20, 2022 at 12:41
  • Did you check if you are getting the recordid with out null. I hope the recordId Variable is returning Null Jul 20, 2022 at 12:45
  • You need to share more code. We see the variable recordId but do not see where it comes from. Share your LWC code and also the head of your method. I am pretty sure that your recordId is not passed to your method
    – q-jack
    Jul 20, 2022 at 13:57
  • yes the problem seems to be with the recordId only. Its value is getting lost. Please refer the comments to see till which line I'm able to access the recordId
    – Dusk123
    Jul 29, 2022 at 11:52

0

You must log in to answer this question.

Browse other questions tagged .