User Story
As a CRM system user, I want to be able to create lead records using provided customer information, so that I can efficiently track and manage potential business opportunities.
JSON
[
{
"firstName": "Michael",
"lastName": "Johnson",
"age": 49,
"email": "michaeljohnson@example.com"
},
{
"firstName": "David",
"lastName": "Lee",
"age": 43,
"email": "davidlee@example.com"
},
{
"firstName": "Sophia",
"lastName": "Johnson",
"age": 35,
"email": "sophiajohnson@example.com"
},
{
"firstName": "Olivia",
"lastName": "Brown",
"age": 46,
"email": "oliviabrown@example.com"
},
{
"firstName": "Emma",
"lastName": "Johnson",
"age": 50,
"email": "emmajohnson@example.com"
},
{
"firstName": "Emily",
"lastName": "Taylor",
"age": 20,
"email": "emilytaylor@example.com"
},
{
"firstName": "David",
"lastName": "Lee",
"age": 34,
"email": "davidlee@example.com"
},
{
"firstName": "David",
"lastName": "Clark",
"age": 26,
"email": "davidclark@example.com"
},
{
"firstName": "James",
"lastName": "Taylor",
"age": 40,
"email": "jamestaylor@example.com"
},
{
"firstName": "Sophia",
"lastName": "Lee",
"age": 38,
"email": "sophialee@example.com"
}
]
Codes
public class PTSContact {
public Integer age;
public String lastName;
public String email;
public String firstName;
}
public class PTSContactAPI {
public static void fetchAndCreateContacts() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.pathtosalesforce.com/JSON/users.php');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
List<PTSContact> contactList = (List<PTSContact>) JSON.deserialize(response.getBody(), List<PTSContact>.class);
List<Contact> sfContacts = new List<Contact>();
for (PTSContact ptsContact : contactList) {
sfContacts.add(new Contact(
FirstName = ptsContact.firstName,
LastName = ptsContact.lastName,
Email = ptsContact.email,
Custom_Age__c = ptsContact.age (Create a new field)
));
}
if (!sfContacts.isEmpty()) {
insert sfContacts;
}
} else {
System.debug('Callout failed with status: ' + response.getStatusCode());
}
}
}