+ Start a Discussion
Kris WebsterKris Webster 

Schedule REST API Callout HELP

Good afternoon, 

I have a API call class that is working as I need it to, however I am not trying to get the class to execute every night at 2 AM. I am trying to implement a schedulable class to call the API class and make it execute every night, but am a little unsure how to do this.. 

Here is my REST API class
string url = *****;
Integer int1 = 0;

String myProcedure1() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setHeader('Authorization', 'Bearer ****);
    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;
                    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;
                        update contactsToUpdate;
					//we now need to find all contacts in SF that match the contacts in Employees
                    //once we have matches we need to synce each ID 
                            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 null;
        return null;
    return null;

void myProcedure2() {
    while (url != null) {
        System.debug('BEFORE URL: ' + url);
        url = myProcedure1();
        System.debug('AFTER URL: ' + url);

I have gone ahead and taken out the URL and the Bearer headers for privacy purposes, but all else is there. 

For anyone that has implemented a way to have an API call excecute every night on a daily basis please share your wisdome with me !! 

Thanks in advance !! 
Neha AggrawalNeha Aggrawal
Hi Kris,

You can just create a schedulable class:
global class dummyrestapi implements Schedulable{
   global void execute(System.SchedulableContext SC) {
      RestClassCalloutClass obj=new RestClassCalloutClass ();

The schedulable class can be scheduled by the following steps:
1. Setup->Develop->Apex Classes->Schedule Apex

User-added image

Hope this helps.
Thanks and Regards, 
Neha Aggrawal
www.initaura.com - Everything Salesforce (https://www.initaura.com)
Kris WebsterKris Webster
Do I need to change anything in line 4? Right now I am recieving the following error for line 4...

Method does not exist or incorrect signature: void CalloutFunc() from the type ZenefitsEmployees

Here is what the class looks like 
global class GetPeople implements Schedulable{
   global void execute(System.SchedulableContext SC) {
      ZenefitsEmployees obj = new ZenefitsEmployees ();

Neha AggrawalNeha Aggrawal
Hi Kris,

Yes replace CalloutFunc with the name of your method/function in the "ZenefitsEmployees" class.