0

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:

  1. From the class below how can I check the status code of the response?
  2. How to map the returned response to the Parser class which I generated from JSON2APEX using the sample JSON?
  3. How to do the mappings on the returned JSON to the account and corporation objects in salesforce?
  4. 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);
} 
0

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:

HTTPResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
    Corporation__c corps = new Corporation__c[] {};
    MySQLJSONParser p = MySQLJSONParser.parse(res.getBody());
    for (MySQLJSONParser.Corporation c : p.Corporations) {
        corps.add(new Corporation__c(
                Title__c = c.title,
                ...
                ));
    }
    insert corps;
} else {
    throw new MyException('HTTP callout failed:'
            + '\nendPoint=' + req.getEndPoint()
            + '\nstatusCode=' + res.getStatusCode()
            + '\nstatus=' + res.getStatus()
            + '\nbody=' + res.getBody()
            );
}

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.

  • Thanks Keith. Actually this records exists in salesforce but want to pull real time data from sql to make sure there are no changes. Since it is update how can I query the existing records(versions) also in the JSON response I get single Account and multiple Corporation records(so I need to map both the Account and Corporation records) which are of master detail in salesforce. – Salesforce Learner Jul 2 '15 at 20:10
  • @HarishGudipudi To query the existing versions (the simplest and safest approach is ) you need to store in Salesforce the primary key used in the SQL database; then you can query for matching SObjects using a where on that field and then compare the new and old values and only update if there are changes, Handle the one Account similarly to the many corporations; for the insert case, insert the Account first and then take its ID after the insert and set that on the corporations then insert those. – Keith C Jul 2 '15 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.