Showing Related Files on a File Object
You can show parent object files on child objects and vice-versa but configuring multiple S-Drive Lightning components on the same record page. This requires creating a new Lightning Component and a new Apex Class is required. The Lightning component will have the S-Drive component in it with attributes set according to the related object file.
In this example, we will be showing how you can enable showing the related Account's files in a Case record page in lightning.
![](../__attachments/1950417420/worddav86cff8796477c31c5198623d65bf4dcb.png%3Fversion=1&modificationDate=1614709732920&cacheVersion=1&api=v2&width=615&height=343?inst-v=45374f53-f4b0-4a30-83dc-edc14e9d1f8c)
The Lightning Component > RelatedAccountFiles.cmp
The Lightning Component JS Controller > RelatedAccountFilesController.js
The Apex Class > RelatedAccountFilesLightningCmpCtrl.apxc
The Lightning Component > RelatedAccountFiles.cmp
<aura:component controller="RelatedAccountFilesLightningCmpCtrl"
implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="relatedAccountId" type="String"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<aura:if isTrue="{!v.relatedAccountId}">
<cg:SDrive title="Related Account Files"
customObjectName="Account"
customObjectFileName="AccountFile__c"
fileNamespacePrefix="cg__"
relationshipName="Account__r"
componentId="AccountFilesCmp"
recordId="{!v.relatedAccountId}"
/>
</aura:if>
</aura:component>
The <cg:SDrive> component will have the following attributes according to your use case:
customObjectName: The object that you want to use S-Drive with
customObjectFileName: The object you created for recording your S-Drive files
fileNamespacePrefix: For file objects coming with S-Drive package this attribute should be "cg__", for other objects you have in your organization it will be empty as ""
relationshipName: The relationship name between the parent object and the file object
componentId: This componentId is advised to be set as unique when you have multiple components in one record page
recordId: This will be set as the related parent object's id that you want to show the files of
If you want to show a specific record's files in every record's page, you can write the ID of the object directly in to recordId attribute. If not, you will need to create the Javascript and Apex controllers.
The Lightning Component JS Controller > RelatedAccountFilesController.js
({
doInit : function(component, event, helper) {
var caseId = component.get("v.recordId");
var action = component.get("c.getRelatedAccountId");
action.setParams({
caseId : caseId
});
action.setCallback(this, function(response) {
var r = response.getReturnValue();
var state = response.getState();
if (component.isValid() && state === "SUCCESS"){
component.set("v.relatedAccountId", r);
}
});
$A.enqueueAction(action);
}
})
The Apex Class > RelatedAccountFilesLightningCmpCtrl.apxc
public class RelatedAccountFilesLightningCmpCtrl {
@AuraEnabled
public static String getRelatedAccountId(String caseId){
String queryStr = 'SELECT ID, AccountId FROM Case WHERE Id = :caseId LIMIT 1';
List<Case> caseList = Database.query(queryStr);
String relatedAccountId = '';
if(null != caseList && caseList.size() > 0){
relatedAccountId = caseList[0].AccountId;
}
return relatedAccountId;
}
}