1

I'm trying to use the HTTP Callout methods provided in Apex to 1. Get a token from Marketo 2. Use this token to create three leads in Marketo using the REST API

However, it doesn't seem to be working. I have never coded something like this before, so I expect that I probably made some very embarrassing and obvious mistakes, but I've been able to find very little documentation on this, so I'm starting from basically scratch and the REST API documentation. Anyways, here's the two classes I've been using to attempt to do this:

Class 1:

global class MakeCallouts {
      static String testCallout() {

        HttpResponse res = CalloutClass.getInfoFromExternalService();
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        System.assertEquals(200, res.getStatusCode());

    //getting the accesstoken  
    String getToken;
    String answer = res.getBody();
    System.debug(answer);

JSONParser parser = JSON.createParser(answer);
while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
        String fieldName = parser.getText();
        parser.nextToken();
        if(fieldName == 'access_token') {
           getToken = parser.getText();      
        } 
}
}
System.debug(getToken);


        HttpResponse res2 = CalloutClass.getInfoFromExternalService2(getToken);


        String contentType2 = res2.getHeader('Content-Type');
        System.assert(contentType2 == 'application/json');
        System.assertEquals(200, res2.getStatusCode());

        System.debug(res2.getBody());
        return res2.getBody();
    }
}

Class 2:

public class CalloutClass {
 public static HttpResponse getInfoFromExternalService () {
    // Instantiate a new http object
    Http h = new Http();

     // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://XXX-rlu-923.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id=XXXX&client_secret=XXXX');
    req.setMethod('GET');

    // Send the request, and return a response
    HttpResponse res = h.send(req);
    return res;

}

 public static HttpResponse getInfoFromExternalService2 (String getToken) {
    // Instantiate a new http object
    Http h2 = new Http();

     // Instantiate a new HTTP request, specify the method (POST) as well as the endpoint
    HttpRequest req2 = new HttpRequest();
    req2.setHeader('Content-Type','application/json');
     req2.setMethod('POST');
    req2.setEndpoint('https://XXXX.mktorest.com/rest/v1/leads.json?access_token='+getToken);
    req2.setBody('{"action":"createOnly","input":[{"email":"kjashaedd-1@klooblept.com","firstName":"Kataldar-1","postalCode":"04828"},{"email":"kjashaedd-2@klooblept.com","firstName":"Kataldar-2","postalCode":"04828"},{"email":"kjashaedd-3@klooblept.com","firstName":"Kataldar-3","postalCode":"04828"}]}}');

    // Send the request, and return a response
    HttpResponse res2 = h2.send(req2);
    return res2;

}



}

The MakeCallouts class is triggered when a Lead is created (this is entirely for testing purposes. I'm going to change the API call after I get this to work. But it's pretty obvious that the call is working when I see three new leads appear in my database, so it's a good test).

  • I'm going to try and help you through this, but I am still a little new to the REST API but I did get my own working today. First off, be very careful posting all of your info in your code. I'm hoping that is not your real client Id and client secret for everyone to see with the real endpoint and your own personal info. Which part are you currently stuck at? I see you are trying to authenticate and get your token. Have you achieved that so far? – Tyager Aug 20 '15 at 0:59
  • just wanted to know what objects u have created in salesforce to get token from marketo – maddy Feb 22 '17 at 20:35
3

There are two problem with your code:

  • When you make call to Marketo Rest API, it results application/json;charset=UTF-8 Content-Type header in response instead of application/json. That's the reason it is failing system assertion test, So I have corrected that line in your code System.assert(contentType == 'application/json;charset=UTF-8');
  • Endpoint URL https://XXXXX.mktorest.com in getInfoFromExternalService2(String) callout method is in uppercase letter and URL https://XXXX.mktorest.com in getInfoFromExternalService() callout method is in lowercase letter, so make sure you have listed both URLs in Remote site setting. Otherwise you can't make callout from salesforce.

    Location for Remote Site Settings : Setup | Administer | Security Controls | Remote Site Settings

Suggestion: Please keep endpoint URL in same letter case instead of listing both URLs differently in Remote Site Settings as remote site settings treat URL as case sensitive. Instead of hard coding the Client ID, Client Secret or Endpoint URL, persist them by creating Custom settings.

~Modified Code~

APEX CalloutClass

public class MarketoAPICallout {
    public static HttpResponse getInfoFromExternalService(){
    // Instantiate a new http object
    Http h = new Http();
    // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest r = new HttpRequest();
        //Make sure you have added this end point in Remote Site setting before making any callout
    r.setEndpoint('https://XXXX.mktorest.com/identity/oauth/token?grant_type=client_credentials&client_id=XXXXX&client_secret=XXXXXX');
    //r.setHeader('Content-type', 'application/json');
    r.setMethod('GET');
    return h.send(r);
    }

    public static HttpResponse getInfoFromExternalService2(String getToken){
        Http h = new Http();
        HttpRequest r = new HttpRequest();
        r.setHeader('Content-Type', 'application/json');
        r.setEndpoint('https://XXXXXXX.com/rest/v1/leads.json?access_token='+getToken);
        r.setMethod('POST');
        r.setBody('{"action":"createOnly","input":[{"email":"kjashaedd-1@klooblept.com","firstName":"Kataldar-1","postalCode":"04828"},{"email":"kjashaedd-2@klooblept.com","firstName":"Kataldar-2","postalCode":"04828"},{"email":"kjashaedd-3@klooblept.com","firstName":"Kataldar-3","postalCode":"04828"}]}}');
        return h.send(r);
    }
}

APEX Callout Demo Class

public class MarketoAPIDemo {
    public static void testCallout() {

        HttpResponse res = MarketoAPICallout.getInfoFromExternalService();
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json;charset=UTF-8');
        System.assertEquals(200, res.getStatusCode());
        String jsonResult = res.getBody();
        String token ;
        JsonParser p = Json.createParser(jsonResult);
        while(p.nextToken()!=null){
            if(p.getCurrentToken() == JSONToken.Field_Name){
                String fieldname = p.getText();
                if(fieldname == 'access_token'){
                    p.nextToken();
                    token = p.getText();
                }
            }
        }
        System.debug('@@@@@@@ TOKEN @@@@@@ :'+ token);
        HttpResponse res2 = MarketoAPICallout.getInfoFromExternalService2(token);
        String contentType2 = res2.getHeader('Content-Type');
        System.assert(contentType2 == 'application/json;charset=UTF-8');
        System.assertEquals(200, res2.getStatusCode());
        System.debug(res2.getBody());
    }

}
  • Thanks so much for the help! I've made the changes you suggested. However, it still doesn't seem to work. I also can't seem to see any of my debug contents even though I set everything to "finest" – MRG Aug 20 '15 at 19:02
  • This now works. The problem was with my trigger. Also, in order to call this from a trigger, I needed to add @future (callout=true) right before public static void testCallout() – MRG Sep 14 '15 at 17:01
  • Cool Cheers ... – Ajay Gupta Sep 14 '15 at 17:14

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.