3

I have an Aura component and I want to iterate through a wrapper class of objects, which has two lists as properties. While I can iterate through the wrapper class list, my lists come up as null when I try to iterate through them. My unit tests show me it's not an Apex issue--the lists are filled. But something is stopping my lists from showing as filled when I try to iterate through them. Am I missing something about how my lists need to be set? Or am I missing an Aura visibility wrinkle?

Wrapper class:

public with sharing class projectProgress {
    @AuraEnabled public integer riskCount {get;set;} //count of risks related to project
    @AuraEnabled public integer riskProgress {get;set;} //value for risk progress bar
    @AuraEnabled public integer issueCount {get;set;} //count of issues related to project
    @AuraEnabled public string projectstage {get;set;} //value for project stage
    @AuraEnabled public string cardTitle {get;set;} //value for project stage
    @auraEnabled public PSE__Proj__c project{get;set;} //current project record
    @auraEnabled public List<PSE__Risk__c> riskList{get;set;} //current Risks
    @auraEnabled public List<PSE__Issue__c> issueList{get;set;} //current Issues

    public projectProgress() {
        riskList = new List<pse__Risk__c>(); 
        issueList = new List<pse__Issue__c>();
    }
}

My Apex controller for the component generates a list of projectProgress objects from an initial SOQL of the project object, and then adds lists of related risks and issues.

public with sharing class progressPageController {

@AuraEnabled
public static List<projectProgress> initClass(){
    List<projectProgress> projectProgresses = new List<projectProgress>();
    //get projects
    Map<ID, pse__proj__c> projectsMap = new Map<ID, pse__proj__c>([SELECT Id, Name, ... FROM PSE__Proj__c WHERE ... ORDER BY ...]);        

    //get risks
    List<pse__Risk__c> risks = [SELECT Id, pse__Project__c, Name ... FROM pse__Risk__c WHERE pse__Project__c IN :projectsMap.keySet()];

    //map for holding risks by project id
    Map<Id,List<pse__Risk__c>> riskMap = new Map<Id,List<pse__Risk__c>>();

    //loop through and add to map
    for(pse__Risk__c thisRisk : risks){
        if(riskMap.get(thisRisk.pse__Project__c)==null){
            List<pse__Risk__c> thisRiskList = new List<pse__Risk__c>();
            thisRiskList.add(thisRisk);
            riskMap.put(thisRisk.pse__Project__c,thisRiskList);
        } else {
            riskMap.get(thisRisk.pse__Project__c).add(thisRisk);
        }
    }
    //get issues
    List<pse__Issue__c> issues = [SELECT Id, pse__Project__c, Name FROM PSE__Issue__c WHERE pse__Project__c IN :projectsMap.keySet()];

    //map for holding issues by project id
    Map<Id,List<pse__Issue__c>> issueMap = new Map<Id,List<pse__Issue__c>>();

    //loop through and add to map
    for(pse__Issue__c thisIssue : issues){
        if(issueMap.get(thisIssue.pse__Project__c)==null){
            List<pse__Issue__c> thisIssueList = new List<pse__Issue__c>();
            thisIssueList.add(thisIssue);
            IssueMap.put(thisIssue.pse__Project__c,thisIssueList);
        } else {
           issueMap.get(thisIssue.pse__Project__c).add(thisIssue);
        }
    }

    //loop through all projects creating projectProgress objects
    for(pse__proj__c thisProject : projectsMap.values()){
        projectProgress obj = new projectProgress();
        obj.project = thisProject;

        //set stage for everything but In Progress
        if(thisProject.pse__Stage__c <> 'In Progress'){
            obj.projectstage = thisProject.pse__Stage__c;
        }

        //add risks from the map
        if(riskMap.get(thisProject.Id)!=null){   
            obj.riskList = riskMap.get(thisProject.Id);
            obj.riskCount = obj.riskList.size();
        } else {
           obj.riskCount = 0; 
        }

        //add issues from the map
        if(issueMap.get(thisProject.Id)!=null){
            obj.issueList = issueMap.get(thisProject.Id);
            obj.issueCount = obj.issueList.size();
        } else {
           obj.issueCount = 0; 
        }

        obj.cardTitle = thisProject.Name;
        projectProgresses.add(obj);
    }
    return projectProgresses;
}
}

