function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kenji775Kenji775 

Why/How does this work and how to test it

Hey All,

Following a guide online I found how to make a class that will query for data and get the results to JSON for me, it's very cool. But I do not understand how it works. It's basically laid out like this

 

 

public class getJsonEvents { public JsonObject json {get;set;} /** invoked on an Ajax request */ public void getEvents() { //Code to build json string } // Returns the JSON result string public String getResult() {

  string jsonString = json.ValuetoString();

return jsonString; } }

 The first method is void, and the 2nd returns the string. The visual force page that calls and uses it successfully looks like this.

 

 

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

 

 So it has the class set as it's controller, and it calls getEvents. Makes sense so far, except that it never calls getResult().

 

Nowhere is getResult invoked that I can find, yet the results get printed. More confusing is that this {!result} variable comes out of seemingly nowhere. I never created it, I never named it, but somehow it's there. I guess that is the default name of the return contents of the controller?

 

When uses this way does all code in a class just run sequentially until it hits a return statment or what? Also, how can I test this?I tried using code like this

 

//Create page reference PageReference opsCalendar = Page.OpsCalendar; //Pass start and end timestamps in url opsCalendar.getParameters().put('start','1262332800'); opsCalendar.getParameters().put('end','1263178646'); //initiate page reference Test.setCurrentPageReference(opsCalendar); //create instance of the class getJsonEvents eventsCtlr = new getJsonEvents(); //This fails because getEvents returns null; can't assign that to string myRunResult = eventsCtlr.getEvents(); //log the results System.Debug('get JSON Events result'+myRunResult );

 but it doesn't work, as i end up trying to assing the results of a null method/function to a string. What do I do?

 

Any help is appreciated. I know these are dumb questions, but at least I'm learning :P Thanks again.

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
rscottrscott

{!result} actually calls getResult(). the 'get' is implied in this case. i believe this was the way you used to have to write properties. it could actually be written like this and it might be clearer to you (not that it's the best way to write it...):

 

public string result {

get {

string jsonString = json.ValuetoString();

return jsonString; 

 

 

you should be able to test by calling getEvents() and then calling getResult() and checking the return value of that.

 

eventsCtlr.getEvents(); 

myRunResult = eventsCtlr.getResult(); 

All Answers

rscottrscott

{!result} actually calls getResult(). the 'get' is implied in this case. i believe this was the way you used to have to write properties. it could actually be written like this and it might be clearer to you (not that it's the best way to write it...):

 

public string result {

get {

string jsonString = json.ValuetoString();

return jsonString; 

 

 

you should be able to test by calling getEvents() and then calling getResult() and checking the return value of that.

 

eventsCtlr.getEvents(); 

myRunResult = eventsCtlr.getResult(); 

This was selected as the best answer
Kenji775Kenji775
Ah, thank you. I was starting to wonder if the word get was implied, but that seemed crazy to me. Thank you for clearing that up, and helping me figure out how to test it. I'll give a shot shortly and let ya know how it goes. Thanks!
Kenji775Kenji775
That seems to have done the trick. Thank you.