The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Automatically update a field in another form

Labels

This Discussion is public

Notifications

I (admin) would like to use this code to update a field in form A and have it update similar field in form B, which is the users form, so the code as is wont work.  To link forms I added the entryid from form B and inserted into a field I called, entryid. Contact email address is also the same in both forms.

frm_after_update_entry

Thanks,
Geno

 

The frm_after_update_entry example code for automatically updating a field in another form is based on forms that have been entered by logged in users, not an admin. The user id has to be identical in both Form A and Form B to work. You'll have to customize that code to work based on entryid instead of user id.

Thanks.  I do understand I have to modify code, but not sure how.  I'll go ahead and give it a shot.

I tried the following code and it didn't work.  I'm trying to update the second form where the entry_id of second form equals the entryid field, 437 of first form.  I made in bold the code that I thought would work, but I got a 500 error.

 

add_action('frm_after_create_entry', 'link_fields', 30, 2);
add_action('frm_after_update_entry', 'link_fields', 10, 2);
function link_fields($entry_id, $form_id){
if($form_id == 39){//Change 39 to the ID of the first form
global $wpdb;
$paypal_field = $_POST['item_meta'][436]; first form
$entryid_field = $_POST['item_meta'][437]; first form
$user = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM ". $wpdb->prefix ."frm_items WHERE id=%d", $entry_id));
$entry_ids = $wpdb->get_col("Select id from ". $wpdb->prefix ."frm_items where form_id='32' and entry_id=$entryid);//Change 32 to the ID of the second form
foreach ($entry_ids as $e)
$wpdb->update( $wpdb->prefix .'frm_item_metas', array('meta_value' => $paypal_field), array('item_id' => $e, 'field_id' => '410'));//Change 410 to the ID of the field to be updated automatically in your second form
}
}

505 because lines 6 & 7 are not commented appropriately at the end?

first form should be //first form

Thanks Bobby for noticing the comments weren't correct.  I also noticed I made an error on the variable created $entryid_field then called  $entryid. I'll go ahead and make changes and hopefully it'll work

I made all the necessary changes, but it's still not working.  What am I missing?

add_action('frm_after_create_entry', 'link_fields', 30, 2);
add_action('frm_after_update_entry', 'link_fields', 10, 2);
function link_fields($entry_id, $form_id){
if($form_id == 39){//first form
global $wpdb;
$paypal_field = $_POST['item_meta'][436]; //first form
$entryid_field = $_POST['item_meta'][437]; //first form
$entry_ids = $wpdb->get_col("Select id from ". $wpdb->prefix ."frm_items where form_id='32' and entry_id=$entryid_field);//second form
foreach ($entry_ids as $e)
$wpdb->update( $wpdb->prefix .'frm_item_metas', array('meta_value' => $paypal_field), array('item_id' => $e, 'field_id' => '410'));//second form
}
}

You are missing of matching braces and parens. Here's the corrected code:

add_action('frm_after_create_entry', 'link_fields', 30, 2);
add_action('frm_after_update_entry', 'link_fields', 10, 2);

function link_fields($entry_id, $form_id) {

if($form_id == 39){ //first form

global $wpdb;

$paypal_field = $_POST['item_meta'][436]; //first form

$entryid_field = $_POST['item_meta'][437]; //first form

$entry_ids = $wpdb->get_col("Select id from ". $wpdb->prefix ."frm_items where form_id='32' and entry_id = $entryid_field"); //second form

foreach ($entry_ids as $e) {

$wpdb->update( $wpdb->prefix .'frm_item_metas', array('meta_value' => $paypal_field), array('item_id' => $e), 'field_id' => '410');//second form

}
}
}

Discussion closed.