• logandj
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

I have a controller which is querying a webservice and returns a JSON formatted response that I need to then display in a VF table on a Contact record. I have the query working and then use JSON.deserialize to put the response into a list. I have a pageBlockTable setup right now  to display a few columns from the response and the values but to properly display anything I've had to hardcode the list position in each value (listName[4] etc..) and when doing so the list values auto-repeat until the row limit defined in the table. I've thought about just building out a very large table with the unique list reference positions (they won't change, only the data in the response) but figured there must be a better way. 

 

How can I take this list and autopopulate the table with the values, ideally using the headers provided in the JSON structure? The structure will stay the same but the contents will change based on a value in the Contact record.  Would using the class deserialize method work better? I've tried to get that to work but I've been unsuccessful. 

 

Thanks!

 

Current VF Page (with hard-coded list references to display something...):

 

<apex:page controller="MemberRestAPI">
    <apex:form >
        <apex:pageblock >
            <apex:commandButton value="Update" onclick="getMemberTransactions(); return false;"/>
            </apex:pageBlock>
            <apex:pageBlock >
            <apex:outputPanel id="RecentTransPanel" layout="block" style="margin-bottom: 5px;width:100%">
                <apex:pageBlockTable value="{!MemberTransactions2}" var="MemberTransactions" id="transactionTable" rows="5">
                
                <apex:column >
                <apex:facet name="header">Transaction Date</apex:facet>
                <apex:outputText value="{!MemberTransactions2[6]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Reference Id</apex:facet>
                <apex:outputText value="{!MemberTransactions2[4]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Retailer Name</apex:facet>
                <apex:outputText value="{!MemberTransactions2[12]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Site ID</apex:facet>
                <apex:outputText value="{!MemberTransactions2[14]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Site Name</apex:facet>
                <apex:outputText value="{!MemberTransactions2[16]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Card Number</apex:facet>
                <apex:outputText value="{!MemberTransactions2[26]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Transaction Type</apex:facet>
                <apex:outputText value="{!MemberTransactions2[28]}"/>
                </apex:column>
                
                </apex:pageBlockTable>
            </apex:outputPanel>
            </apex:pageblock>
            
<!-- Debug section --> <apex:pageBlock > <apex:outputText value="{!MemberTransactions2}" id="transactionValues" /> </apex:pageBlock> </apex:form> </apex:page>

 Current Controller Code:

 

public with sharing class MemberRestAPI {

public pageReference getMemberTransactions() {
        getMemberTransactions2();
        return null;
}
    
public List<String> getMemberTransactions2() {
     
     //For Execute Anonymous only
     String accessToken = '7064f046-eb0b-49ad-a4fd-11978e6d2191';
     String memberId = '2375070';
        
     HttpRequest req = new HttpRequest();
    req.setEndpoint('URL/public/rest/transactions?programId=320&accountNumber='+memberId);
     req.setMethod('GET');
     req.setHeader('Content-type', 'application/json');
     req.setHeader('access_token', accessToken); 
  
     Http http = new Http();
     System.debug(req.getBody());

     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
    
    //String mTs1 = res.getBody();
    String Transactions = res.getBody();
    //String Transactions = mTs1.replaceAll('transaction', 'transactions');

    List<String> MemberTransactions = (List<String>)JSON.deserialize(Transactions, List<String>.class);
    
    
    System.debug(MemberTransactions);
    
    return MemberTransactions;
   
}
   
//end Class   
}

 

Sample List:

 

[{, transaction, {, referenceId, 1432023700, transactionDateTime, 2012-10-31 11:08:17.0 EDT, postDateTime, 2012-10-31 11:08:35.316 EDT, retailerId, 10001, retailerName, Test, siteId, 10001-1, siteName, Test, transactionAmount, , rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-01 23:59:59.999 EDT, cardNumber, *9010, transactionType, Adjustment, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023187, transactionDateTime, 2012-10-31 11:04:33.0 EDT, postDateTime, 2012-10-31 11:05:02.179 EDT, retailerId, 10001, retailerName, Test, siteId, 10001-1, siteName, Test, transactionAmount, , rewardEarned, 0.02, rewardRedeemed, , expirationDate, 2012-11-01 23:59:59.999 EDT, cardNumber, *9010, transactionType, Adjustment, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023697, transactionDateTime, 2012-10-31 10:05:40.88 CST, postDateTime, 2012-10-31 11:07:30.445 CST, retailerId, 49000, retailerName, Test, siteId, 49000-4, siteName, Test, transactionAmount, 2.00, rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-30 23:59:59.999 CST, cardNumber, *9010, transactionType, Issuance, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023293, transactionDateTime, 2012-10-31 10:05:40.88 CST, postDateTime, 2012-10-31 11:07:28.105 CST, retailerId, SH2196, retailerName, Test, siteId, 57441911203, siteName, Test, transactionAmount, 2.00, rewardEarned, , rewardRedeemed, 0.05, expirationDate, , cardNumber, *9010, transactionType, Redemption, categoryType, Fuel Stations, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023159, transactionDateTime, 2012-10-31 09:45:40.88 CST, postDateTime, 2012-10-31 11:02:38.822 CST, retailerId, SH2196, retailerName, Test, siteId, 57441911203, siteName, Test, transactionAmount, 2.00, rewardEarned, , rewardRedeemed, 0.03, expirationDate, , cardNumber, *9010, transactionType, Redemption, categoryType, Fuel Stations, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023286, transactionDateTime, 2012-10-31 09:45:40.88 CST, postDateTime, 2012-10-31 11:02:40.036 CST, retailerId, 49000, retailerName, Test, siteId, 49000-4, siteName, Test, transactionAmount, 2.00, rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-30 23:59:59.999 CST, cardNumber, *9010, transactionType, Issuance, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1430059132, transactionDateTime, 2012-08-03 23:18:00.0 CDT, postDateTime, 2012-08-04 00:17:40.784 CDT, retailerId, part1, retailerName, part1 - iss/red, siteId, SiteA, siteName, SiteA - iss/red, transactionAmount, 20.33, rewardEarned, 2.05, rewardRedeemed, , expirationDate, 2012-10-31 23:59:59.999 CDT, cardNumber, *1011, transactionType, Issuance, categoryType, Participant Programs, siteNameFlag, true, }, }, {, transaction, {, referenceId, 1427148165, transactionDateTime, 2012-05-15 17:33:48.337 EDT, postDateTime, 2012-05-15 17:33:48.362 EDT, retailerId, part1, retailerName, part1 - iss/red, siteId, part1-2, siteName, Promotion Code Activation, transactionAmount, 0.00, rewardEarned, 0.20, rewardRedeemed, , expirationDate, 2012-07-31 23:59:59.999 EDT, cardNumber, *1011, transactionType, Issuance, categoryType, Participant Programs, siteNameFlag, true, }, }]

 The deserialize method seems to not remove the brackets which seems weird so i'll probably need to add a line to remove those. 

 

JSON Response Sample: 

 

[
{
"transaction":{
"referenceId":1432023700,
"transactionDateTime":"2012-10-31 11:08:17.0 EDT",
"postDateTime":"2012-10-31 11:08:35.316 EDT",
"retailerId":"10001",
"retailerName":"Test",
"siteId":"10001-1",
"siteName":"Test",
"transactionAmount":"",
"rewardEarned":"0.03",
"rewardRedeemed":"",
"expirationDate":"2012-11-01 23:59:59.999 EDT",
"cardNumber":"*9010",
"transactionType":"Adjustment",
"categoryType":"-",
"siteNameFlag":false
}
}
]

 Ideally I don't need to display everything in the response on the page but will if needed. I'd like to change the headers so they display as a normal name and not a variable name and the table will display a maximum of 20 rows (20 transactions). 

 

Screenshot of VF Page:

 

http://i.imgur.com/dqPtPRA.png

 

 

I have a controller which is querying a webservice and returns a JSON formatted response that I need to then display in a VF table on a Contact record. I have the query working and then use JSON.deserialize to put the response into a list. I have a pageBlockTable setup right now  to display a few columns from the response and the values but to properly display anything I've had to hardcode the list position in each value (listName[4] etc..) and when doing so the list values auto-repeat until the row limit defined in the table. I've thought about just building out a very large table with the unique list reference positions (they won't change, only the data in the response) but figured there must be a better way. 

 

How can I take this list and autopopulate the table with the values, ideally using the headers provided in the JSON structure? The structure will stay the same but the contents will change based on a value in the Contact record.  Would using the class deserialize method work better? I've tried to get that to work but I've been unsuccessful. 

 

Thanks!

 

Current VF Page (with hard-coded list references to display something...):

 

<apex:page controller="MemberRestAPI">
    <apex:form >
        <apex:pageblock >
            <apex:commandButton value="Update" onclick="getMemberTransactions(); return false;"/>
            </apex:pageBlock>
            <apex:pageBlock >
            <apex:outputPanel id="RecentTransPanel" layout="block" style="margin-bottom: 5px;width:100%">
                <apex:pageBlockTable value="{!MemberTransactions2}" var="MemberTransactions" id="transactionTable" rows="5">
                
                <apex:column >
                <apex:facet name="header">Transaction Date</apex:facet>
                <apex:outputText value="{!MemberTransactions2[6]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Reference Id</apex:facet>
                <apex:outputText value="{!MemberTransactions2[4]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Retailer Name</apex:facet>
                <apex:outputText value="{!MemberTransactions2[12]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Site ID</apex:facet>
                <apex:outputText value="{!MemberTransactions2[14]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Site Name</apex:facet>
                <apex:outputText value="{!MemberTransactions2[16]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Card Number</apex:facet>
                <apex:outputText value="{!MemberTransactions2[26]}"/>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Transaction Type</apex:facet>
                <apex:outputText value="{!MemberTransactions2[28]}"/>
                </apex:column>
                
                </apex:pageBlockTable>
            </apex:outputPanel>
            </apex:pageblock>
            
<!-- Debug section --> <apex:pageBlock > <apex:outputText value="{!MemberTransactions2}" id="transactionValues" /> </apex:pageBlock> </apex:form> </apex:page>

 Current Controller Code:

 

public with sharing class MemberRestAPI {

public pageReference getMemberTransactions() {
        getMemberTransactions2();
        return null;
}
    
public List<String> getMemberTransactions2() {
     
     //For Execute Anonymous only
     String accessToken = '7064f046-eb0b-49ad-a4fd-11978e6d2191';
     String memberId = '2375070';
        
     HttpRequest req = new HttpRequest();
    req.setEndpoint('URL/public/rest/transactions?programId=320&accountNumber='+memberId);
     req.setMethod('GET');
     req.setHeader('Content-type', 'application/json');
     req.setHeader('access_token', accessToken); 
  
     Http http = new Http();
     System.debug(req.getBody());

     HTTPResponse res = http.send(req);
     System.debug(res.getBody());
    
    //String mTs1 = res.getBody();
    String Transactions = res.getBody();
    //String Transactions = mTs1.replaceAll('transaction', 'transactions');

    List<String> MemberTransactions = (List<String>)JSON.deserialize(Transactions, List<String>.class);
    
    
    System.debug(MemberTransactions);
    
    return MemberTransactions;
   
}
   
//end Class   
}

 

Sample List:

 

[{, transaction, {, referenceId, 1432023700, transactionDateTime, 2012-10-31 11:08:17.0 EDT, postDateTime, 2012-10-31 11:08:35.316 EDT, retailerId, 10001, retailerName, Test, siteId, 10001-1, siteName, Test, transactionAmount, , rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-01 23:59:59.999 EDT, cardNumber, *9010, transactionType, Adjustment, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023187, transactionDateTime, 2012-10-31 11:04:33.0 EDT, postDateTime, 2012-10-31 11:05:02.179 EDT, retailerId, 10001, retailerName, Test, siteId, 10001-1, siteName, Test, transactionAmount, , rewardEarned, 0.02, rewardRedeemed, , expirationDate, 2012-11-01 23:59:59.999 EDT, cardNumber, *9010, transactionType, Adjustment, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023697, transactionDateTime, 2012-10-31 10:05:40.88 CST, postDateTime, 2012-10-31 11:07:30.445 CST, retailerId, 49000, retailerName, Test, siteId, 49000-4, siteName, Test, transactionAmount, 2.00, rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-30 23:59:59.999 CST, cardNumber, *9010, transactionType, Issuance, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023293, transactionDateTime, 2012-10-31 10:05:40.88 CST, postDateTime, 2012-10-31 11:07:28.105 CST, retailerId, SH2196, retailerName, Test, siteId, 57441911203, siteName, Test, transactionAmount, 2.00, rewardEarned, , rewardRedeemed, 0.05, expirationDate, , cardNumber, *9010, transactionType, Redemption, categoryType, Fuel Stations, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023159, transactionDateTime, 2012-10-31 09:45:40.88 CST, postDateTime, 2012-10-31 11:02:38.822 CST, retailerId, SH2196, retailerName, Test, siteId, 57441911203, siteName, Test, transactionAmount, 2.00, rewardEarned, , rewardRedeemed, 0.03, expirationDate, , cardNumber, *9010, transactionType, Redemption, categoryType, Fuel Stations, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1432023286, transactionDateTime, 2012-10-31 09:45:40.88 CST, postDateTime, 2012-10-31 11:02:40.036 CST, retailerId, 49000, retailerName, Test, siteId, 49000-4, siteName, Test, transactionAmount, 2.00, rewardEarned, 0.03, rewardRedeemed, , expirationDate, 2012-11-30 23:59:59.999 CST, cardNumber, *9010, transactionType, Issuance, categoryType, -, siteNameFlag, false, }, }, {, transaction, {, referenceId, 1430059132, transactionDateTime, 2012-08-03 23:18:00.0 CDT, postDateTime, 2012-08-04 00:17:40.784 CDT, retailerId, part1, retailerName, part1 - iss/red, siteId, SiteA, siteName, SiteA - iss/red, transactionAmount, 20.33, rewardEarned, 2.05, rewardRedeemed, , expirationDate, 2012-10-31 23:59:59.999 CDT, cardNumber, *1011, transactionType, Issuance, categoryType, Participant Programs, siteNameFlag, true, }, }, {, transaction, {, referenceId, 1427148165, transactionDateTime, 2012-05-15 17:33:48.337 EDT, postDateTime, 2012-05-15 17:33:48.362 EDT, retailerId, part1, retailerName, part1 - iss/red, siteId, part1-2, siteName, Promotion Code Activation, transactionAmount, 0.00, rewardEarned, 0.20, rewardRedeemed, , expirationDate, 2012-07-31 23:59:59.999 EDT, cardNumber, *1011, transactionType, Issuance, categoryType, Participant Programs, siteNameFlag, true, }, }]

 The deserialize method seems to not remove the brackets which seems weird so i'll probably need to add a line to remove those. 

 

JSON Response Sample: 

 

[
{
"transaction":{
"referenceId":1432023700,
"transactionDateTime":"2012-10-31 11:08:17.0 EDT",
"postDateTime":"2012-10-31 11:08:35.316 EDT",
"retailerId":"10001",
"retailerName":"Test",
"siteId":"10001-1",
"siteName":"Test",
"transactionAmount":"",
"rewardEarned":"0.03",
"rewardRedeemed":"",
"expirationDate":"2012-11-01 23:59:59.999 EDT",
"cardNumber":"*9010",
"transactionType":"Adjustment",
"categoryType":"-",
"siteNameFlag":false
}
}
]

 Ideally I don't need to display everything in the response on the page but will if needed. I'd like to change the headers so they display as a normal name and not a variable name and the table will display a maximum of 20 rows (20 transactions). 

 

Screenshot of VF Page:

 

http://i.imgur.com/dqPtPRA.png