+ Start a Discussion
Jerry ClifftJerry Clifft 

How do I take this HTTPResponse data and write to a Salesforce Object?

I am new to API Integration and I have a question. I did the trailhead for API Integration, but I did not learn the final part of what I needed.

The following code works just fine the developer console.
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}
I can use the debug screen to see all the "animals" that returned.
Debug Screen

But what I really want to do is write (Create records, one per debug line) of this data to a new Salesforce Object call Animals in a field name TypeOfAnimal. Can someone please provide an example of how to do this please.

Thanks
Jerry
Best Answer chosen by Jerry Clifft
SandhyaSandhya (Salesforce Developers) 
Hi,

Please modify your code as below.
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
        Animal__c ani = new Animal__c();
         ani.name=String.valueOf(animal);
         ani.TypeOfAnimal__c=String.valueOf(animal);
        insert ani;
        System.debug('ani'+ani);

        
    }
}

Hope this helps you!

If this helps you, please mark it as solved.

Thanks and Regards
Sandhya

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Please modify your code as below.
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
        Animal__c ani = new Animal__c();
         ani.name=String.valueOf(animal);
         ani.TypeOfAnimal__c=String.valueOf(animal);
        insert ani;
        System.debug('ani'+ani);

        
    }
}

Hope this helps you!

If this helps you, please mark it as solved.

Thanks and Regards
Sandhya
This was selected as the best answer
Jerry ClifftJerry Clifft
Sandhya, this works perfectly, thank you!