I am really struggling with getting some values I need outside a for
loop.
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 = 'xxxxx';
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 xxxxx');
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:');
Set<String> empIds = new Set<String>();
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');
empIds.add(employeeId);
}
}
List<Contact> contactList = [SELECT Id, FirstName, LastName, Zenefits_ID__c FROM Contact WHERE Zenefits_ID__c IN: empIds 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.newInstance(Start_Date);
tor.Last_Day_Off__c = date.newInstance(End_Date);
tor.Status__c = 'Saved';
tor.Zenefits_ID__c = timeoff_ID;
torToUpdate.add(TOR);
}
}
}
}
return nextUrl;
}
}
I need to get the values of Start_Date
, End_Date
, and timeoff_ID
, so that I can use them in my record insert, but as you can see in the code those values are stuck up in the FOR loop.
How would I be able to get them outside the FOR loop so that they are able to be used in my value assignments such as:
tor.Last_Day_Off__c = date.newInstance(End_Date);
employeeId
? You may just like to use aMap<Id, String>
here and be able to utilize that in your next loop to fetch the corresponding value.employeeId
same as the Contact'sId
that you are iterating in your next loop?