I am trying to iterate over the object inside array, What I want to do is to iterate over the array and it's object and print value of inside object's key.

Below is my code:

<ul class="mainDataList">
    <apex:variable var="count" value="{!0}"/>
    <apex:repeat value="{!activity['extra']}" var="x">
            <p class="eventTitle">{!activity['extra'][count]['title']}</p>
            <p class="analyticSection">
                    <apex:outputText value="{! IF((activity['extra'][count]['section'] == null), 'NA',  activity['extra'][count]['section']) }" /> 
            <div class="thumbAndEvents">
        <apex:variable value="{!count+1}" var="count"/>

I am getting "Content cannot be displayed: Map key section not found in map"

If it is null/empty then don't print anything else print the value which is an inside object.

EDIT: Apex Response Model:

public SObject extra { get; set; } 

public static Object parse(String input)
        return  JSON.deserializeUntyped(input);

I have specified extra key as a sobject and method return value as Object. I had to change it because I have inconsistent data in that perticular key and by doing this way I am able to achieve what I wanted.

  • Can you add your apex controller as well – Lokesh Jul 22 '19 at 7:51
  • What is activity? And extra can't be used as the value of a repeat tag if it's represented as a single SObject in the controller. In the sample data in your comment below, it looks like it's an array/list of objects with multiple attributes. – Thomas Taylor Jul 22 '19 at 10:43
  • the 'activity' has lots of key-value pair data but I am facing problem in just 'extra' key which has inconsistent data – Mysterious Coder Jul 22 '19 at 11:01
  • The Apex snippet doesn't show what the extra SObject is being populated with. While repeat can take any Object, it is usually used to iterate over an Apex collection, like a Map or Set. – Thomas Taylor Jul 22 '19 at 11:14
  • Is it possible the way I am doing? – Mysterious Coder Jul 22 '19 at 11:23

You shouldn't use <apex:variable> in that way. From the documentation:

Note: <apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.

As Lokesh said, a snippet of your controller, or at least a clear explanation of the data structure you're trying to iterate over, is needed to give you much additional guidance. But you've defined the <apex:repeat>'s var as x, but then do not make any reference to x inside of the <apex:repeat>. It seems like the display lines inside the repeat should probably look something like:

<p class="eventTitle">{!x['title']}</p>
<p class="analyticSection">
<strong><apex:outputText value="{!IF((x['section'] == null),'NA',
         x['section']) }" /> </strong>

But we'd need to know more about the data structure in activity to say more.

  • I have below type of data to iterate: extra:[ {duration=, title=New presentation started, section=, hasrating=-1, thumb=null, extra=null}{duration=28, title=Surveys, section=null, hasrating=-1, thumb=null, extra=null} ] – Mysterious Coder Jul 22 '19 at 8:36
  • I want to iterate over the extra array and get each objects' value – Mysterious Coder Jul 22 '19 at 8:43
  • Please edit your question and add the information there, so that other answerers can see it. Please also specify the Apex collection type & what the elements' data types are. But probably you want {!x.title} and {!x.section} – Thomas Taylor Jul 22 '19 at 8:46
  • It is not working I have tried it – Mysterious Coder Jul 22 '19 at 10:24
  • That's why both Lokesh and I asked you to edit your question (rather than adding another comment) to specify the details of the data structure in the controller. – Thomas Taylor Jul 22 '19 at 10:30

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.