Tray Platform / Standard/Best Practices / Pagination / Pagination example

Pagination example

Introduction

This article is adapted from a Tray blogpost titled Are Non-Paying Customers Burning Out Your Support Team? Here’s How to Fix It (note that the original blogpost was written before the 'loop forever' operation was available - so you should definitely use this article as your guide!)

The idea here is that you are making use of Salesforce as a CRM to manage accounts and contracts, alongside Stripe as a payment system, and you are wanting Tray to take care of the task of automatically checking if any of your contracts are not yet linked to Stripe accounts - thus making sure that non-paying customers aren't taking up your valuable time!

The tutorial below will take you through setting up a workflow which runs periodically and uses a 'pagination' system which can loop through dozens of records and stop when the job is done.

A basic breakdown of what we will be achieving is:

  1. Use the Stripe List customers operation to get all customer data

  2. Use Loop Connectors to create batches of customers which are then sub-processed one-by-one, grabbing their Stripe ID and email address

  3. For each customer presented by the loop, use the SalesForce Find Record operation (by email) to get the SalesForce ID and Stripe ID as stored in SalesForce

  4. Use a Boolean Connector to check if the Stripe ID for that Salesforce Contract is empty

  5. If so then update the Contract with the Stripe ID

This tutorial will be divided into two stages - setting up the system to paginate through the Stripe list of customers, followed by adding the steps to find each customer in Salesforce and updating their Stripe ID if needed.

Set up the pagination system

When returning records, the Stripe API returns a maximum of 100 records at a time, in a list sorted by ID. If there are more than 100 records to be returned, Stripe returns a has_more property so you know whether to make another request. It also lets you pass in a value called Starting After so that you can enter the ID of the last customer in the previous list of 100; thus Stripe will know to start the next batch of 100 from the customer which comes after this ID.

Because we can't get a count of the total records to be returned by Stripe, we will have to set our main loop as a continuous loop which breaks once the has_more property returns True; and within this main loop we will create the sub loop which processes the customers from the main loop batch one-by-one.

Start your workflow with a scheduled trigger set to run e.g. every Monday at 6am

Your complete workflow should look like this:

complete-workflow-2

You can then move on to setting up the pagination:

Pagination - Set the main and sub loops

full-pagination-workflow

The steps involved here are:

Once you have been through all the above tabs which explain the pagination setup, we can move on with further steps:

Find and update Salesforce contracts with missing Stripe ID

Now we just need to add the steps which will find the relevant Salesforce contracts and update their Stripe ID, if it is not found.

This can be done by adding some steps to the beginning of the customer loop, just before the Last customer in batch? step:

Note on testing

In order to test a setup such as this, it would be standard to use a manual trigger instead of a scheduled trigger, so you can click to run at any time.

When a run of the workflow has been triggered you can click on the 'Debug' tab to check if it was successful and view the input/output of each step in order to troubleshoot.