0

I have an apex callout that sends an sql query to an external MySQL server. The query output is returned in the response body and I could see that retrieved correctly in the console. But when the JSON string returned from the external server is de-serialized and coded to an a custom object list and added as a pageblocktable content, it is not getting rendered. I am not able to see any column values. The list data is correctly shown with values in console.

Can anyone please help me to find where the problem is?

VF Code - show_stores.vfp

<apex:page controller="query">
    <apex:form >
      <apex:pageBlock title="Queries">
        <apex:commandButton value="View All Stores"  action="{!sendQuery}" reRender="table1" />
      </apex:pageBlock>
      <apex:pageBlock id="table1">
        <apex:pageBlockTable value="{!result}" var="res">
            <apex:column value="{!res.storeNo}" headerValue="Store No"/>
            <apex:column value="{!res.storeName}" headerValue="Store Name"/>          
         </apex:pageBlockTable>    
       </apex:pageBlock>
     </apex:form>
</apex:page>

Controller - query.apxc

    public  String queryString{get;set;}
    public  List<myObj> result{get;set;}
    
    public pageReference sendQuery(){
        string s;
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Accept','application/json');
        req.setEndpoint('https://www.example.com/endpointselect.php');
        query x = new query();
        x.queryString = 'Select * from test';
        String JSONString = JSON.serialize(x);
        system.debug('Sending string'+JSONString);
        req.setBody(JSONString);
        HTTPResponse res = http.send(req);
        s=res.getBody();
        system.debug('Receiving string:'+s);
        List<myObj> result = (List<myObj>)JSON.deserialize(s, List<myObj>.class);
        system.debug(result);
       //return result;
        return null;
    }    
}

myObj class - myObj.apxc

global class myObj {
    public string storeNo{get;set;}
    public string storeName{get;set;}   
       
    public void insert1(){
        myObj x = new myObj();
        x.storeNo = this.StoreNo;
        x.StoreName = this.StoreName;
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://www.example.com/endpoint.php');
        req.setMethod('POST');
        String JSONString = JSON.serialize(x);
        system.debug(JSONString);
        req.setBody(JSONString);
        HTTPResponse res = http.send(req);
    }
}
0

You are trying to iterate over a List<myObj> which wont work like that.

Try this, I have removed all the callouts and made an simple example:

public String queryString { get; set; }
public List<myObj> result { get; set; }
public List<Integer> indexes { get; set; }

public pageReference sendQuery() {
    string s;
    queryString = 'This is life';
    List<myObj> result = (List<myObj>) JSON.deserialize('[{"storeNo":"123","storeName":"TestName"}]', List<myObj>.class);
    this.result = result;
    this.indexes = new List<Integer>();
    for (Integer i = 0; i < result.size(); i++) {
        this.indexes.add(i);
    }
    system.debug(result);
    //return result;
    return null;
}

public class myObj {
    public string storeNo { get; set; }
    public string storeName { get; set; }
}

and you VFPage as :

<apex:page controller="DeleteAllBigObject">
    <apex:form >
        <apex:pageBlock title="Queries">
            <apex:commandButton value="View All Stores"  action="{!sendQuery}" reRender="table1">
            </apex:commandButton>
        </apex:pageBlock>
        <apex:pageBlock id="table1">
            <apex:pageBlockTable value="{!Indexes}" var="i">
                <apex:column value="{!result[i].storeNo}" headerValue="Store No"/>
                <apex:column value="{!result[i].storeName}" headerValue="Store Name"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
1
  • Thanks Nagendra. It worked.
    – Kiran G
    Nov 26 '20 at 13:14

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.