This is my code at the moment, the API is only set to receive the ID's one at a time (if this is a major thing I can ask for this to be changed) so that I can send them in a batch instead. Also any comments to improve my code would be appreciative I've only just started so I'm sure there will be improvements to be made.
public class sendToApi {
public static string token = 'test';
public static string sfid = '';
public static HttpResponse httpCallout() {
// Format SF ID to send to API
string data = '{' + '"token": "' + token + '", ' + '"sfid":' + '"' + sfid + '"' + '}';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.com');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody(data);
HttpResponse response = Http.send(request);
System.debug('Http Response: ' + response);
System.debug('Salesforce ID: ' + sfid);
return response;
}
public static void updateRecord(DVLA_Lookup_Opportunity__c dv) {
// dvlaOppToUpdate.Id = dv.Id;
system.debug('sfid:' + dv.id);
dv.SentToAPI__c = 'Sent';
update dv;
}
@future (callout=true)
public static void dvlaUpdates() {
// Create a list of account records from a SOQL query
List<DVLA_Lookup_Opportunity__c> dvla = [SELECT Id, SentToAPI__c, Account_Id__c FROM DVLA_Lookup_Opportunity__c WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Pending'];
// Loop through the list and update the sfid field
for(DVLA_Lookup_Opportunity__c dv : dvla) {
sfid = dv.id;
HttpResponse res = null;
res = httpCallout();
if (res.getStatusCode() == 200) {
updateRecord(dv);
}
else {
System.debug(res.getStatusCode());
}
}
}
}