0

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;
                }
            }
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – David Reed
    Commented Oct 23, 2019 at 16:13

3 Answers 3

0

Yes you could have a map that would store the values for each employee based on Id. So you could have something like this

map<String,map<String,Object>> empMap = new map<String,map<String,Object>>();

//imagine for(){} loop

map<String,Object> empValues = new map<String,Object>();

string ptoUrl = (String) wrapper3.get('url'););
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');

empValues.put('startDate',vacationRequest.get('start_date'));
//... and so on
string employeeId = ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0);
empMap.put(employeeId,empValues);


//then ContactList For(){} Loop
for (Contact con : contactList)
{
    System.debug('contactList ' + contactList); 

    map<String,Object> tempEmpValues = empMap.get(c.Zenefits_ID__c);

    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.parse(tempEmpValues.get('startDate'));
    //... and so on
    torToUpdate.add(TOR);

    system.debug('tor' + torToUpdate); 

}
1

The variables that you define in a loop, only live during that one iteration of your loop, so you can not reuse them anywhere else.
I would recommend you creating a Map<String, Contact>(if the Zenefits ID is unique) and then loop over vacationRequests again to fill in the correct data. For example:

Map<String, Contact> empIdToContactMap = new Map<String, Contact>();
for (Object vacationRequestWrapper : vacationRequests) {
    if (vacationRequest.get('status').equals('approved')) {
        empIds.add(ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0))
    }
}

List<Contact> contactList = [SELECT Id, Zenefits_ID__c FROM Contact]; // Your query here
for (Contact c : contactList) {
    empIdToContactMap.put(c.Zenefits_ID__c, c);
}

for (Object vacationRequestWrapper : vacationRequests) {
    String employeeId = ptoUrl.substring(ptoUrl.length() - 7, ptoUrl.length() - 0);
    Contact c = empIdToContactMap.get(employeeId);
    // Create you time off requests here
}
0
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(con.id);
                                         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); 

                                }
                            }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .