I have to make REST API call from salesforce to a HTTP URL GET method to get the JSON response parse it and display it on the vf page.
My Questions:
- From the class below how can I check the status code of the response?
- How to map the returned response to the Parser class which I generated from JSON2APEX using the sample JSON?
- How to do the mappings on the returned JSON to the account and corporation objects in salesforce?
- Can I display the info on vf page without saving or mapping to the corresponding objects in salesforce?
Integration Class:
public class MySQLIntegrationHelper{
public String getResult{get;set;}
//public String Response {get; set;}
public PageReference submit() {
getResult=getData();
return null;
}
public String getData()
{
HttpRequest req= new HttpRequest();
Http http = new Http();
req.setMethod('GET');
String url = 'http://mysql.com/buyout/account/1234';
req.setEndpoint(url);
String username = 'test';
String password = 'testpw';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ';
System.debug( 'authorizationHeader : ' + authorizationHeader );
req.setHeader('Authorization', authorizationHeader);
}
try {
HttpResponse res = new Http.send(req);
System.debug(res.toString());
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
} catch (System.CalloutException e) {
System.debug('The callout failed with this message:'+e);
}
return null;
}
JSON Parser:
public class MySQLJSONParser {
public class Corporations {
public Integer id;
public String listingid;
public String corporation_id;
public String location_id;
public String title;
}
public Account Account;
public List<Corporations> Corporations;
public class Account {
public Integer id;
public String InternalAccountId;
public String account_id;
public String name;
public String status;
public String bid_type;
}
public static MySQLJSONParser parse(String json) {
return (MySQLJSONParser) System.JSON.deserialize(json, MySQLJSONParser.class);
}
Best Answer
Here is an outline for 1/2/3 assuming you want to insert the data. If it's an update you'll need to query the existing versions of the objects:
On 4, yes you can present objects returned by the parse directly in your page if you want to: Visualforce can present Apex object data but you have to do more work such as outputting your own labels.