"I'm interested in retrieving the most recent 30 'Send Email' activities for leads using the Marketo REST API, sorted by 'activityDate' in descending order (from the most recent to the least recent). Could you please guide me on how to achieve this?
Currently, my approach involves the following steps:
Retrieve the creation date of a lead using their leadID.
/rest/v1/leads.json?filterType=id&filterValues=5799
Create a 'nextPageToken' by setting the 'sinceDatetime' parameter to the lead's creation date.
/rest/v1/activities/pagingtoken.json?sinceDatetime=2023-11-04T09:29:50.839Z
Fetch all 'Send Email' activities until 'moreResults' is 'false' since the results are paginated.
/rest/v1/activities.json?leadIds=5799&activityTypeIds=6&nextPageToken=<value from step 2>
Sort the results based on 'activityDate' in descending order.
Return the top 'min(results.length, 30)' results.
However, I'm not entirely satisfied with this solution because it requires multiple API calls to retrieve the most recent 30 activities, starting from the beginning. Is there an alternative approach while getting nextPageToken for 1st page such that it gets me the desired result or do I have to provide sinceDatetime?
Solved! Go to Solution.
As you’ve already figured out, this is wildly inefficient.
First, Sent Email is only kept for 90 days. So there’s no reason to go back further than that.
There’s no specific way to “tail” the log using the API but I would suggest going back 30 days and scan forward, then if you need more go back 60 days and scan again.
You can also keep a field like Last Email Sent Week Ending updated (always recommended in any Marketo instance) using a weekly batch campaign so you know the latest practical date.
Why don't you just create the paging token from the system date and get the activity until you get the 30 counts of Send Email activities as a result? Or, as an equivalent step, you can use Bulk Activity Extract, and use the createdAt filter that includes the startAt and endAt attributes to get the 30 counts of the Send Email activities. You can always use the 31-day window (the maximum range allowed) and pick the first 30 Send Email activities. Since you need the latest activities, there's little point in querying for the activities the other way around.
As you’ve already figured out, this is wildly inefficient.
First, Sent Email is only kept for 90 days. So there’s no reason to go back further than that.
There’s no specific way to “tail” the log using the API but I would suggest going back 30 days and scan forward, then if you need more go back 60 days and scan again.
You can also keep a field like Last Email Sent Week Ending updated (always recommended in any Marketo instance) using a weekly batch campaign so you know the latest practical date.