Wednesday, January 18, 2023

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

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":"ravi@gmail.com","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":"johnny_depp@gmail.com",
"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":"leonardo_dicaprio@gmail.com",
"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":"john_wayne@gmail.com",
"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":"angelina_jolie@gmail.com",
"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":"dido@gmail.com","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":"adele@gmail.com","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":"hugh_jackman@gmail.com",
"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":"will_smith@gmail.com","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":"clint_eastwood@gmail.com","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":"barack_obama@gmail.com",
"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":"kate_winslet@gmail.com",
"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":"eminem@gmail.com","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.


No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...