implementing external activity natively in salesforce

Implementing Pardot External Activities Natively in Salesforce

Pardot is delivering a whole new way to leverage your prospect data in the Salesforce Winter ‘22 release. Our earlier blog post covers this new feature and how to set up the Pardot External Activity in Salesforce so any third-party service can begin sending these activities to Pardot via API. This post explains what third-party services need to do to send these activities to Salesforce using Salesforce declarative solutions (Flow/Process builder). 

At a high level, we need to:

  • Configure Salesforce to allow our solution to call the Pardot API
  • Implement Salesforce APEX code to handle the Pardot API request
  • Add an action to a Flow to make use of our new code
  • Test

This solution is a little more technical than our post on Zapier. Once you are done, you will end up with a Flow like this:

Start Record-triggered flow

Configure Salesforce

Any time we want to work with the Pardot API, we need to “authenticate” with Salesforce in order to get an Access Token. 

First, follow the steps in our earlier blog post Connecting to Pardot API from APEX. By the end, you should have:

  • A brand new Connected App (to avoid issues, don’t re-use previously created Connected Apps unless they were created using the instructions above) 
  • Named Credential for connecting to the API 

Salesforce APEX code

To build this capability, we need to create an @InvocableMethod so that our Salesforce declarative automations can see it and call it to do our bidding.As with any code solution, there are a variety of ways that we can tackle this. The code sample below will work for readers with one Pardot Business Unit. The original code file (and APEX Tests) can be found in our GitHub repository: external-activities-sfdx

Note: Sorry our code formatting isn’t the best. You can copy-paste the following code into a file and it will work as expected.

public with sharing class PardotExternalActivityPublisher {
    public static final Integer HTTP_REQUESTS_PER_BATCH = 50;
    public static final String ONLY_ONE_BUSINESS_UNIT_ID = '0UvB00000004000AAA';
    public static final String NAMED_CREDENTIAL = 'APEX_Pardot_Credential';

    public class ExternalActivity {
        // @InvocableVariable(label='Business Unit Id')
        // public String businessUnitId;
        @InvocableVariable(label='Extension' required=true)
        public String extension;
        @InvocableVariable(label='Type' required=true)
        public String type;
        @InvocableVariable(label='Value' required=true)
        public String value;
        @InvocableVariable(label='Prospect Email' required=true)
        public String email;
    }

    @InvocableMethod(label='Send Activity to Pardot')
    public static void sendActivityToPardot(List<ExternalActivity> activities) {
        //Very quickly pass this request into the ASYNC Queue, eliminating delays for Users
        System.enqueueJob(new QueueablePardotCall(activities));
    }

    /**
     * Handles Asynchronously firing each Activity to Pardot
     */
    public class QueueablePardotCall implements System.Queueable, Database.AllowsCallouts {
        private List<ExternalActivity> activities;

        public QueueablePardotCall(List<ExternalActivity> activities) {
            this.activities = activities;
        }

        public void execute(System.QueueableContext ctx) {
            //depending on how many Activities we are processing, 
            //we might hit the APEX limit of 100 Web Callouts
            List<ExternalActivity> remainingActivities = new List<ExternalActivity>();
            Integer processedCount = 0;

            for(ExternalActivity activity : activities) {
                if(processedCount < HTTP_REQUESTS_PER_BATCH ) {
                    HttpRequest req = new HttpRequest();
                    req.setHeader('Pardot-Business-Unit-Id', ONLY_ONE_BUSINESS_UNIT_ID);
                    req.setHeader('Content-Type', 'application/json');
                    // req.setHeader('Pardot-Business-Unit-Id', activity.businessUnitId);
                    // activity.businessUnitId=null;

                    req.setEndpoint('callout:'+NAMED_CREDENTIAL+'/v5/external-activities');
                    req.setMethod('POST');
                    String body = System.JSON.serialize(activity, true);
                    System.debug('Submitting: ' + body);
                    req.setBody(body);
                    Http http = new Http();
                    try {
                        http.send(req);
                    }
                    catch(Exception e) {
                        //we fire it off and don't do anything if there's an error
                        //probably not the best approach for Production, though it will
                        //be up to you how to handle it
                        System.debug('There was an error submitting the External activity');
                        System.debug('Message: ' + e.getMessage() + '\n' +
                                        'Cause: ' + e.getCause() + '\n' +
                                        'Stack trace: ' + e.getStackTraceString());
                    }
                    processedCount++;
                }
                else {
                    //we will process this in the next batch of Payloads
                    remainingActivities.add(activity);
                }
            }
            if(!remainingActivities.isEmpty()) {
                System.enqueueJob(new QueueablePardotCall (remainingActivities));
            }
        }
    }
}

