Submitting second from using response of first form [RESOLVED]

Form 1 is submitted and then I get a response like below:

2023-04-09 2:11:44.988986 - DEBUG → GF_Webhooks::process_feed(): Webhook successfully executed. code: 200; body: {“generated_in”:“0.03”,“status”:“ok”,“person”:{“id”:“15672a6b-0974-435e-a6e5-e5b9c073fc82”,“family_id”:“”}}

I want to parse and grab the id value in the above response and submit a second form:

I tried below where 8 is form id of first form and 9 is second form id of second form. But I don’t see second webform submitted at all in the logs:

add_action( 'gform_webhooks_post_request_8', function ( $response, $feed, $entry, $form ) {
    if ( ! is_wp_error( $response ) ) {
        #GFAPI::delete_entry( $entry['id'] );
		$form_id = "9";
		$person_id = $response["person"]["id"];
		GFAPI::submit_form( $form_id , $person_id );
    }
}, 10, 4 );

Also not sure if I am parsing the response properly.

Add some custom logging statements to your code. First, enable Gravity Forms logging: https://docs.gravityforms.com/logging-and-debugging/#h-enabling-logging

Then add some custom logging statements to your code following these examples: https://docs.gravityforms.com/custom-logging-statements/#h-writing-to-the-core-log

I recommend adding a statement right inside your function to ensure it’s running. Then add a statement any time you assign a value to a variable, and log the return value from any function call you make. Try something like this:

add_action( 'gform_webhooks_post_request_8', function ( $response, $feed, $entry, $form ) {
    GFCommon::log_debug( current_action() . '(): running.' );
    GFCommon::log_debug( current_action() . '(): My Response => ' . print_r( $response, true ) );
    if ( ! is_wp_error( $response ) ) {
        GFCommon::log_debug( current_action() . '(): My Entry => ' . print_r( $entry, true ) );
        #GFAPI::delete_entry( $entry['id'] );
		$form_id = "9";
        GFCommon::log_debug( current_action() . '(): My Form ID => ' . print_r( $form_id, true ) );
		$person_id = $response["person"]["id"];
		GFCommon::log_debug( current_action() . '(): My Person ID => ' . print_r( $person_id, true ) );
		$result = GFAPI::submit_form( $form_id , $person_id );
        GFCommon::log_debug( current_action() . '(): My Result => ' . print_r( $result, true ) );
    }
}, 10, 4 );

With that revised code and Gravity Forms logging enabled, test your form, then check the Gravity Forms Core log and search for the statements that begin "My ". Let us know what you find out!

1 Like

Thank You Chris for helpful response. I see one issue is grabbing the person_id.

The Webhook reply I get from first form submission:

2023-04-10 18:08:36.932667 - DEBUG → GF_Webhooks::process_feed(): Webhook successfully executed. code: 200; body: {“generated_in”:“0.04”,“status”:“ok”,“person”:{“id”:“5b240c5e-f797-4da1-ae7f-db0fee0cd152”,“family_id”:“”}}

This is how I am trying to grab person_id, but is not working:

$person_id = $body[“person”][“id”];

2023-04-10 18:08:36.933114 - DEBUG → gform_webhooks_post_request_8(): My Form ID => 9
2023-04-10 18:08:36.933171 - DEBUG → gform_webhooks_post_request_8(): My Person ID =>

The response body is JSON encoded, so you’ll need to decode it before you can access its values e.g.

$body_json  = wp_remote_retrieve_body( $response );
$body_array = json_decode( $body_json, true );
$person_id  = rgars( $body_array, 'person/id' );

You also need to make sure you are passing the correct values to GFAPI::submit_form(). If you check the following page of the documentation, you’ll find the second arg should be an array of input values, not just your person id.

1 Like

Thank you Chris and Richard. It works now with the suggested code. For people_id I used $input_values[‘input_1’] = $person_id;.

1 Like

Glad to read that you found a fully functional solution with the code snippets.

For others following along that might like to do the same without requiring any code: