function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kris WebsterKris Webster 

Scheduling a API Callout HELP

Good afternoon all ! I am attempting to schedule an API callout using the Database.Batchable method. 

I have all the code written and have attempted to schedule the class via the Salesforce Scheduler interface, however when the code was excecuted nothing happened to the records the code is supposed to update. ** When I run the class via ANONYMOUS APEX it works perfectly though... is there a better way to schedule this class ? 

Here is the API Callout class 
 
global class ZenefitsEmployees implements Database.Batchable<sObject> {
    
    public String query = 'Select ID, Name from Contact';

    global Database.QueryLocator start(Database.BatchableContext ctx) {
        return Database.getQueryLocator(query);
    }
    
    global void execute (Database.BatchableContext ctx, List<Contact> records) {

	string url = 'REMOVED FOR SECURTY PURPOSES';
	Integer int1 = 0;

    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer REMOVED FOR SECURITY PURPOSES');
    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')) {
                List<Object> people = (List<Object>)wrapper2.get('data');
                for (Object peopleWrapper : people) {
                    Map<String,Object> Employees = (Map<String,Object>) peopleWrapper;
                    if(Employees.containsKey('last_name')){
                    String ZenefitsLastName = (String) Employees.get('last_name');
                    String ZenefitsFirstName = (String) Employees.get('first_name');
                    String ZenefitseEmployeeId = (String) Employees.get('id');
                    String ZenefitsEmail = (String) Employees.get('work_email');    
                        
                    List<Contact> contactList = [SELECT Id, FirstName, LastName, Zenefits_ID__c, Email FROM Contact WHERE Email = :ZenefitsEmail LIMIT 200];
                            List<Contact> contactsToUpdate = new List<Contact>();
                        	for(Contact con : contactList){
                       			con.Zenefits_Id__c = ZenefitseEmployeeId;
                            	contactsToUpdate.add(con);
                        
                        		}
                        		update contactsToUpdate;
                            

                            	system.debug('Employees ' + Employees);
                            	system.debug('last name ' + ZenefitsLastName);
                        		system.debug('Employee id ' + ZenefitseEmployeeId);
                        		system.debug('first name ' + ZenefitsFirstName);
                            	system.debug('Contact list ' + contactList);
                            	system.debug('TEST ' + contactsToUpdate);
                   				}
               				}
            			}
           			return ;
        		}
        	return ;
    	}
    	return ;
	}
    
    global void finish(Database.BatchableContext ctx)
    {
   
      AsyncApexJob a = [SELECT Id, Status, ExtendedStatus, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob WHERE Id =
                          :ctx.getJobId()];
   // Send an email to the Apex job's submitter notifying of job completion. 
    
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {a.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + a.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + a.TotalJobItems +
   ' batches with '+ a.NumberOfErrors + ' failures. The error is related to ' + a.ExtendedStatus);
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
    }    
}
And then I have a second scheduleable class that calls this class and theoretically executes the code.. 
 
global class GetPeople implements Schedulable {
      global void execute(SchedulableContext ctx) {
           database.executeBatch(new ZenefitsEmployees(),200);
      }
}
ANY HELP WOULD BE GREAT!!