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
Brian Cherry FWIBrian Cherry FWI 

Rest Callout in Constructor data isn't showing

Hey Everyone -

I have a callout in my constructor and trying to avoid having to do a remote action and javascript processing. I would like to have the processing done server side and just display the results from the call. The problem is, the page loads before the call is complete so I'm left with a blank page. Dev console shows that the call out was a success and the object was populated. Here's the page and class. Any ideas on how to get this to work or am I stuck with remote action and js processing?
 
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class StatushubJSON {
	public static void consumeObject(JSONParser parser) {
		Integer depth = 0;
		do {
			JSONToken curr = parser.getCurrentToken();
			if (curr == JSONToken.START_OBJECT || 
				curr == JSONToken.START_ARRAY) {
				depth++;
			} else if (curr == JSONToken.END_OBJECT ||
				curr == JSONToken.END_ARRAY) {
				depth--;
			}
		} while (depth > 0 && parser.nextToken() != null);
	}

	public class Services {
		public Integer id {get;set;} 
		public String name {get;set;} 
		public String updated_at {get;set;} 
		public Boolean up {get;set;} 
		public Services(JSONParser parser) {
			while (parser.nextToken() != JSONToken.END_OBJECT) {
				if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
					String text = parser.getText();
					if (parser.nextToken() != JSONToken.VALUE_NULL) {
						if (text == 'id') {
							id = parser.getIntegerValue();
						} else if (text == 'name') {
							name = parser.getText();
						} else if (text == 'updated_at') {
							updated_at = parser.getText();
						} else if (text == 'up') {
							up = parser.getBooleanValue();
						} else {
							System.debug(LoggingLevel.WARN, 'Services consuming unrecognized property: '+text);
							consumeObject(parser);
						}
					}
				}
			}
		}
	}
	
	public Integer id {get;set;} 
	public String name {get;set;} 
	public String updated_at {get;set;} 
	public String logo {get;set;} 
	public Integer services_count {get;set;} 
	public Integer down_services_count {get;set;} 
	public String subdomain {get;set;} 
	public List<Services> services {get;set;} 

	public StatushubJSON(JSONParser parser) {
		while (parser.nextToken() != JSONToken.END_OBJECT) {
			if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
				String text = parser.getText();
				if (parser.nextToken() != JSONToken.VALUE_NULL) {
					if (text == 'id') {
						id = parser.getIntegerValue();
					} else if (text == 'name') {
						name = parser.getText();
					} else if (text == 'updated_at') {
						updated_at = parser.getText();
					} else if (text == 'logo') {
						logo = parser.getText();
					} else if (text == 'favicon') {
						
					} else if (text == 'services_count') {
						services_count = parser.getIntegerValue();
					} else if (text == 'down_services_count') {
						down_services_count = parser.getIntegerValue();
					} else if (text == 'subdomain') {
						subdomain = parser.getText();
					} else if (text == 'services') {
						services = new List<Services>();
						while (parser.nextToken() != JSONToken.END_ARRAY) {
							services.add(new Services(parser));
						}
					} else {
						System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
						consumeObject(parser);
					}
				}
			}
		}
	}
	
	
	public static StatushubJSON parse(String json) {
		return new StatushubJSON(System.JSON.createParser(json));
	}
}
public class StatusHub {
   public string result {get;set;}
   public List <StatusHubJSON > parse {get;set;}  
    
    public statusHub()
    {
        Http h=new  Http();
        HttpRequest req=new  HttpRequest();
        req.setMethod('GET');
        req.setHeader('api-key','xxxxxx');
        req.setEndPoint('https://app.statushub.io/api/status_pages/fwi-status');
        HttpResponse res = h.send(req);
        string json = res.getBody();
        StatusHubJSON parse=new StatusHubJSON(System.JSON.createParser(json));
     }
    
    

}


 
Brian Cherry FWIBrian Cherry FWI
I figured this out.  I was doing it backwards...