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>
WS_SMSNotification wnot{get;set;}
and then initialize as per your logic.