I had created wrapper class and the trigger and integration but its not updating
public class DataUpdateWrapper {
public List<ExternalEvent> events;
public class ExternalEvent {
public String eventType;
public String eventName;
}
}
public class DataUpdater {
@future(callout=true)
public static void callExternalAPI(Set<Id> updatedContractIds) {
List<ServiceContract> updatedContracts = [SELECT Id, Event_Type__c, Event_Name__c FROM ServiceContract WHERE Id IN :updatedContractIds];
// Make the callout and process the response
HttpRequest req = new HttpRequest();
req.setEndpoint('https://apigenerator.dronahq.com/api/qrq2khO_/data');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Response body: ' + res.getBody());
if (res.getStatusCode() == 200) {
try {
List<DataUpdateWrapper> wrapperList = (List<DataUpdateWrapper>) JSON.deserialize(
res.getBody(),
List<DataUpdateWrapper>.class
);
for (DataUpdateWrapper wrapper : wrapperList) {
for (DataUpdateWrapper.ExternalEvent event : wrapper.events) {
System.debug('Event Type from API: ' + event.eventType);
for (ServiceContract sc : updatedContracts) {
System.debug('Service Contract Event Type: ' + sc.Event_Type__c);
if (sc.Event_Type__c == event.eventType) {
sc.Event_Name__c = event.eventName;
}
}
}
}
update updatedContracts;
if (!updatedContracts.isEmpty()) {
update updatedContracts;
}
} catch (Exception e) {
System.debug('Exception: ' + e.getMessage());
}
} else {
System.debug('HTTP Request failed with status code: ' + res.getStatusCode());
System.debug('Response body: ' + res.getBody());
}
}
}
trigger ServiceContractUpdate on ServiceContract (after update) {
Set<Id> updatedContractIds = new Set<Id>();
for(ServiceContract sc : Trigger.new) {
if(sc.Event_type__c != Trigger.oldMap.get(sc.Id).Event_type__c){
updatedContractIds.add(sc.Id);
}
}
if(!updatedContractIds.isEmpty()) {
DataUpdater.callExternalAPI(updatedContractIds);
}
}
Here is the updated code.
Trigger
trigger ServiceContractUpdate on ServiceContract (after update) {
Set<Id> updatedContractIds = new Set<Id>();
for(ServiceContract sc : Trigger.new) {
if(sc.Event_type__c != Trigger.oldMap.get(sc.Id).Event_type__c || sc.Event_Name__c != Trigger.oldMap.get(sc.Id).Event_Name__c){
updatedContractIds.add(sc.Id);
}
}
if(!updatedContractIds.isEmpty()) {
DataUpdater.callExternalAPI(updatedContractIds);
}
}
Apexclass
public class DataUpdater {
@future(callout=true)
public static void callExternalAPI(Set<Id> updatedContractIds) {
List<ServiceContract> updatedContracts = [SELECT Id, Event_Type__c, Event_Name__c FROM ServiceContract WHERE Id IN :updatedContractIds];
HttpRequest req = new HttpRequest();
req.setEndpoint('https://apigenerator.dronahq.com/api/J7Dq5w-n/data');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Response body: ' + res.getBody());
if (res.getStatusCode() == 200) {
try {
// Deserialize the response into a list of DataUpdateWrapper objects
List<DataUpdateWrapper> wrapperList = (List<DataUpdateWrapper>) JSON.deserialize(
res.getBody(), List<DataUpdateWrapper>.class
);
for (DataUpdateWrapper wrapper : wrapperList) {
for (DataUpdateWrapper.ExternalEvent event : wrapper.events) {
System.debug('Event Type from API: ' + event.eventType);
for (ServiceContract sc : updatedContracts) {
System.debug('Service Contract Event Type: ' + sc.Event_Type__c);
if (sc.Event_Type__c == event.eventType) {
sc.Event_Name__c = event.eventName;
}
}
}
}
if (!updatedContracts.isEmpty()) {
update updatedContracts;
}
} catch (Exception e) {
System.debug('Exception: ' + e.getMessage());
}
} else {
System.debug('HTTP Request failed with status code: ' + res.getStatusCode());
System.debug('Response body: ' + res.getBody());
}
}
}
WrapperClass
public class DataUpdateWrapper {
public Integer id; // This corresponds to the "id" field in the JSON response
public List<ExternalEvent> events; // This corresponds to the "events" array in the JSON
public class ExternalEvent {
public String eventType; // This corresponds to "eventType" in the JSON
public String eventName; // This corresponds to "eventName" in the JSON
}
}
Also, if you find this content helpful please mark it as the best answer.