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
Rowan  ChristmasRowan Christmas 

Returning custom objects for an aura:iteration

Hey kind folks,

I'm trying to figure out how to return a custom class for an aura:iteration. There are some things (specfically TopicAssignments) that I don't have a relationship to from my object, so I can use the SOQL join functions.

I am doing the following in apex:
 
// make a class that will get returned and has the reference to the data I need
public class InsightData {
        
        @AuraEnabled
        public Insight__c insight;
        
        @AuraEnabled
        public String test_name;
        
        @AuraEnabled
        public String LongName;
}

// the apex code for building and returning the data objects
// just doing test data for now while I try to figure this out
public class InsightsController {

     @AuraEnabled
    public static List<InsightData> getInsights() {
        List <Insight__c> insights = [SELECT Id, Long_Name__c, Data_Source__c, Details__c, LastModifiedDate,
                Data_Source_Icon_URL__c, Directly_Responsible_User__c, OwnerId, Chart__c
                FROM Insight__c ORDER BY CreatedDate DESC];
        
        List <InsightData> dataList = new List <InsightData> ();
        for (Integer i = 0; i < dataList.size(); i++) {
            Insight__c insight = insights[i];
            InsightData data = new InsightData();
            data.insight = insight;
            data.LongName = insight.Long_Name__c;
            data.test_name = 'Insight #:'+i;
            dataList[i] = data;
        }
        return dataList;
    }
}

From what I've read online this should work... if there is a better way to do otherwise unallowable JOINs, I'd love to hear about the solution as well.

Thanks!
 
bob_buzzardbob_buzzard
That's the mechanism that I would use if I can't simply return a collection of sobjects. The component will happily iterate over a custom class, and you can specify that as the type of the attribute in the component.
Adam Gill 17Adam Gill 17
Hi Rowan - did you ever solve this? Your code example helped me fix a problem I had (I'd forgotten to decalre my properties on my custom class as Public and @AuraEnabled) - but just looking at your code the loop where it transfers data from the original List<InsightData> to dataList is incorrect. As dataList is empty, it will never iterate....you should be iterating through the List<InsightData> - e.g.
 
int i = 1;
for (Insight__c insight : insights)
{
            InsightData data = new InsightData();
            data.insight = insight;
            data.LongName = insight.Long_Name__c;
            data.test_name = 'Insight #:'+i;
            dataList.Add(data);
            i = i+1;
}