Using real form entries as Zapier test data

Summary

This article provides a code snippet that you can use to fetch actual form submissions from your entries table to see when you are testing out your Zapier Zap. This is to address a need that some Zapier users have requested with the release of the official Gravity Forms Zapier Add-On v4.

Background

It may seem like this ability existed before version 4, but in actual fact it was a somewhat happenstance. There was no way for Zapier to fetch sample data on demand so they listened for it instead. The Gravity Forms Zapier Add-On would send sample data to the zap URL when you saved the feed or when you saved changes to the form.

While the integration was never designed to use form submissions (entries) as sample data it was possible that if the form happened to be submitted at the time Zapier was listening for the sample data they would also pick up the real entry data as well.

The new version of the Gravity Forms app that is available on Zapier.com does not listen for incoming sample data. We have setup a registered API endpoint where the app can request the sample data on demand, but both the Zapier app and Gravity Forms add-on require updates to utilize it. This code snippet is a work around in the meantime.

Code Snippet

Add this to your theme’s functions.php file, or within any custom functions plugin you may be using.

This code snippet will override the response of the REST API request which gets the sample data with the latest real form entry.

/**
* Replaces the Gravity Forms Zapier sample entry response with the latest real entry, if available.
*
* @param WP_REST_Response $response The response to be returned.
* @param WP_REST_Server   $server   The current instance of the REST API server.
* @param WP_REST_Request  $request  The request which is currently in progress.
*/
add_filter( 'rest_post_dispatch', function ( $response, $server, $request ) {
if ( ! function_exists( 'gf_zapier' )
|| $response->get_status() !== 200
|| $request->get_method() !== 'GET'
|| empty( $request['form_id'] )
|| $request->get_route() !== "/gf/v2/forms/{$request['form_id']}/sample-entry"
|| ! GFAPI::current_user_can_any( 'gravityforms_view_entries' )
) {
return $response;
}

$entries = GFAPI::get_entries( $request['form_id'], array( 'status' => 'active' ), array(), array( 'page_size' => 1 ) );
if ( empty( $entries[0]['id'] ) ) {
return $response;
}

$form = GFAPI::get_form( $request['form_id'] );
$feed = array( 'meta' => array( 'adminLabels' => false ) );

return rest_ensure_response( gf_zapier()->get_body( $entries[0], $form, $feed ) );
}, 10, 3 );

If you need the entry data to use the field admin labels change 'adminLabels' => false to 'adminLabels' => true.