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;
}
}
Best Answer
Your getCaseDetailsById() should not be returning List instead it should be returning PageReference (or) Void