Detect which field changed after admin update [RESOLVED]

Is there a way to know which field changed when an admin makes an update to an entry?

It is possible to track which fields were updated in a Gravity Forms entry by utilizing the gform_after_update_entry hook, which is triggered after an entry is updated. Within the callback function for this hook, you can access the original and updated entry data, and compare the two to determine which fields have been altered.

Alternatively, you can also track changes by using the GravityRevisions or Entry Revisions Add-on. This allows you to keep a record of the modifications made to an entry and revert back to previous versions if necessary.

Thanks so much Faisal – this works beautifully.

I would also like to trigger an email notification. How would I accomplish that within the gform_after_update_entry hook?

You can use the wp_mail() function to send an email notification within the callback function for the gform_after_update_entry hook.

Here is an example of how you can send an email notification when a specific field is updated:

add_action( 'gform_after_update_entry', 'send_update_notification', 10, 2 );

function send_update_notification( $form, $entry_id ) {
  
  // Get the original entry
  $original_entry = GFAPI::get_entry( $entry_id );
  // Get the updated entry
  $updated_entry = GFFormsModel::get_current_lead();
  
  // Compare the two entries to determine which fields have been updated
  $updated_fields = array();
  foreach ( $updated_entry as $key => $value ) {
    if ( $original_entry[ $key ] !== $value ) {
      $updated_fields[] = $key;
    }
  }
  
  // Check if the specific field has been updated
  if ( in_array( 'your_field_id', $updated_fields ) ) {
    // Send email notification
    wp_mail( 'your@email.com', 'Gravity Form Entry Updated', 'An entry has been updated in your Gravity Form.' );
  }
}

In this example, we use the GFAPI::get_entry() function to retrieve the original entry and GFFormsModel::get_current_lead() function to obtain the updated entry. We then compare the two entries to determine which fields have been modified and verify if a specific field has been altered.

You can customize the subject and body of the email and employ various mailer plugins such as Mailgun or SendGrid to send email notifications.

Ensuring that your server is correctly configured to send emails and that your hosting provider permits sending emails from your domain are essential.

1 Like

Thanks so much for the example, Faisal. I have been using the “gform_after_update_entry” hook, but this is another way which is always good to learn.

Is there any way to trigger an email that is already configured as a GForms notification? Currently, users are using the “Resend” option inside the edit screen, but I’d like to automate it.

1 Like

Hi Margaret. To trigger an existing notification (rather than using wp_mail to send a message) please use GFAPI::send_notifications:

Basically, this line:

wp_mail( 'your@email.com', 'Gravity Form Entry Updated', 'An entry has been updated in your Gravity Form.' );

Will be replaced by something like this:

GFAPI::send_notifications( $form, $entry, 'my_update_event' );

my_update_event will be a custom notification event you create:

1 Like

This is perfect, Chris, works exactly how I need it to. Thanks so much!

1 Like