I am really struggling with this... I am trying to use variables from my first for loop in my second for loop and I can not figure out how to do it...
Basically I need the values of the Start_Date, End_Date, and Timeoff_ID so that I can use them to update specific field on the TOR object before I insert the records.. I have been staring at this code for hours and I think I am starting to go crazy. Any help would be greatly appreciated !
Here is the code
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 LWzuCcK1SGSKJG2r2GEr');
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:');
Map<String,map<String, Object>> empMap = new Map<String,map<String, Object>>();
Map<String, Object> ptoMap = new Map<String, Object>();
for (Object vacationRequestWrapper : vacationRequests) {
Map<String, Object> vacationRequest = (Map<String, Object>) vacationRequestWrapper;
if (vacationRequest.get('status').equals('approved')) {
system.debug('vacationRequests ' + vacationRequest);
Map<String, Object> wrapper3 = (Map<String, Object>) vacationRequest.get('creator');
Map<String,Object> empValues = new Map<String,Object>();
string ptoUrl = (String) wrapper3.get('url');
empValues.put('startDate',vacationRequest.get('start_date'));
empValues.put('endDate',vacationRequest.get('end_date'));
empValues.put('ptoId',vacationRequest.get('id'));
string ptoId = (string)vacationRequest.get('id');
string employeeId = ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0);
empMap.put(employeeId, empvalues);
ptoMap.put(ptoId, empvalues);
system.debug('ptoMap ' + ptoMap);
system.debug('empMap'+ empMap);
}
}
Map<String, Object> tempEmpValues = new Map<String, Object>();
Map<Id, Contact> contactMap = new Map<Id, Contact>();
List<Contact> contactList = [SELECT Id, FirstName, LastName, Zenefits_ID__c FROM Contact WHERE Zenefits_ID__c IN : empMap.keySet() LIMIT 200];
for (Contact con : contactList) {
System.debug('contactList ' + contactList);
tempEmpValues = empMap.get(con.Zenefits_ID__C);
contactMap.put(con.id, con);
system.debug('contactMap ' + contactMap);
system.debug('tempDates' + tempEmpValues.get('startDate'));
}
List<Time_Off_Request__c> torList = [SELECT ID, Zenefits_ID__c FROM Time_Off_Request__c WHERE Zenefits_ID__c NOT IN : ptoMap.keySet() LIMIT 200];
for(Time_Off_Request__c tor : torList) {
if(tor.Zenefits_Id__c != Null){
return Null;
} else {
tor.Employee__c = contactMap.get(contact.firstName);
tor.Project__c = 'a8q8A000000CafTQAS';
tor.First_Day_Off__c = date.valueof((string)tempEmpValues.get('startDate'));
tor.Last_Day_Off__c = date.valueof((string)tempEmpValues.get('endDate'));
tor.Status__c = 'Approved';
tor.Zenefits_ID__c = (string)tempEmpValues.get('ptoId');
torToUpdate.add(TOR);
system.debug('tor' + torToUpdate);
}
}
}
}
}
return nextUrl;
}
}