To use this code, make sure you replace the Business Unit ID at the top of the code with your Business unit ID (to find this, navigate to Salesforce Setup > Pardot Account Setup).

For readers with multiple Pardot Business Units, remove the constant ONLY_ONE_BUSINESS_UNIT_ID and then uncomment the businessUnit lines throughout. You will need to either specify the Business Unit ID in your Flow, or you could write additional APEX to iterate through your Pardot Business Units by working with the PardotTenant object in Salesforce.

You might also want to specify how you want to handle any exceptions you get from making the Pardot API call. In our example, we simply write exceptions to the debug log.

Our APEX code does assume that the Contact has synced over to Pardot already. If you can’t make this assumption, you may consider calling a Pardot Form Handler to make sure that the prospect is in Pardot already. We have an APEX example for that too (which follows a very similar pattern, so it should be easy to merge them).

Adding an Action to a Flow

Once the APEX has been deployed, you will now be able to use it declaratively.

In our example, we have a Zoom Webinar Member (which is a Junction Object between a Zoom Webinar and a Contact).

To set this up in a Flow:

  1. Navigate to Setup > Flows
  2. Select “New Flow” or edit an existing Flow
  3. Select the + symbol to add a new Element, select “Action”
  4. In the “Search all actions” window, locate “Send Activity to Pardot”
  5. Provide a meaningful Label and Description
  6. Set your input values
    1. Extension: Enter the name of the Marketing App Extension you created in Salesforce
    2. Prospect Email: Source the email from one of the fields/variables in your flow
    3. Type: Enter one of the activities you set up and associated with your Marketing App Extension in Salesforce
    4. Value: Enter (or source from a field/variable) the unique value to identify this Activity, event IDs work great here
  7. Click “Done”
Send activity to pardot

Test

Once all elements of your Flow are ready, testing can begin. Activate your Flow and perform the action you are using to trigger the Flow. After a couple of moments, check the Pardot prospect you are testing with, and you should now see all the information you passed through.

prospect activities

Testing is a little bit tricky, for two reasons:

  1. We are executing this functionality asynchronously, meaning a problem won’t show up in Salesforce like you are used to seeing. Debug logs will be your friend here. But don’t worry, there isn’t too much to sort through.
  2. If the Named Credential or anything else isn’t quite set up right (from step 1), Salesforce and debug logs aren’t very helpful in troubleshooting. You will have to painstakingly go through the instructions again to make sure that nothing was missed / done incorrectly.

Considerations

  • The Export Activity API call only works for known prospects, and it will not work if the email address is not already associated with a prospect in your Pardot Business Unit (this is why we have the form handler in our example).
  • If you have multiple Pardot Business Units, there is no intelligence of “choosing the right one.” You need to target the right one with your APEX solution, which assumes all prospects going through this code are from the same Pardot Business Unit. As we mentioned in the APEX section, you have the flexibility to code whatever you need to handle your business case. 

 For assistance with this or other Pardot External Activities, reach out to Sercante!

Published by

Adam Erstelle

Adam Erstelle is the VP of Engineering, Labs with Sercante. He loves learning about and solving really interesting challenges with Pardot and Salesforce often by building some cool tools.

Leave a Reply