2

I have the following class, that implements 2 custom inner classes, with a list of one type inside another class. What this does is do a callout to an external REST service, gets back a JSON response that contains 1 element of Output, and inside that output is a list of SMS class elements. I've done the callouts successfully, parsed the class ok, verified with system.debug that I'm getting the list elements correctly. What I'm stumped on, is: How do I refer to these elements in the DataTable on the page, based on my class structure?

Right now it's: Inner class Output -> contains list of 1 or more SMS inner class elements. I've tried:
wnot.output.SMS[i].parameter name wnot.output[i].parameter name SMS[i].parameter name

All give me 'Unknown property' error. Is my inner class setup borked and maybe I should rethink this. This is a sort of test for me (never did a class that created inner class objects for display on a VF page) . Do I need a wrapper class for this setup or what?

public with sharing class WS_SMSNotification {

    //getters / setters 
     public String mobileNumber {get;set;}
     public String months {get;set;}
     public List<SMS> smsList {get;set;}
     public String descr {get; set;}

     public WS_SMSNotification()
      {
      mobileNumber='';
      months='';
      smsList=new List<SMS>();
      }


  public class Output {
          public String xmlns_ns0;
          public List<SMS> SMS;
     }

     public Output output;

     public class SMS {
          public String Text;
          public String Status;
          public String Type;
          public String CreationDate;
     } 

     //public SMS sms;



    /*  Parse function to generate instances of the output class, which contains 1 or more instances of the inner SMS class  */


       public static WS_SMSNotification parse(String json) {
               String json2 = json.replace('ns0:', '');  // since strings are immutable 
               system.debug(json2);
               return (WS_SMSNotification)System.JSON.deserialize(json2, VF_WS_SMSNotification.class);
     }       

/* Get method for the VF page  */
 public List<WS_SMSNotification.SMS> getlist(String phoneno, String months){

    /*  Code omitted that does callout and retrieves JSON to parse */


  String jsonResponse = res.getBody();


 WS_SMSNotification wnot = new WS_SMSNotification();
   wnot = parse(jsonResponse);

         List<WS_SMSNotification.SMS> ist = new List<VF_WS_SMSNotification.SMS>();
        for(WS_SMSNotification.SMS o: wnot.output.SMS){
              ist.add(o);   //adding each SMS element to the list
         }
  return ist;
  }   
 }

Page:

  <apex:page showHeader="false" sidebar="false" controller="VF_WS_SMSNotification">
    <apex:sectionHeader title="SMS List" subtitle="List" />
      <apex:pageBlock>
      <apex:outputPanel>
      <apex:form>
      <apex:commandButton value="pressMe!" id="theButton" action="{!getList}" />
      </apex:form>
     </apex:outputPanel>                    
    </apex:pageBlock>
<apex:pageBlock id="table" title="Notifications">
<apex:dataTable value="{!smsList}" var="i" id="theTable" rowClasses="odd,even"
<apex:facet name="caption">table caption</apex:facet>
    <apex:facet name="header">table header</apex:facet>
<apex:facet name="footer">table footer</apex:facet>
<apex:column>
<apex:facet name="header">Status</apex:facet>
<apex:facet name="footer">column footer</apex:facet>
<apex:outputText value="{!wnot.SMS[i].Status}"/>
</apex:column>
<apex:column>
<apex:facet name="header">Text</apex:facet>
<apex:facet name="footer">column footer</apex:facet>
<apex:outputText value="{!wnot.sms[i].Text}"/>
</apex:column>
<apex:column>
<apex:facet name="header">Type</apex:facet>
<apex:facet name="footer">column footer</apex:facet>
<apex:outputText value="{!wnot.sms[i].Type}"/>
</apex:column>
<apex:column>
<apex:facet name="header">Date</apex:facet>
          <apex:facet name="footer">column footer</apex:facet>
         <apex:outputText value="{!wnot.sms[i].CreationDate}"/>
    </apex:column>
   </apex:dataTable>
    </apex:pageBlock>
</apex:page>
1
  • 1
    Regardless it's a wrapper class instance or not, you need a getter to bind that with the visualforce page like WS_SMSNotification wnot{get;set;} and then initialize as per your logic.
    – highfive
    Commented Sep 25, 2014 at 3:35

1 Answer 1

5

It looks like you have misunderstood how the var attribute of apex:dataTable works.

The var attribute gives you to equivalent of your loop variable in a for-each loop.

e.g. in your case you've used i for you var attribute, in Apex that would look like this.

for(SMS i : smsList)
{
    // Use i within the loop
}

In this case you would operate directly on i in your loop. This is exactly the same with an apex:dataTable.

To cut a long story short, rather than using wnot.SMS[i].Status to reference the list items within your data table, you should switch to using just i.Status within the context of the apex:dataTable.

Edit:

In addition to this you need to change the fields on your inner class to properties instead so that they can be accessed in Visualforce.

public class SMS 
{
    public String Text { get; set; }
    public String Status { get; set; }
    public String Type { get; set; }
    public String CreationDate { get; set; }
} 
5
  • Yeah, that doesn't work. The VF page still throws errors that it doesn't recognize a variable called 'WS_SMSNotification.SMS.Status Unknown property 'WS_SMSNotification.SMS.Status could this be because the inner class element SM
    – akarnid
    Commented Sep 25, 2014 at 11:33
  • @akarnid See my edit. Commented Sep 25, 2014 at 11:36
  • Quick edits ahoy! I see you have edited your answer Alex so I'll edit my comment. Facepalm for me for not realizing that I need to declare the {get;set;} inside of the class definition.
    – akarnid
    Commented Sep 25, 2014 at 11:43
  • @akarnid I take it you managed to get this working then? Commented Sep 27, 2014 at 17:15
  • Yeah, this part of the riddle is solved. What I'm dealing with now are values from an apex:inputtext field not being set in the controller. That's a totally different problem.
    – akarnid
    Commented Sep 28, 2014 at 23:10

You must log in to answer this question.

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