function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nejib KsaierNejib Ksaier 

returnValue is returning empty list

Hi everybody, 

I'm trying to get a list in a lightning component. In the sandbox and in the community preview in my org i get a non empty list, but when i use the live version of my community, i get always an empty list for the same settings and same code. 
Can someone help? 

thanks, 
Best Answer chosen by Nejib Ksaier
Nejib KsaierNejib Ksaier
The problem was witj the visibility when i upload the file from a intern user account. I added a trigger to set the Visibility to AllUsers on the file in the ContentDocumentLink and it works now. 

Thank you for your time and have a nice Day :)

All Answers

Tiago Armando CoelhoTiago Armando Coelho
Can you please post the code of the component? Do you have permissions on the list?

Kind regards
Nejib KsaierNejib Ksaier
this is the code for the component: 

getAttachements: function(component) {
        var plantId = 'a0F0Y000005c7DcUAI';
        var action = component.get('c.getContractAttachedUrl');
        action.setParams({
            "plantId": plantId
        });


        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var urls = response.getReturnValue();
                component.set('v.slides', urls);
            }
        });

        $A.enqueueAction(action);
    },

and this is my apex class: 

@AuraEnabled
    public static  List<CombinedAttachment> getContractAttachedUrl(String plantId){
        
        // Get Contract Id from plant
        Date now = date.today();
        Entity_Contract_Relationship__c contractId = [Select Contract__c from Entity_Contract_Relationship__c  where Entity__c = :plantId and (Valid_to__c > :now or Valid_to__c =null) limit 1];
        
        // Get Attached Files
        List<Contract__c> RepCombinedAttachment = new List<Contract__c>();
        List<CombinedAttachment> RepComAttachment = new List<CombinedAttachment>();
        RepCombinedAttachment = [Select (Select id, title,contentUrl, LastModifiedDate, createdby.name,RecordType From CombinedAttachments) from Contract__c where id = :contractId.Contract__c];
        for(Contract__c  cc: RepCombinedAttachment){
             if(!cc.CombinedAttachments.isEmpty()) {
                 for(CombinedAttachment temp : cc.CombinedAttachments){
                     RepComAttachment.add(temp);
                 }
             }
        }
        return RepComAttachment;
    }
Tiago Armando CoelhoTiago Armando Coelho
A possible problem is on this line 

 var plantId = 'a0F0Y000005c7DcUAI';

Please don't use the Id directly in code because the id in a Sandbox can be different from Production.

Please tell if this helps you.

Kind regards, 
Nejib KsaierNejib Ksaier
this static id is from the Production, i tested the same function in the 'Execute anonymous' and i get my list. 
Tiago Armando CoelhoTiago Armando Coelho
So if you run the below query in developer console what is the result

Select Contract__c, Valid_to__c from Entity_Contract_Relationship__c  where Entity__c =  ' a0F0Y000005c7DcUAI' 

Please post a screenshoot

 
Nejib KsaierNejib Ksaier
User-added image
Nejib KsaierNejib Ksaier
ie get this result: (Entity_Contract_Relationship__c:{Contract__c=a0K0Y000005aJbjUAE, Id=a0E0Y000003qq90UAA})
Tiago Armando CoelhoTiago Armando Coelho
Sorry Nejib 

Can you run it in query editor and post the query result?
Nejib KsaierNejib Ksaier
User-added image
Tiago Armando CoelhoTiago Armando Coelho
Hi Nejib, 

Do you have any idea what is your list that is coming empty?

Kind regards, 
Nejib KsaierNejib Ksaier
this is the list i get when i execute the methode with this id in the anonymous execution : 
(CombinedAttachment:{ParentId=a0K0Y00000B0U6PUAV, Id=0690Y000004KpABQA0, Title=SGCertifiedPlatformDeveloperI (6), LastModifiedDate=2018-03-12 08:45:46, CreatedById=0050Y000002lzvKQAQ, RecordType=File})

Kind regards,
Tiago Armando CoelhoTiago Armando Coelho
Please run the following code in developper console as anonymous:

        // Get Contract Id from plant
        String plantId = 'a0F0Y000005c7DcUAI';
        Date now = date.today();
        Entity_Contract_Relationship__c contractId = [Select Contract__c from Entity_Contract_Relationship__c  where Entity__c = :plantId and (Valid_to__c > :now or Valid_to__c =null) limit 1];
        
        // Get Attached Files
        List<Contract__c> RepCombinedAttachment = new List<Contract__c>();
        List<CombinedAttachment> RepComAttachment = new List<CombinedAttachment>();
        RepCombinedAttachment = [Select (Select id, title,contentUrl, LastModifiedDate, createdby.name,RecordType From CombinedAttachments) from Contract__c where id = :contractId.Contract__c];
        for(Contract__c  cc: RepCombinedAttachment){
             if(!cc.CombinedAttachments.isEmpty()) {
                 for(CombinedAttachment temp : cc.CombinedAttachments){
                     RepComAttachment.add(temp);
                 }
             }
        }
        System.assertEquals(RepComAttachment.size(), 999999);

What are the relation between CombinedAttachments and Contract__c.

Do you have CombinedAttachments?
Nejib KsaierNejib Ksaier
i got this error: System.AssertException: Assertion Failed: Expected: 1, Actual: 999999. 
Yes i have combined attachement. In this case the Contract__c id is the parentId.
It could be an issue with permissions but i'm not sure which option should i activate in profile for the combinedattachement, because when i run the code as system admin everything looks fine, and when i use the profile i created for the community i get an empty list
Tiago Armando CoelhoTiago Armando Coelho
Ahh ok. 

So you need to go to profile and give read permissions on the object and fields Contract__c.

KInd regards, 
 
Nejib KsaierNejib Ksaier
I already did, but i could be a limitation on the salesforce customer community licence
Tiago Armando CoelhoTiago Armando Coelho
You can do the following test login as a user with the new profile. 
Try access to the record with the id  a0F0Y000005c7DcUAI
Nejib KsaierNejib Ksaier
The problem was witj the visibility when i upload the file from a intern user account. I added a trigger to set the Visibility to AllUsers on the file in the ContentDocumentLink and it works now. 

Thank you for your time and have a nice Day :)
This was selected as the best answer