My JS controller is very simple, it just loads up a list of objects

({
    doInit : function(component, event, helper) {
        //call apex class method
        var action = component.get('c.initClass');

        action.setCallback(this,function(response){
            component.set('v.myProjectProgressList', response.getReturnValue());    
        });
        $A.enqueueAction(action);
    }
})

My component iterates through the wrapper class and everything works great except the lists, which are empty, even though my apex unit tests show them as having objects in them. So I think it's a visibility issue.

<aura:component controller="progressPageController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myProjectProgressList" type="List"/>
     <aura:iteration items="{!v.myProjectProgressList}" var="projectProgress">
          {!projectProgress.project.Name} //works fine
          <ul>
               <aura:iteration items="{!projectProgress.issueList}" var="issue">
                    <li>{!issue.Name}</li> //is null, doesn't show
               </aura:iteration>
          </ul>
     </aura:iteration>
 </aura:component>
5
  • Your code doesn't make it obvious, so I have to ask... did you actually query pse__Risk__c.Name in your Apex code? SOQL only returns fields that you explicitly query.
    – sfdcfox
    Commented Jun 5, 2019 at 1:34
  • On this: even though my apex unit tests show them as having objects in them -- your test data vs. org data supposedly (and should be) is different. Are you sure that when your component runs, the SOQL you have to fetch the issues returns records?
    – Jayant Das
    Commented Jun 5, 2019 at 1:55
  • Yes, I am SOQLing all the fields I am using in the component. And, in my tests, I check for risks and issues by the project id created in the test, so i'm not getting org data. Commented Jun 5, 2019 at 2:56
  • @SteveAndersen And that's exactly what I was referring to. Are you sure that the apex when called from the aura component fetches the records? A good way will be to put a system.debug and verify in the class and see if at all your list is populated with records or not.
    – Jayant Das
    Commented Jun 5, 2019 at 12:58
  • ALso can you do console.log(JSON.stringify(response.getReturnValue())); to see if you recieve result from apex? Commented Jun 5, 2019 at 13:24

2 Answers 2

0

I can see in your Apex Class you are missing Name in the SOQL.

Use below SOQL where I have added Name in the SOQL

    List<pse__Issue__c> issues = [SELECT Id, pse__Project__c, Name FROM PSE__Issue__c WHERE pse__Project__c IN :projectsMap.keySet()];

After replacing your SOQL then try to run the Lightning Component as You are {!issue.Name} in the lightning component so you must need to include Name in the SOQL.

Let me know if this works for you :)

1
  • Thanks for seeing that--it was a mistake in copying over. When I SOQL the Name field, I get the same result. The list issuesList appears to have no objects in it. Commented Jun 5, 2019 at 13:16
-1
   if(riskMap.get(thisProject.Id)!=null){   
            obj.riskList = new List<PSE_Risk__c>(riskMap.get(thisProject.Id));
            obj.riskCount = obj.riskList.size();
        } else {
           obj.riskCount = 0; 
        }

        //add issues from the map
        if(issueMap.get(thisProject.Id)!=null){
            obj.issueList = new List<PSE_Issue__c>(issueMap.get(thisProject.Id));
            obj.issueCount = obj.issueList.size();
        } else {
           obj.issueCount = 0; 
        }
1
  • 1
    Code dumps make for poor answers. Even if it is correct, it doesn't help people understand what the issue was, or help them learn how to avoid similar issues in the future. The explanation is as important as the solution itself, and the more details you can provide, the better.
    – Derek F
    Commented Feb 15 at 0:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .