-
Notifications
You must be signed in to change notification settings - Fork 1
Implemented bulkified Queueable Apex version for after update #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,31 +111,30 @@ public with sharing class DummyJSONCallout { | |
| * | ||
| * @param contactId The Salesforce Contact ID used to generate the JSON payload for the external system. | ||
| */ | ||
| @future(callout = true) | ||
| public static void postCreateDummyJSONUser(String contactId) { | ||
| // Create HTTP request to send. | ||
| HttpRequest request = new HttpRequest(); | ||
| // Set the endpoint URL. Use direct URL or for best practices use Named Credential. | ||
| request.setEndpoint('callout:DummyJsonUser/add'); | ||
| // Set the HTTP method to POST. | ||
| request.setMethod('POST'); | ||
| // Set the body using generateDummyJsonUserPayload method. | ||
| // String jsonBody = generateDummyJsonUserPayload(); | ||
| // if (jsonBody.length() > 0) | ||
| request.setBody(generateDummyJsonUserPayload(contactId)); | ||
|
|
||
| // Send the HTTP request and get the response. | ||
| Http http = new Http(); | ||
| HttpResponse response = http.send(request); | ||
| // If the HTTP response code is successful, update the contact. | ||
| if (response.getStatusCode() >= 200 || response.getStatusCode() <= 299) { | ||
| Contact cont = [ | ||
| SELECT Id, DummyJSON_Last_Updated__c | ||
| FROM Contact | ||
| WHERE Id = :contactId]; | ||
| cont.DummyJSON_Last_Updated__c = Datetime.now(); | ||
| update cont; | ||
|
|
||
| public static List<Id> postCreateDummyJSONUser(Set<Id> contactIds) { | ||
| Map<Id, String> jsonById = generateDummyJsonUserPayload(contactIds); | ||
|
||
|
|
||
| List<Id> contactIsToUpdate = new List<Id>(); | ||
| for (Id contId : contactIds) { | ||
| // Create HTTP request to send. | ||
| HttpRequest request = new HttpRequest(); | ||
| // Set the endpoint URL. Use direct URL or for best practices use Named Credential. | ||
| request.setEndpoint('callout:DummyJsonUser/add'); | ||
| // Set the HTTP method to POST. | ||
| request.setMethod('POST'); | ||
| // Set the body using generateDummyJsonUserPayload method. | ||
| request.setBody(jsonById.get(contId)); | ||
|
|
||
| // Send the HTTP request and get the response. | ||
| Http http = new Http(); | ||
| HttpResponse response = http.send(request); | ||
| // If the HTTP response code is successful, update the contact. | ||
| if (response.getStatusCode() >= 200 || response.getStatusCode() <= 299) { | ||
| contactIsToUpdate.add(contId); | ||
| } | ||
| } | ||
| return contactIsToUpdate; | ||
| } | ||
|
|
||
| /* | ||
|
|
@@ -154,25 +153,30 @@ public with sharing class DummyJSONCallout { | |
| * @return String The JSON string payload that represents the Contact's details. | ||
| */ | ||
| @TestVisible // Allows test class to see this method. Since it is private, it would not be visible otherwise. | ||
| private static String generateDummyJsonUserPayload(String contactId) { | ||
| private static Map<Id, String> generateDummyJsonUserPayload(Set<Id> contactIds) { | ||
| // Query the contact to get the field values to generate the JSON payload. | ||
| Contact cont = [ | ||
| List<Contact> contacts = [ | ||
| SELECT Id, FirstName, LastName, Email, Phone, DummyJSON_Id__c | ||
| FROM Contact | ||
| WHERE Id = :contactId | ||
| WHERE Id IN :contactIds | ||
| ]; | ||
| // Create a map of the field values. | ||
| Map<String, Object> valuesMap = new Map<String, Object>(); | ||
| valuesMap.put('salesforceId', contactId); | ||
| valuesMap.put('firstName', String.isNotBlank(cont.FirstName) ? cont.FirstName : 'unknown'); | ||
| valuesMap.put('lastName', String.isNotBlank(cont.LastName) ? cont.LastName : 'unknown'); | ||
| valuesMap.put('email', String.isNotBlank(cont.Email) ? cont.Email : 'unknown'); | ||
| valuesMap.put('phone', String.isNotBlank(cont.Phone) ? cont.Phone : 'unknown'); | ||
| // Serialize the map into a JSON string. | ||
| String json = JSON.serialize(valuesMap); | ||
| Map<Id, String> jsonById = new Map<Id, String>(); | ||
| for (Contact cont : contacts) { | ||
| // Create a map of the field values. | ||
| Map<String, Object> valuesMap = new Map<String, Object>(); | ||
| valuesMap.put('salesforceId', String.valueOf(cont.Id)); | ||
| valuesMap.put('firstName', String.isNotBlank(cont.FirstName) ? cont.FirstName : 'unknown'); | ||
| valuesMap.put('lastName', String.isNotBlank(cont.LastName) ? cont.LastName : 'unknown'); | ||
| valuesMap.put('email', String.isNotBlank(cont.Email) ? cont.Email : 'unknown'); | ||
| valuesMap.put('phone', String.isNotBlank(cont.Phone) ? cont.Phone : 'unknown'); | ||
| // Serialize the map into a JSON string. | ||
| String json = JSON.serialize(valuesMap); | ||
| jsonById.put(cont.Id, json); | ||
| } | ||
|
|
||
| // Make sure to check that required contacts fields have a value. Default the value to unknown if it does not exists. | ||
| // Integration data can change over time. It is a best practice to add safeguards/validation to ensure the integration does not break. | ||
|
|
||
| return json; | ||
| return jsonById; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class PostDummyJSONUserQueueable implements Queueable, Database.AllowsCallouts { | ||
|
|
||
| private Map<Id, Contact> contsById; | ||
|
|
||
| public PostDummyJSONUserQueueable(Map<Id, Contact> contactsById) { | ||
| this.contsById = contactsById; | ||
| } | ||
|
|
||
| public void execute(QueueableContext qc) { | ||
| List<Id> contactIdsForUpdate = DummyJSONCallout.postCreateDummyJSONUser(this.contsById.keySet()); | ||
|
|
||
| List<Contact> contacts = [ | ||
| SELECT Id, DummyJSON_Last_Updated__c | ||
| FROM Contact | ||
| WHERE Id IN :contactIdsForUpdate | ||
| ]; | ||
| for (Contact cont : contacts) { | ||
| cont.DummyJSON_Last_Updated__c = Datetime.now(); | ||
| } | ||
|
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this inside the postCreatedummyJSON. |
||
| Database.update(contacts); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>59.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexClass> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