You need to sign in to do that
Don't have an account?
Ankita Jena 11
How to update a custom checkbox field on Content version object when the file is uploaded from Custom Lightning File Upload Component? which is embeded in Account Record Page.
The custom field is : Isctive__c on Content Version Object.
Can we update the checkbox field directly in Apex or we need Trigger for that?
Will appreciate your solution.
Please find the below code:
<aura:component controller="WZ_AccountFileUploadHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="files" type="ContentDocument[]"/>
<aura:attribute name="recordId" type="String" default="0011w00000tb4N2AAI"/>
<aura:attribute name="accept" type="List" default="['.jpg', '.jpeg','.pdf','.csv','.xlsx','.png','.pptx','.Docx', '.Doc']"/>
<aura:attribute name="multiple" type="Boolean" default="false"/>
<aura:attribute name="Spinner" type="boolean" default="false"/>
<div class="slds">
<lightning:notificationsLibrary aura:id="notifLib"/>
<div class="contentbox">
<div class="slds-page-header header slds-text-heading_medium slds-align_absolute-center" style="background-color:#7BD7F7; font-family:serif,Garamond;">
<div class="slds-p-right_x-small"><img height="40" width="40" src="{!$Resource.WZ_Account_Id_Card}"/></div><p style="color:#36393A;font-size: 1.45rem !important;"><b>ACCOUNT ID CARD</b></p></div>
<div class="slds-grid slds-theme_default">
<div style="width:100%" class="slds-p-bottom_medium">
<center>
<lightning:fileUpload label="" multiple="{!v.multiple}" file-field-name="Title"
accept="{!v.accept}" recordId="{!v.recordId}" aura:id="lightningFileUpload"
onuploadfinished="{!c.UploadFinished}" />
</center>
</div>
</div>
<div class="slds-form--compound" style="position:relative">
<table class="slds-table slds-table--bordered">
<!-- hide the table header if file is empty -->
<aura:if isTrue="{!not(empty(v.files))}">
<thead>
<tr>
<th>Title</th>
<th>FileType</th>
<th></th>
</tr>
</thead>
</aura:if>
<tbody>
<aura:iteration items="{!v.files}" var="f">
<tr>
<td class="slds-cell-wrap"><a href="" id="{!f.Id}" onclick="{!c.previewFile}">{!f.Title}</a></td>
<td class="slds-wrap slds-cell-wrap">{!f.FileType}</td>
<td><a href="javascript:void(0)" id="{!f.Id}" onclick="{!c.delFiles}">
<lightning:icon size="x-small" iconName="utility:delete" alternativeText="Delete" />
</a>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
<aura:if isTrue="{!v.Spinner}">
<div class="slds-spinner_container">
<div class="slds-spinner slds-spinner--medium" aria-hidden="false" role="alert">
<div class="slds-spinner__dot-a"></div>
<div class="slds-spinner__dot-b"></div>
</div>
</div>
</aura:if>
</div>
</div>
</div>
</aura:component>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//WZ_AccountFileUploadComponentController.js
({
doInit:function(component,event,helper){
helper.getuploadedFiles(component);
},
previewFile :function(component,event,helper){
var rec_id = event.currentTarget.id;
$A.get('e.lightning:openFiles').fire({
recordIds: [rec_id]
});
},
UploadFinished : function(component, event, helper) {
var uploadedFiles = event.getParam("files");
helper.getuploadedFiles(component);
component.find('notifLib').showNotice({
"variant": "info",
"header": "Success",
"message": "File Uploaded successfully!!",
closeCallback: function() {}
});
},
delFiles:function(component,event,helper){
component.set("v.Spinner", true);
var documentId = event.currentTarget.id;
helper.delUploadedfiles(component,documentId);
}
})
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WZ_AccountFileUploadComponentHelper.js
({
getuploadedFiles:function(component){
var action = component.get("c.getFiles");
action.setParams({
recordId : component.get("v.recordId")
});
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
var result = response.getReturnValue();
component.set("v.files",result);
}
});
$A.enqueueAction(action);
},
delUploadedfiles : function(component,documentId) {
var action = component.get("c.deleteFiles");
action.setParams({
"sdocumentId":documentId
});
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
this.getuploadedFiles(component);
component.set("v.Spinner", false);
}
});
$A.enqueueAction(action);
},
})
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WZ_AccountFileUploadHandler.apxc
public class WZ_AccountFileUploadHandler {
@AuraEnabled
public static List<ContentDocument> getFiles(string recordId){
Set<Id> recordIds = new Set<Id>{recordId};
Set<Id> documentIds = new Set<Id>();
List<ContentDocumentLink> cdl=[SELECT id,LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN:recordIds ];
for(ContentDocumentLink cdLink:cdl){
documentIds.add(cdLink.ContentDocumentId);
}
return [SELECT Id,Title,FileType FROM ContentDocument WHERE id IN: documentIds];
}
@AuraEnabled
public static void deleteFiles(string sdocumentId){
delete [SELECT Id,Title,FileType from ContentDocument WHERE id=:sdocumentId];
}
}
Thanks.
Can we update the checkbox field directly in Apex or we need Trigger for that?
Will appreciate your solution.
Please find the below code:
<aura:component controller="WZ_AccountFileUploadHandler" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="files" type="ContentDocument[]"/>
<aura:attribute name="recordId" type="String" default="0011w00000tb4N2AAI"/>
<aura:attribute name="accept" type="List" default="['.jpg', '.jpeg','.pdf','.csv','.xlsx','.png','.pptx','.Docx', '.Doc']"/>
<aura:attribute name="multiple" type="Boolean" default="false"/>
<aura:attribute name="Spinner" type="boolean" default="false"/>
<div class="slds">
<lightning:notificationsLibrary aura:id="notifLib"/>
<div class="contentbox">
<div class="slds-page-header header slds-text-heading_medium slds-align_absolute-center" style="background-color:#7BD7F7; font-family:serif,Garamond;">
<div class="slds-p-right_x-small"><img height="40" width="40" src="{!$Resource.WZ_Account_Id_Card}"/></div><p style="color:#36393A;font-size: 1.45rem !important;"><b>ACCOUNT ID CARD</b></p></div>
<div class="slds-grid slds-theme_default">
<div style="width:100%" class="slds-p-bottom_medium">
<center>
<lightning:fileUpload label="" multiple="{!v.multiple}" file-field-name="Title"
accept="{!v.accept}" recordId="{!v.recordId}" aura:id="lightningFileUpload"
onuploadfinished="{!c.UploadFinished}" />
</center>
</div>
</div>
<div class="slds-form--compound" style="position:relative">
<table class="slds-table slds-table--bordered">
<!-- hide the table header if file is empty -->
<aura:if isTrue="{!not(empty(v.files))}">
<thead>
<tr>
<th>Title</th>
<th>FileType</th>
<th></th>
</tr>
</thead>
</aura:if>
<tbody>
<aura:iteration items="{!v.files}" var="f">
<tr>
<td class="slds-cell-wrap"><a href="" id="{!f.Id}" onclick="{!c.previewFile}">{!f.Title}</a></td>
<td class="slds-wrap slds-cell-wrap">{!f.FileType}</td>
<td><a href="javascript:void(0)" id="{!f.Id}" onclick="{!c.delFiles}">
<lightning:icon size="x-small" iconName="utility:delete" alternativeText="Delete" />
</a>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
<aura:if isTrue="{!v.Spinner}">
<div class="slds-spinner_container">
<div class="slds-spinner slds-spinner--medium" aria-hidden="false" role="alert">
<div class="slds-spinner__dot-a"></div>
<div class="slds-spinner__dot-b"></div>
</div>
</div>
</aura:if>
</div>
</div>
</div>
</aura:component>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//WZ_AccountFileUploadComponentController.js
({
doInit:function(component,event,helper){
helper.getuploadedFiles(component);
},
previewFile :function(component,event,helper){
var rec_id = event.currentTarget.id;
$A.get('e.lightning:openFiles').fire({
recordIds: [rec_id]
});
},
UploadFinished : function(component, event, helper) {
var uploadedFiles = event.getParam("files");
helper.getuploadedFiles(component);
component.find('notifLib').showNotice({
"variant": "info",
"header": "Success",
"message": "File Uploaded successfully!!",
closeCallback: function() {}
});
},
delFiles:function(component,event,helper){
component.set("v.Spinner", true);
var documentId = event.currentTarget.id;
helper.delUploadedfiles(component,documentId);
}
})
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WZ_AccountFileUploadComponentHelper.js
({
getuploadedFiles:function(component){
var action = component.get("c.getFiles");
action.setParams({
recordId : component.get("v.recordId")
});
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
var result = response.getReturnValue();
component.set("v.files",result);
}
});
$A.enqueueAction(action);
},
delUploadedfiles : function(component,documentId) {
var action = component.get("c.deleteFiles");
action.setParams({
"sdocumentId":documentId
});
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
this.getuploadedFiles(component);
component.set("v.Spinner", false);
}
});
$A.enqueueAction(action);
},
})
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WZ_AccountFileUploadHandler.apxc
public class WZ_AccountFileUploadHandler {
@AuraEnabled
public static List<ContentDocument> getFiles(string recordId){
Set<Id> recordIds = new Set<Id>{recordId};
Set<Id> documentIds = new Set<Id>();
List<ContentDocumentLink> cdl=[SELECT id,LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN:recordIds ];
for(ContentDocumentLink cdLink:cdl){
documentIds.add(cdLink.ContentDocumentId);
}
return [SELECT Id,Title,FileType FROM ContentDocument WHERE id IN: documentIds];
}
@AuraEnabled
public static void deleteFiles(string sdocumentId){
delete [SELECT Id,Title,FileType from ContentDocument WHERE id=:sdocumentId];
}
}
Thanks.
please find your solution.
let me know if it helps you and close your query by marking it as best answer.
Thank you
You can do it in the same lightning component itself no need of trigger. Please find the below updated code :
JS Controller:
I have updated the UploadFinished method.
JS Helper:
Added new method updateCV.
Apex Class:
Added new method updateCV in apex class
Thanks,
Maharajan.C
But it already worked with a minor change in aura cmp.
https://developer.salesforce.com/docs/component-library/bundle/lightning:fileUpload/documentation