+ Start a Discussion
Luciano RobertoLuciano Roberto 

How display list attachments of Custom object

Hello Folks,

I am creating a Lightning component to display the list of attachments related to registering a custom object. Could check where I am going wrong, because nothing is displayed.
 
AttachmentList.cmp 
--------------------------------------------------------------------------------- 
<aura:component implements="forceCommunity:availableForAllPageTypes,force:hasRecordId" access="global" controller="AttachmentListController"> <aura:attribute name="recordId" type="Id" required="true"/> <aura:attribute name="files" type="List"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <span class="text-blue"> <!-- <aura:iteration items="{!v.record.Attachments}" var="obj"> --> <aura:iteration items="{!v.files}" var="obj"> {!obj.Title} <br/> </aura:iteration> </span>
 </aura:component>

AttachmentListcontroller.js -------------------------------------------------------------------------------------------------- 

({ 

doInit : function(component, event, helper) { $A.enqueueAction(component.get('c.getList')); }, getList: function(component, event, helper) { var action = component.get("c.getContentDocs"); action.setParams( { arecordId : component.get('v.recordId') }); action.setCallback(this, function(actionResult) { component.set('v.files',actionResult.getReturnValue()); }); $A.enqueueAction(action); }, 

})

AttachmentListController.apxc ------------------------------------------------------------------------------------------------------------ 

public class AttachmentListController
 { 

@AuraEnabled 
public static List<ContentDocument> getContentDocs(Id arecordId) 
{
 List<ContentDocumentLink> CDLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :arecordId];

 //List<ContentDocumentLink> CDLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :'a08M000000BuU4vIAF']; if (CDLs.size() < 1) return new List<ContentDocument>(); // Make a List of ContentDocument IDs List <Id> CDIdList = new List <Id> (); for (ContentDocumentLink nextCDL : CDLs) { CDIdList.add(nextCDL.ContentDocumentId); } List<ContentDocument> entries = [SELECT Id, Title, FileType FROM ContentDocument WHERE ContentDocument.Id IN :CDIdList]; return entries;
 }
 }



Thanks
Best Answer chosen by Luciano Roberto
Arun ParmarArun Parmar
Hi Luciano Roberto,

You are doing absolutely fine. just change some code->
1. first remove required attribute from recordId because if you methion recordId as required thn you have mention some default values.  
2. Uncomment controller method logic.
You can use this code -> 

AttachmentList.cmp ->
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="AttachmentListController"> 
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="files" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <span class="text-blue">
        <aura:iteration items="{!v.files}" var="obj">
            {!obj.Title} <br/>
        </aura:iteration> 
    </span>

 </aura:component>
AttachmentListcontroller.js
({
    
    doInit : function(component, event, helper) { 
        $A.enqueueAction(component.get('c.getList')); 
    }, 
    getList: function(component, event, helper) { 
        var action = component.get("c.getContentDocs"); 
        action.setParams( { 
            arecordId : component.get('v.recordId')
        }); 
        action.setCallback(this, function(actionResult) {
            component.set('v.files',actionResult.getReturnValue()); 
        }); 
        $A.enqueueAction(action); 
    },
    
})

AttachmentListController.apxc
public class AttachmentListController {
    @AuraEnabled
    public static List<ContentDocument> getContentDocs(Id arecordId) {
        List<ContentDocumentLink> CDLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :arecordId];
        if (CDLs.size() < 1) {
            return new List<ContentDocument>();
        }
        List<Id> CDIdList = new List<Id> (); 
        for (ContentDocumentLink nextCDL : CDLs) { 
            CDIdList.add(nextCDL.ContentDocumentId); 
        } 
        List<ContentDocument> entries = [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :CDIdList]; 
        return entries;
    }
}

Now first try to upload any file to any record, put this component on record page and see the results. It will work.

Please mark as best answer if it helpful.

Thanks

 

All Answers

Arun ParmarArun Parmar
Hi Luciano Roberto,

You are doing absolutely fine. just change some code->
1. first remove required attribute from recordId because if you methion recordId as required thn you have mention some default values.  
2. Uncomment controller method logic.
You can use this code -> 

AttachmentList.cmp ->
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="AttachmentListController"> 
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="files" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <span class="text-blue">
        <aura:iteration items="{!v.files}" var="obj">
            {!obj.Title} <br/>
        </aura:iteration> 
    </span>

 </aura:component>
AttachmentListcontroller.js
({
    
    doInit : function(component, event, helper) { 
        $A.enqueueAction(component.get('c.getList')); 
    }, 
    getList: function(component, event, helper) { 
        var action = component.get("c.getContentDocs"); 
        action.setParams( { 
            arecordId : component.get('v.recordId')
        }); 
        action.setCallback(this, function(actionResult) {
            component.set('v.files',actionResult.getReturnValue()); 
        }); 
        $A.enqueueAction(action); 
    },
    
})

AttachmentListController.apxc
public class AttachmentListController {
    @AuraEnabled
    public static List<ContentDocument> getContentDocs(Id arecordId) {
        List<ContentDocumentLink> CDLs = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :arecordId];
        if (CDLs.size() < 1) {
            return new List<ContentDocument>();
        }
        List<Id> CDIdList = new List<Id> (); 
        for (ContentDocumentLink nextCDL : CDLs) { 
            CDIdList.add(nextCDL.ContentDocumentId); 
        } 
        List<ContentDocument> entries = [SELECT Id, Title, FileType FROM ContentDocument WHERE Id IN :CDIdList]; 
        return entries;
    }
}

Now first try to upload any file to any record, put this component on record page and see the results. It will work.

Please mark as best answer if it helpful.

Thanks

 
This was selected as the best answer
Luciano RobertoLuciano Roberto

Thank you very much Arun Parmar's,
Worked perfectly!!

I just need now to create a link to download the file, I'm afraid how to help me?

Luciano RobertoLuciano Roberto
Thank you so much Arun, I already put your answer as resolved.
If you can help me with my other question about downloading the file, follow the link in the other topic. Thank you!

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IMJ9QAO#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Lightning&criteria=OPENQUESTIONS&id=9062I000000IMQZQA4