0

I have the following code which gives me below error.

Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList

The VF Page I developed.

<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="{!caList}" var="ca">
                <apex:column headerValue="CaseNumber" value="{!ca.CaseNumber}" />
                <apex:column headerValue="Subject" value="{!ca.Subject}" />
                <apex:column headerValue="Status" value="{!ca.Status}" />
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

And Controller class

public class ApexRestController {
    public List<Case> caList {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 = 'XX';
        String CLIENT_SECRET = 'XX';
        String USERNAME = 'XX';
        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();
        return access_token;
    } 

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

        HTTPResponse res = http.send(req);
        String json = res.getBody();

        Case ca = (Case) System.JSON.deserialize(json, Case.class);
        caList = new List<Case>();
        caList.add(ca);
        return caList;
    }
}
1

Your getCaseDetailsById() should not be returning List instead it should be returning PageReference (or) Void

  • Hey Thnx working, what PageReference is only working ? – user4567570 May 15 '17 at 19:06
  • Sorry , can you please explain , i am not able to get it . – freaker87 May 15 '17 at 19:10
  • Why PageReference method es expected to the salesforce? Why List<> does not get worked ? – user4567570 May 15 '17 at 19:15
  • 1
    When action method is invoked the system invokes a GET / POST method . When you do a GET/POST method it implicitly means that the system will do a request -response . When the response comes back it may in various format xml,html,xhtml ,xhr anything which the browser has to render visually. This is one part to it . But on the other hand salesforce as a system will try to redirect you to some page after action has been performed and the data type it looks is only PageReference / void - If it solves your query mark it so that people will know that this solution may work for them - THanks – freaker87 May 15 '17 at 19:20
0

If you change the return type of your getCaseDetailsById method to PageReference, update caList and return null from the method, the page will post back with the updates to caList.

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.