Gform_after_submission and GFAPI:: update entries

I am trying to use gform_after submission to update two separate fields on a form (Id=1) with two different specific URL links. One (field id -111) is a link to a file and the other (field id - 115) is a link to a page. Both URL links include within them a Gravity Perks unique ID field ( field id-90) that is only defined on submission.

At the moment I am using two separate php snippets. Each works OK when activated individually but when both snippets are activated simultaneously the snippet 2 works but snippet 1 does not. Perhaps doing separate snippets is the issue here? Is there a better syntax to populate these URLs into the fields with just one snippet please? Thanks.

Snippets below

snippet 1

add_filter( 'gform_after_submission_1', 'combine_id_name', 10, 2 );

function combine_id_name( $entry, $form_id ) {
    $unique_field = '90';  // Change this to the ID of the Unique Field
    $name_field =  "2";   // Change this to the ID of the Name field
    $id_name = "111";   // Change this is the Id of the hidden single line text field that will hold the combined values
    $entry[$id_name] = "https://casswizard.com/web/wp-content/uploads/sites/555/gravity_forms/1-xxxxxxxxxxx7/Files/Insurer-Terms/". $entry[$unique_field] .'-'. sanitize_title ($entry[$name_field])."/". $entry[$unique_field] .'-'. sanitize_file_name ($entry[$name_field]).".pdf";
    $result  = GFAPI::update_entry( $entry );
    return $result;
}

snippet 2

add_filter( 'gform_after_submission_1', 'combine_view_name', 10, 2 );

function combine_view_name( $entry, $form_id ) {
    $unique_field = '90';  // Change this to the ID of the Unique Field
    $view_name = "115";   // Change this is the Id of the hidden single line text field that will hold the combined values
    $entry[$view_name] = "https://casswizard.com/brokerltd/insurer-toba-3/?wdt_column_filter[0]=". $entry[$unique_field];
    $result  = GFAPI::update_entry( $entry );
    return $result;
}

Hey,

A couple areas you may want to focus for getting it working:

  • gform_after_submission is an action (not filter) so your initial calls should be add_action instead of add_filter and avoid having the return at end.

  • The GFAPI::update_entry function returns “True for success or a WP_Error instance”. You may want to inspect/error_log that value to see if your first snippet is encountering an error on the update?

  • The $entry parameter which gets passed into the action is not updated in between each hook execution. That’s what filters are for. You could either call GFAPI::get_entry within each snippet, or it would be a little more efficient to use GFAPI::update_entry_field instead of updating the entire entry. This is the main reason that you are finding only 1 update result for what you want to modify.

Did you know that for scenarios such as this the Gravity Flow Form Connector’s step types to either Update Field or Update an Entry can provide a no-code approach to updates? Your second snippet appears to only using hard coded values or field/merge tags that would be a perfect fit. For ones like your first snippet, where you are running additional PHP functions on the to-be-returned value, the Form Connector does have a filter gravityflowformconnector_update_field_values that lets you do similar in a low-code approach.

Regards,
Jamie

1 Like

Jamie Thanks

I have updated the snippets to actions but that has made no difference. They both work individually but when both are activated it still only executes snippet two. How would I convert Snippet 1 and 2 to use GFAPI::update_entry_field? I have tried the following and they don’t work so I’m guessing i have got the syntax wrong ( I’m not a coder!)
Snippet 1 GFAPI::update_entry_field( $form_id, $id_name, $entry )
Snippet 2. GFAPI::update_entry_field( $form_id, $view_name, $entry )

Thanks

Also, once I have got the syntax correct, can I not put the update of both fields into one function by having two GFAPI::update_entry_field statements in it?
Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.