0

I'm an absolute newbie to Pardot. I need to upsert prospects using the API using Apex and I was following this guide for authentication. https://thespotforpardot.com/2021/02/02/pardot-api-and-getting-ready-with-salesforce-sso-users-part-3a-connecting-to-pardot-api-from-apex/

The example code given there to get your pardot account info seems to work fine.

I'm using this code to upsert the prospects, and where I believe the error is:

HttpRequest upsertProspect = new HttpRequest();                 
upsertProspect.setEndpoint('callout:APEX_Pardot_Credential/prospect/version/4/do/batchUpsert?format=json'); 
                    upsertProspect.setMethod('POST');
                    upsertProspect.setHeader('Pardot-Business-Unit-Id', buid);
                    upsertProspect.setBody(JSON.serialize(pBody));
                    system.debug(upsertProspect.getBody());
                    HttpResponse upsertResponse = http.send(upsertProspect);
                    system.debug(upsertResponse);

Where APEX_Pardot_Credential is a named credential with the base pardot api url( https://pi.pardot.com/api), pBody is an instance of a custom class containing a list of prospects, and buid is a variable containing the Pardot Business Unit Id.

Edit: Here's the rest of the code, though most of it is just sorting through the Contacts List and the meat of the request is the code above.

public class PardotWebServiceCallout {
@future (callout=true)
public static void upsertProspect(List<Id> contactIds) {
        
        String buid = Query to get Business Unit Id from custom metadata
           
        List<list<id>> idsList = new list<list<id>>();
        for(Integer i = 0 ; i < (contactIds.size() / 50)+1 ; i++){
                List<Id> lstTemp = new List<Id>();
                for(Integer j=(i*50);(j<(i*50)+50) && j<contactIds.size() ; j++){
                    lstTemp.add(contactIds.get(j));
                }
                idsList.add(lstTemp);
            }
        for(List<id> i : idsList){ 
        
            ProspectBody pBody = new ProspectBody();
            List<Prospects> prospectList = new List<Prospects>();
        
        
            for (Contact con : [Select Id,Email, FirstName, LastName From Contact Where Id IN: i]) {
                Prospects prosp = new Prospects();
                
                prosp.fid = con.Id;
                prosp.last_Name = con.LastName;
                prosp.first_Name = con.FirstName;
                prosp.email = con.Email;
                prospectList.add(prosp);
                           
            }
            
            pBody.prospects = prospectList;
            
            HttpRequest upsertProspect = new HttpRequest();
             
           upsertProspect.setEndpoint('callout:APEX_Pardot_Credential/prospect/version/4/do/batchUpsert?format=json'); 
            upsertProspect.setMethod('POST');
            upsertProspect.setHeader('Pardot-Business-Unit-Id', buid);
            upsertProspect.setBody(JSON.serialize(pBody));
            system.debug(upsertProspect.getBody());
            HttpResponse upsertResponse = http.send(upsertProspect);
            system.debug(upsertResponse);
        }
}
public class ProspectBody{
    public List<Prospects> prospects;
    
    public ProspectBody(){
        
    }
}

public class Prospects {
    
    public String email;
    public String first_name;
    public String last_name;
    public String fid;
    public Prospects(){
        
    }
}

}

I was getting a bad response status code 400 error, but after working some more on it now I'm getting the following.

15:53:19:438 FATAL_ERROR System.CalloutException: Illegal character in opaque part at index 75: callout:APEX_Pardot_Credential/prospect/version/4/do/batchUpsert?prospects={"prospects":[{"last_name":"TESTING","first_name":"TEST","fid":"0044F00002f5DS7RML","email":"[email protected]"}]}

Any help would be appreciated.

4
  • Your code looks ok but there are lots of reasons why it could return 400. Please edit your question and provide a Minimal, Reproducible Example
    – identigral
    Aug 3, 2021 at 20:16
  • Sorry, I didn't think it was relevant because it's just me sorting through the list of contacts to divide it into something manageable by the batchUpsert. But I added the rest of the code.
    – Crimson97
    Aug 3, 2021 at 22:30
  • Try stuffing your JSON into the prospects query param: .../do/batchUpsert?format=json&prospects=url_encode(JSON.serialize(pBody)). Replace url_encode by code from this
    – identigral
    Aug 3, 2021 at 23:39
  • 1
    Thanks! This solved the issue.
    – Crimson97
    Aug 4, 2021 at 17:16

1 Answer 1

0

I think the challenge with your original code is that you were POSTing a body of a RAW JSON payload, when it needs to be in x-www-form-urlencoded format. You kind of see what's going on with @identigral's answer, where you have added: prospects=JSONPAYLOAD to the URL itself.

Pardot isn't really helping with mixing the two, especially with what most API devs are used to doing.

Should you find that your URLs are getting too big, you likely can just move what you've come across into the body itself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .