0

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);
  • Are the dates in consideration here different for different employeeId? You may just like to use a Map<Id, String> here and be able to utilize that in your next loop to fetch the corresponding value. – Jayant Das Oct 21 at 20:56
  • The dates are associated with the employeeId, so how would I do that ? @JayantDas – Kris Webster Oct 21 at 21:35
  • Is employeeId same as the Contact's Id that you are iterating in your next loop? – Jayant Das Oct 21 at 21:36
  • it is not. EmployeeId is the Id associated with the system that we are pulling this information from, and we have each Salesforce users unique Id from that system in a field on their contact records (Zenefits_ID__C), so we are finding all the records where the EmployeeID matches with the Zenefits_ID__c and then inserting new records on the contacts in Salesforce based off of that. @JayantDas – Kris Webster Oct 21 at 21:42
0

Based on your comments, employeeId is basically your Zenefits_ID__c on your Contact record. So you will need to create a Map of the value associated with the Zenefits_ID__c and be able to utilize that later in your code.

A very basic implementation of this will look like as below (you should fine tune this, this is an approach to help you get started in that direction). Declare a Map<String, String> outside of the loops, populate it in your first loop and then use it in your second loop.

Map<String, String> mapVals = new Map<String, String>();

Then populate this in first loop as:

mapVals.put(employeeId, Start_Date);

And finally use the value of Start_Date in your second loop as:

tor.First_Day_Off__c = 
    date.newInstance(
        mapVals.get(con.Zenefits_ID__c));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.