+ Start a Discussion
laura.mckevitt@bakertilly.comlaura.mckevitt@bakertilly.com 

JSON Class Error : Non-void Method

Hello,

 

I am trying to construct a class that gathers a list of specific accounts in Salesforce, serialize in JSON, and then I can display my string in Visualforce. I am most likely forgetting something here, but any help would be much appreciated...the following error I get is: Non-void method might not return a value or might have statement after a return statement.

 

APEX CLASS:

public with sharing class JSONController {

public String jsonString {get;set;}

//Constructor
public JSONController()
{
jsonString = getAccounts();
}

public String getAccounts(){
List<Account> accounts = [
SELECT Name, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, Phone, Website, Latitude__c, Longitude__c
FROM Account
WHERE Latitude__c != null
AND Longitude__c != null
AND Account_Status__c = 'Active'
AND RecordTypeId in (SELECT Id FROM RecordType WHERE Name = 'Retail Account')
];
String accountsJSON = JSON.serializePretty(accounts);
}
}

 

VF PAGE:

<apex:page controller="JSONController" contentType="application/x-JavaScript; charset=utf-8"
showHeader="false" standardStylesheets="false" sidebar="false">
{!jsonString}
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

your getAccounts() method says it returns a string, but has no return statement in it. (missing return accountsJSON at the end).

 

also, in your query you can just do where ... and RecordType.Name = 'Retail Account' no need for the sub-query.

All Answers

SuperfellSuperfell

your getAccounts() method says it returns a string, but has no return statement in it. (missing return accountsJSON at the end).

 

also, in your query you can just do where ... and RecordType.Name = 'Retail Account' no need for the sub-query.

This was selected as the best answer
laura.mckevitt@bakertilly.comlaura.mckevitt@bakertilly.com

Ha! Thanks so much Simon, and the tip on the RecordType Name was very helpful. Thanks again!