User Story
As a Salesforce Administrator,
I want to automate the process of creating Accounts, Contacts, and Cases in Salesforce by making a callout to an external API,
so that data from our external sources can be seamlessly integrated into our Salesforce instance for efficient tracking and management.
Acceptance Criteria:
-
Data Callout:
- The system should make a callout to an external API endpoint to retrieve JSON data.
- The API response should contain details for multiple accounts, including nested contacts, cases, and tasks.
-
Account Creation:
- Each
account
in the JSON should be created as an Account record in Salesforce.
- The
accountName
should map to the Name
field.
- The
industry
should map to the Industry
field.
billingAddress
and shippingAddress
details should map to the standard Billing
and Shipping
fields.
-
Contact Creation:
- For each account, the nested
contacts
array should be used to create associated Contact records.
- Each contact should map
firstName
, lastName
, email
, and phone
to the corresponding Contact fields.
- The
Contact
records should be linked to the parent Account
created from the JSON data.
-
Case Creation:
- For each account, the nested
cases
array should be used to create associated Case records.
- Each case should map
subject
, status
, and priority
to the corresponding Case fields.
- The
Case
records should be linked to the parent Account
.
-
Task Creation:
- For each account, the
tasks
array should be processed to create associated Task records if needed.
- Each task should map
subject
, status
, and dueDate
to the corresponding Task fields.
- Tasks should be associated with the
Account
or Contact
as needed.
JSON
{
"accounts": [
{
"accountName": "Tech Solutions Inc.",
"industry": "Technology",
"billingAddress": {
"street": "123 Main St",
"city": "San Francisco",
"state": "CA",
"postalCode": "94105",
"country": "USA"
},
"shippingAddress": {
"street": "789 Market St",
"city": "San Francisco",
"state": "CA",
"postalCode": "94103",
"country": "USA"
},
"contacts": [
{
"firstName": "John",
"lastName": "Doe",
"email": "jdoe@techsolutions.com",
"phone": "+1 415-123-4567"
},
{
"firstName": "Jane",
"lastName": "Smith",
"email": "jsmith@techsolutions.com",
"phone": "+1 415-234-5678"
},
{
"firstName": "Alice",
"lastName": "Johnson",
"email": "ajohnson@techsolutions.com",
"phone": "+1 415-345-6789"
}
],
"cases": [
{
"subject": "Technical Issue with Software",
"status": "In Progress",
"priority": "High"
},
{
"subject": "Billing Inquiry",
"status": "Closed",
"priority": "Medium"
}
],
"tasks": [
{
"subject": "Follow up on new lead",
"status": "Not Started",
"dueDate": "2024-11-20"
},
{
"subject": "Prepare report for Q4 meeting",
"status": "In Progress",
"dueDate": "2024-12-01"
}
]
},
{
"accountName": "Green Energy Corp.",
"industry": "Energy",
"billingAddress": {
"street": "456 Greenway Blvd",
"city": "Seattle",
"state": "WA",
"postalCode": "98101",
"country": "USA"
},
"shippingAddress": {
"street": "789 Eco Drive",
"city": "Seattle",
"state": "WA",
"postalCode": "98102",
"country": "USA"
},
"contacts": [
{
"firstName": "Robert",
"lastName": "Brown",
"email": "rbrown@greenenergy.com",
"phone": "+1 206-123-4567"
},
{
"firstName": "Sara",
"lastName": "Wilson",
"email": "swilson@greenenergy.com",
"phone": "+1 206-234-5678"
}
],
"cases": [
{
"subject": "Inquiry about renewable energy options",
"status": "Open",
"priority": "Low"
}
],
"tasks": [
{
"subject": "Schedule call with potential partner",
"status": "Completed",
"dueDate": "2024-11-10"
}
]
}
]
}
Codes
public class PTSResponse {
public CustomAccounts[] accounts;
public class CustomAccounts {
public String accountName;
public String industry;
public BillingAddress billingAddress;
public ShippingAddress shippingAddress;
public CustomContacts[] contacts;
public CustomCases[] cases;
public CustomTasks[] tasks;
}
public class BillingAddress {
public String street;
public String city;
public String state;
public String postalCode;
public String country;
}
public class ShippingAddress {
public String street;
public String city;
public String state;
public String postalCode;
public String country;
}
public class CustomContacts {
public String firstName;
public String lastName;
public String email;
public String phone;
}
public class CustomCases {
public String subject;
public String status;
public String priority;
}
public class CustomTasks {
public String subject;
public String status;
public String dueDate;
}
public static PTSResponse parse(String json){
return (PTSResponse) System.JSON.deserialize(json, PTSResponse.class);
}
}
public class PTSAPI {
public static void GetAccountWithCases(){
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndPoint('https://api.pathtosalesforce.com/JSON/AccountsWithCases.json');
HttpResponse res = http.send(req);
List<Account> accListToBeInserted = new List<Account>();
List<Contact> contactListToBeInserted = new List<Contact>();
List<Case> caseListToBeInserted = new List<Case>();
List<Task> taskListToBeInserted = new List<Task>();
Map<String, List<Contact>> conMap = new Map<String, List<Contact>>();
Map<String, List<Case>>caseMap = new Map<String, List<Case>>();
Map<String, List<Task>> taskMap = new Map<String, List<Task>>();
List<Contact> TempContactList = new List<Contact>();
List<Case> TempCaseList = new List<Case>();
List<Task> TempTaskList = new List<Task>();
PTSResponse response = PTSResponse.parse(res.getBody());
//system.debug(response);
for(PtsResponse.CustomAccounts accData: response.accounts){
Account acc = new Account();
acc.Name = accData.accountName;
acc.Industry = accData.industry;
acc.BillingCity = accData.BillingAddress.city;
acc.BillingStreet = accData.BillingAddress.street;
acc.BillingPostalCode = accData.BillingAddress.postalCode;
acc.BillingState = accData.BillingAddress.state;
acc.BillingCountry = accData.BillingAddress.country;
acc.ShippingCity = accData.shippingAddress.city;
acc.ShippingCountry = accData.shippingAddress.country;
acc.ShippingStreet = accData.shippingAddress.street;
acc.ShippingState = accData.shippingAddress.state;
accListToBeInserted.add(acc);
for(PtsResponse.CustomContacts contactData: accData.contacts){
Contact con = new Contact();
con.FirstName = contactData.firstName;
con.LastName = contactData.lastName;
con.Phone = contactData.phone;
con.email = contactData.email;
TempContactList.add(con);
}
conMap.put(acc.Name, TempContactList.clone());
TempContactList.clear();
for(PtsResponse.CustomCases caseData: accData.cases){
Case c = new Case();
c.Subject = caseData.subject;
c.Priority = caseData.priority;
c.Status = caseData.status;
TempCaseList.add(c);
}
caseMap.put(acc.Name, TempCaseList.clone());
TempCaseList.clear();
for(PtsResponse.CustomTasks taskData: accData.tasks){
Task t = new Task();
t.Subject = taskData.subject;
t.ActivityDate = Date.valueOf(taskData.dueDate);
t.Status = taskData.status;
TempTaskList.add(t);
}
taskMap.put(acc.Name, TempTaskList.clone());
TempTaskList.clear();
}
insert accListToBeInserted;
List<Account> insertedAccList = [Select Id, Name From Account Where Id IN :accListToBeInserted];
for(Account acc:insertedAccList ){
//System.debug(acc.id);
for (Contact con: conMap.get(acc.Name)){
System.debug(conMap.get(acc.Name).size());
con.accountId = acc.id;
contactListToBeInserted.add(con);
}
for (Case newCase: caseMap.get(acc.Name)){
newCase.accountId = acc.id;
caseListToBeInserted.add(newCase);
}
for (Task newTask: taskMap.get(acc.Name)){
newTask.WhatId = acc.id;
taskListToBeInserted.add(newTask);
}
}
insert contactListToBeInserted;
insert caseListToBeInserted;
insert taskListToBeInserted;
}
}