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