JSON End Point URL Data Insert To Object(Contact) in Salesforce

By | February 3, 2021

End Point URL: https://api.androidhive.info/contacts/

First add above URL in Remote Site Settings.

Apex Class

public class JsonDataInsert {
    public static HttpResponse jsonData() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.androidhive.info/contacts/');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            EmployeeRecordsList employeeRecords = (EmployeeRecordsList) JSON.deserialize(response.getBody(),EmployeeRecordsList.class);
            //JSON.deserializeUntyped(jsonString)
            List<Employee__c> empobj = new List<Employee__c>();
            for (DeclareEmployee c: employeeRecords.contacts) {
                Employee__c e = new Employee__c ();
                e.id__c = c.id;
                e.Name = c.name;
                e.Email__c = c.email;
                e.Gender__c = c.gender;
                e.Address__c = c.address;
                e.Phone__c = c.phone.mobile;
                e.Home_Phone__c = c.phone.home;
                e.Office_Phone__c = c.phone.office;
                empobj.add(e);
            }
            insert empobj;
        }
        return response;
    }
    public class EmployeeRecordsList {
        List<DeclareEmployee> contacts;
    }
    public class DeclareEmployee {
        String id;
        String name;
        String email;
        String address;
        String gender;
        DeclarePhone phone;
    }
    public class DeclarePhone {
        String mobile;
        String office;
        String home;
    }
}

Test Class

@isTest
public class JsonDataInsertTest {
    @isTest static void testingTheCallout() {
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGeneratorClass ());
         JsonDataInsert.jsonData ();
        List<Employee__c> empobj = [select id,Name from Employee__c];
        system.assertEquals(true, !empobj.isEmpty());
        Test.stopTest();
    }
}

Mock Test Class

@isTest
global class MockHttpResponseGeneratorClass implements HttpCalloutMock{
    global HTTPResponse respond(HTTPRequest req) {
        string body = '{"contacts":[{"id":"c200000","name":"Ravi Tamada","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c201","name":"Johnny Depp","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c202","name":"Leonardo Dicaprio","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c203","name":"John Wayne","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c204","name":"Angelina Jolie","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"female","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c205","name":"Dido","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"female","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c206","name":"Adele","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"female","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c207","name":"Hugh Jackman","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c208","name":"Will Smith","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c209","name":"Clint Eastwood","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c2010","name":"Barack Obama","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c2011","name":"Kate Winslet","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"female","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}},{"id":"c2012","name":"Eminem","email":"[email protected]","address":"xx-xx-xxxx,x - street, x - country","gender":"male","phone":{"mobile":"+91 0000000000","home":"00 000000","office":"00 000000"}}]}';
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(body);
        res.setStatusCode(200);
        return res;
    }
}

Open Anonymous Window Execute Apex Class Method JsonDataInsert.jsonData();

Then insert Records in Object.

Leave a Reply

Your email address will not be published. Required fields are marked *