I am having a really hard time bulkifying my code, and need to do so becasue I am reaching the SOQL limit on execution.
Here is my code as of now.
public class Zenefits_Time_Off implements Schedulable, Database.AllowsCallouts {
public void execute(SchedulableContext SC) {
Zenefits_Time_Off.makeCallout();
}
@future(callout = true)
public static void makeCallout() {
List<Time_Off_Request__c> torToUpdate = new List<Time_Off_Request__c> ();
string url = 'https://api.zenefits.com/time_off/vacation_requests';
system.debug('before while');
while (url != null) {
url = makeHttpCall(url, torToUpdate);
system.debug('after while');
}
insert torToUpdate;
system.debug('SECONDTOR ' + torToUpdate);
}
public static String makeHttpCall(String url, List<Time_Off_Request__c> torToUpdate) {
string nextUrl = '';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'Bearer xxxxxx');
request.setEndpoint(url);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of posts.
Map<String, Object> wrapper = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
if (wrapper.containsKey('data')) {
Map<String, Object> wrapper2 = (Map<String, Object>) wrapper.get('data');
if (wrapper2.containsKey('data')) {
nextUrl = (String) wrapper2.get('next_url');
system.debug('next URL' + nextUrl);
List<Object> vacationRequests = (List<Object>) wrapper2.get('data');
System.debug('Received the following vacation requests:');
for (Object vacationRequestWrapper : vacationRequests) {
Map<String, Object> vacationRequest = (Map<String, Object>) vacationRequestWrapper;
if (vacationRequest.get('status').equals('approved')) {
Map<String, Object> wrapper3 = (Map<String, Object>) vacationRequest.get('creator');
string ptoUrl = (String) wrapper3.get('url');
string employeeId = ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0);
String Start_Date = (String) vacationRequest.get('start_date');
String End_Date = (String) vacationRequest.get('end_date');
string timeoff_ID = (String) vacationRequest.get('id');
string hours = (String) vacationRequest.get('hours');
List<Contact> contactList = [SELECT Id, FirstName, LastName, Zenefits_ID__c FROM Contact WHERE Zenefits_ID__c = :employeeId LIMIT 200];
for (Contact con : contactList) {
System.debug('contactList ' + contactList);
Time_Off_Request__c tor = new Time_Off_Request__c();
tor.Employee__c = con.Id;
tor.Project__c = 'a8q8A000000CafTQAS';
tor.First_Day_Off__c = date.valueOf(Start_Date);
tor.Last_Day_Off__c = date.valueOf(End_Date);
tor.Status__c = 'Saved';
torToUpdate.add(TOR);
system.debug('TOR' + tor);
system.debug('vacation ' + vacationRequest);
system.debug('creator ' + wrapper3);
System.debug('url ' + ptoUrl);
System.debug('ptoID ' + employeeId);
System.debug('start date ' + Start_Date);
System.debug('end date ' + End_Date);
System.debug('timeoffID ' + timeoff_ID);
System.debug('hours ' + hours);
System.debug('TORtoUpdate ' + torToUpdate);
}
}
}
}
}
}
return nextUrl;
}
}
The error is coming from line 48 as that is producing too many SOQL Queries. How do I fix this error?
vacationRequests
and in that loop you're perform a query each iteration of that loopif
statements instead of nestedif
statements. You could also bake this validation into a method in your deserialization class. 3) Instead ofwhile(url != null)
, considerwhile(!String.isBlank(url))
.isBlank()
handles null and empty strings.