0

I am calling the REST GET method and trying to display the result of the REST call in Visualforce page. I developed the code so far but facing small error.

Unknown property 'ApexRestController.cases'

It would be better to display result on page load only. How ?

When I tried using Postman I got the following response which I want to show to client using Visualforce page.

{"attributes":{"type":"Case","url":"/services/data/v37.0/sobjects/Case/5007F000000qqvCQAQ"},
"CaseNumber":"00001047",
"Subject":"Bigfoot Sighting!",
"Status":"Working",
"Priority":"Medium",
"Id":"5007F000000qqvCQAQ"}

The code I developed so far:

CaseDetails

public class CaseDetails {
    public String CaseNumber {get; set;}
    public String Subject {get; set;}
    public String Status {get; set;}
    public String Priority {get; set;}
    public String Id {get; set;}
}

ApexRestVFPage

<apex:page controller="ApexRestController">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton reRender="table1" value="Get Case Details" action="{!getCaseDetailsById}"/>
        </apex:pageBlock>

        <apex:pageBlock id="table1">
            <b>Output Contact Details </b>
            <apex:pageBlockTable value="{!cases}" var="c">
                <apex:column headerValue="CaseNumber" value="{!c.CaseNumber}" />
                <apex:column headerValue="Subject" value="{!c.Subject}" />
                <apex:column headerValue="Status" value="{!c.Status}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

ApexRestController

public class ApexRestController {
    public static List<CaseDetails> cases {get; set;}


    public static String getAccessToken(){
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');
        req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');

        String CLIENT_ID = 'XXXXX';
        String CLIENT_SECRET = 'XXX';
        String USERNAME = 'XXX';
        String PASSWORD = 'XX';

        req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
            '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);

        Http http = new Http();
        HTTPResponse res = http.send(req);

        String access_token = res.getBody();
        List<String> access_tokenList = new List<String>();
        access_tokenList  = access_token.split(',',-1);
        system.debug(access_tokenList[0]);
        access_tokenList  = access_tokenlist[0].split(':',-1);
        system.debug(access_tokenList[1]);
        String tokennew = access_tokenList[1].replace('"','');
        system.debug(tokennew);
        return tokennew;
    } 

    public static CaseDetails getCaseDetailsById(){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://ap5.salesforce.com/services/apexrest/Cases/XXXXX');
        req.setMethod('GET');
        req.setHeader('Authorization', 'Bearer '+getAccessToken());

        HTTPResponse res = http.send(req);
        System.debug(res.getBody());

        cases = (List<CaseDetails>) JSON.deserialize(res.getBody(), List<CaseDetails>.class);
        return cases[0];
    }
}
1

The issue is you have declared the variable as static

public static List<CaseDetails> cases {get; set;}

And you are trying to access that from

<apex:pageBlockTable value="{!cases}" var="c">

That's the reason it is giving Unknown property 'ApexRestController.cases' error.

Correct usage

public List<CaseDetails> cases {get; set;}
0

Static variables are not serialized (as if you'd used the "transient" keyword), meaning they will be "lost" between each trip back to the server. I would recommend removing all of the static keywords in your code. None of them are necessary for your code to function.

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.