I recently posted a question regarding a component I am building. That post has the code I will be referencing.

I have been able to make the necessary edits and now I return an sObject to the serverResult attribute, which is also listed as an object type now.

My question is this: how can I pull the correct information out of serverResult? Here is the method I am currently using:

<aura:iteration items="{!v.serverResult}" var="item">
            <li class="slds-item">
                <div class="slds-tile slds-tile--board">
                    <h3 class="slds-truncate"><a href="{! '#/sObject/' + item.Id}"> {!v.objectName}: {!item.Name}</a>  </h3>
                    <div class="slds-tile__detail slds-text-body--small">

                        <p>{!v.field1Label}: {!item.field1} <br/>
                        {!v.field2Label}: {!item.field1} <br/>
                        {!v.field3Label}: {!item.field1}</p> <br/>                            


I can get item.id and item.name to work perfectly because I can do a direct reference to them. The whole idea of this component is that I can use it again on a different object, and thus a different set of field references. Hence why the fields are named field1, etc.

The problem is that {!item.field1} won't return anything because (obviously) field1 is not an actual field name on that object. How can I make [field1] dynamic so I can have it actually look for the field I defined in the field1 attribute?

I tried <p>{!v.field1Label}: "{!item."+field1+"}" <br/> and <p>{!v.field1Label}: <ui:outputText value="{!item.'+v.field1'"/> <br/> but those are throwing an error.


Your best bet here would be to use a custom apex class, create the contents of that class in your apex method and finally use that on your component.

So let's say you have a custom class as:

ServerResult {
    public string field1Label{get;set;}
    public string field1Value{get;set;}
    public string field2Label{get;set;}
    public string field2Value{get;set;}

You can define a server method where you can fetch data as required from different object, and then create a list of this custom class as below:

public static List<ServerResult> getResults() {
    List<ServerResult> myrecList = new List<ServerResult>();
    // all your custom logic to fetch records and populate this list
    return myrecList;

Finally, in your JS controller where you call the aura apex method, you can have it retrieved as:

// all other codes
component.set("v.serverResult", response.getReturnValue());

Finally use as you are rendering it in your component.

  • Still he has to show fieldLabel:fieldValue pair right... He's trying to create apex:releatedList VF implementation in SF. – Pranay Jaiswal May 1 '18 at 19:09
  • The custom bean class can be extended for that purpose. Updated in my answer. – Jayant Das May 1 '18 at 19:11
  • Forgive me if my understanding is incorrect, but I think that was exactly what I started with. I created a wrapper class named tileWrapper and returned that to a list attribute exactly the way you are showing. The problem I was having is that when I wanted to get that information into my aura:iteration, the fields were returning blank. I could not even get item.name to display. I switched to List<sObject> and suddenly I could get item.name to show. I thought it was a data type error or something? – Chance May 1 '18 at 19:25
  • @Chance So if you had a "name" variable in your wrapper class, you should have been able to use that. Note that the variable names accessed on the component/JS are case sensitive, so you may like to check on that front. – Jayant Das May 1 '18 at 19:28
  • 1
    Yes if you did not already have it in there. All variables in the class that you plan to use on the component need to be auraenabled. – Jayant Das May 1 '18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.