Skip to main content
Skip table of contents

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.

  1. The Lightning Component > RelatedAccountFiles.cmp

  2. The Lightning Component JS Controller > RelatedAccountFilesController.js

  3. The Apex Class > RelatedAccountFilesLightningCmpCtrl.apxc

The Lightning Component > RelatedAccountFiles.cmp

CODE
<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

CODE
({
    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

CODE
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;
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.