Apex Trigger Whenever a Contact is updated (Email / Phone), Salesforce calls external REST API again using Queueable Apex | Salesforce sends Contact data using POST REST API using Queueable Apex
REAL-TIME BUSINESS SCENARIO (POST + QUEUEABLE)
๐ Scenario
- Salesforce has Contacts not yet synced with an External CRM
- Salesforce sends Contact data using POST REST API
- External system returns:
- externalCustomerId
- Salesforce:
- updates the Contact with External ID
- creates a new Log record (example: Integration Log)
- Whenever a Contact is updated (Email / Phone):
- Salesforce calls external REST API again using Queueable Apex
Salesforce Setup
Custom Fields of Contact
- External_Customer_Id__c (Text)
Custom Object (optional but real-world)
- Integration_Log__c:
- Request__c (Long Text)
- Response__c (Long Text)
- Status__c (Text)
Step 1 Account Trigger (After Update): ContactPracticeTrigger.apxt
TRIGGER ContactPracticeTrigger ON Contact (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
IF(TRIGGER.isAfter){
IF(TRIGGER.isUpdate){
List<Id> conIds = NEW List<Id>();
FOR(Contact con:TRIGGER.new){
Contact oldData = TRIGGER.oldMap.get(con.Id);
IF(con.Phone != oldData.Phone || con.Email != oldData.Email){
conIds.add(con.Id);
}}IF(!conIds.isEmpty()){
ContactPracticeQueueable que = NEW ContactPracticeQueueable(conIds);
System.enqueueJob(que);
//System.enqueueJob(NEW ContactPracticeQueueable(conIds));
}}}}
Step 2 Apex Controller: ContactPracticeQueueable.apxc
|
๐ Up to 99% Off Courses (Coupon)
๐ฅ Use Promo Code: STANDARDOFFER๐ฅ ๐ Get Free Salesforce Course Access: www.thevijaykumar.w3web.net |
public class ContactPracticeQueueable implements Queueable, DATABASE.AllowsCallouts{
public List<Id> contactIds;
public ContactPracticeQueueable(List<Id> ids){
this.contactIds = ids;
}public void EXECUTE(QueueableContext qc) {
List<Contact> conObjList = [SELECT Id, FirstName, LastName, Email, Phone, External_Customer_Id__c FROM Contact WHERE Id IN:contactIds];
List<Contact> conItem = NEW List<Contact>();
List<Integration_Log__c> logs = NEW List<Integration_Log__c>();
FOR(Contact con:conObjList){
Http h = NEW Http();
HttpRequest req = NEW HttpRequest();
req.setEndpoint('https://reqres.in/api/users'); // Public test API
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('x-api-key', 'reqres_4d079b1969e2489f803812be427d6bc3');
//req.setBody('{"fname":con.FirstName, "lname":con.LastName, "email":con.Email, "phone":con.Phone}');
Map<String, Object> bodyData = NEW Map<String, Object>();
bodyData.put('fname',con.FirstName);
bodyData.put('lname',con.LastName);
bodyData.put('email',con.Email);
bodyData.put('phone',con.Phone);
req.setBody(JSON.serialize(bodyData));
HttpResponse res = h.send(req);
system.debug('StatusCode ' + res.getStatusCode());
Integration_Log__c log = NEW Integration_Log__c();
log.Request__c = req.getBody();
log.Response__c = res.getBody();
log.Status__c = String.valueOf(res.getStatusCode());
logs.add(log);
system.debug('logs##### ' + logs);
IF(res.getStatusCode() == 200 || res.getStatusCode() == 201){
Map<String, Object> resData = (Map<String, Object>) json.deserializeUntyped(res.getBody());
system.debug('resData ' + resData);
string resId = (String) resData.get('id');
con.External_Customer_Id__c = resId;
conItem.add(con);
}}IF(!conItem.isEmpty()){
UPDATE conItem;}IF (!logs.isEmpty()) {
INSERT logs;}}}
Further post that would you like to learn in Salesforce
Can we use trigger new in after update?
trigger. new is available only on insert, update, and undelete triggers and represents the new version of the records after the trigger event.
How do you call an external API from Salesforce using Apex?
To call an API from Apex, use a named credential, which specifies the URL of a callout endpoint and its required authentication parameters. By security policy, sessions created by Lightning components aren't enabled for API access. This restriction prevents even your Apex code from making API calls to Salesforce.
What are the limitations of Queueable apex?
Limitations of Queueable Apex: Job Depth Limit: Queueable Apex allows a maximum of 5 chained jobs in a single transaction. Exceeding this limit results in a runtime error. Future Methods do not have this limitation, as they cannot be chained.
Related Topics | You May Also Like
|
๐ Get Free Course โ
๐ Salesforce Administrators ๐ Salesforce Lightning Flow Builder ๐ Salesforce Record Trigger Flow Builder |
๐ Get Free Course โ |

