+ Start a Discussion
huskerwendyhuskerwendy 

How to call my api rest class

I'm not really a developer but have used apex in the past to write triggers. Now I'm trying to use the Rest API to make a call to GoToWebinar's API. I've written some code but am not sure how to test to see if my API call is even working. How do a test that from the Developer Console? Also, when I implement the feature, I am hoping to have it execute without human interaction as a job that runs each night. How do you do that?

Here's my code:
public class g2wCallout {
    public void getWebinars(){
      HttpRequest req = new HttpRequest(); 
      //Set HTTPRequest Method
      req.setMethod('GET');
    
      //Set HTTPRequest header properties
      req.setHeader('content-type', 'application/json');
      req.setHeader('Accept','application/json');
      req.setHeader('Authorization','OAuth ');
     
      req.setEndpoint( 'https://api.citrixonline.com/G2W/rest/organizers/3131084/historicalWebinars?fromTime=2015-05-28T18:00:00Z&toTime=2015-05-28T24:00:00Z' );
    
      //Set the HTTPRequest body	
       req.setBody('{"access_token":"blahblahblahblahblah"}'); 	
    
      Http http = new Http();
      
       try {
     
            //Execute web service call here		
           // HTTPResponse res = http.send(req);	
    
            //Helpful debug messages
             System.debug('this is a test');
            /*System.debug(res.toString());
          
            System.debug('STATUS:'+res.getStatus());
            System.debug('STATUS_CODE:'+res.getStatusCode());
            */
        } catch(System.CalloutException e) {
        //Exception handling goes here....
        }	
	}
}

 
Amit Chaudhary 8Amit Chaudhary 8
Create a scheduler class and schedule the same in night. And call your code like below in scheduler class


g2wCallout obj = new g2wCallout ();
obj.getWebinars();
 
huskerwendyhuskerwendy
Hi Amit,

Thanks for the help. I created the scheduler class and then used the "Schedule Apex" button in the UI to schedule it to run. However, is there a way to test it "On Demand" so that I don't have to wait for the scheduled job to execute? 
global class g2wCalloutScheduler implements Schedulable {
    global void execute(SchedulableContext sc) {
 		g2wCallout obj = new g2wCallout ();
   	// obj.getWebinars();
    }   
}

 
huskerwendyhuskerwendy
I'm just trying to make sure that my API call is actually working. Thanks!
 
Amit Chaudhary 8Amit Chaudhary 8
Try below code to execute your scheduler class
g2wCalloutScheduler m = new g2wCalloutScheduler();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Please Mark this as solution if this will help you

Thanks
Amit Chaudhary
huskerwendyhuskerwendy
This is making me crazy. I don't really know what I'm doing. When I add that code to my g2wCalloutScheduler class, I get this error:
System.LimitException: Maximum stack depth reached: 1001

I commented that out and created a test class so that I could run it from the Developer Console to test my API call and now I get this error:
Methods defined as TestMethod do not support Web service callouts, test skipped

How in the world do I test an API callout to see if it's working and what data it's returning?








 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post that will help you.

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xsuIAA
https://success.salesforce.com/issues_view?id=a1p30000000RZLxAAO
http://salesforce.stackexchange.com/questions/17254/maximum-stack-depth-reached-1001

I hope this will help you !!!  Please let me know if you need any help.

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com