Create an entry in another form query

By: Jon Stu | Asked: 03/20/2024
ForumsCategory: Code SnippetsCreate an entry in another form query
Jon Stu asked 9 months ago
In the knowldegbase for frm_after_create_entry
There is an example: Create an entry in another form#
See Below. Can I just ask how do I add another 2 fields from Form A (like 6614) to populate in Form B (6622).
I can\'t workout how to add them in.
Also, it says:
\'item_key\' => \'entry\', //change entry to a dynamic value if you would like
How do I make this dynamic, I would like \"entry name\" and the \"Created by fields\" of Form B entry to be the same as Form A.   add_action(\'frm_after_create_entry\', \'create_entry_after_submission\', 30, 2); function create_entry_after_submission($entry_id, $form_id){
if ( $form_id != 372 ) {//Change 372 to the ID of Form A
return;
} $form2 = \'373\';//Change 373 to the ID of Form B
//Get user
if (!isset($_POST[\'frm_user_id\'])){
return;
}
$user = $_POST[\'frm_user_id\'];
//get data
if (!isset($_POST[\'item_meta\'][6614])){ //replace 6614 with the field ID of the Form A value
return;
}
$new_value = $_POST[\'item_meta\'][6614];
//create entry
FrmEntry::create(array(
\'form_id\' => $form2,
\'item_key\' => \'entry\', //change entry to a dynamic value if you would like
\'frm_user_id\' => $user,
\'item_meta\' => array(
6620 => $entry_id, //change 6620 to the ID of the field you want to populate (in Form B) with the entry ID of Form A
6621 => $user,//change 6621 to the ID of the userID field in Form B
6622 => $new_value,//Change 6622 to the ID of a field in Form B you want to populate with a value from Form A
),
));
}  
2 Answers
Rob LeVineRob LeVine answered 9 months ago
You add id/value pairs to the item_meta array that's at the end of the FrmEntry::create command.
Jon Stu replied 9 months ago

Thanks this has worked!
I created some $newvalues above the FrmEntry::create command, and then added in the mapped fields after!

Jon Stu answered 9 months ago
This worked but I would like to amend this, to not create an entry in form b, if a field in Form A is set to "draft" I tried this but it errored out any ideas why my conditional draft code didn't work? add_action('frm_after_create_entry', 'create_entry_after_submission', 30, 2);

function create_entry_after_submission($entry_id, $form_id){
    if ( $form_id != 4 ) {//Change 4 to the ID of Form A
        return;
    }

    $form2 = '64';//Change 64 to the ID of Form B
    //Get user  
    if (!isset($_POST['frm_user_id'])){
        return;
    }
    $user = $_POST['frm_user_id'];
    //get data
    if (!isset($_POST['item_meta'][99])){ //replace 99 with the field ID of the Form A value
        return;
    }
    $new_value = $_POST['item_meta'][99];
    $value_email = $_POST['item_meta'][19];
    
   // Check if the specific field 994 value in Form A is not "draft"
        $draft_field_id = '994';
        $draft_field_value = $entry->metas[ $draft_field_id ];
        if ( $draft_field_value === 'draft' ) {
            return; // If the field value is "draft", do not proceed with creating an entry in Form B
        }

    
    //create entry
    FrmEntry::create(array(
        'form_id' => $form2,
        'item_key' => $value_email, //change entry to a dynamic value if you would like
        'frm_user_id' => $user,
        'item_meta' => array(
             996 => $entry_id, //change 996 to the ID of the field you want to populate (in Form B) with the entry ID of Form A
             877 => $user,//change 877 to the ID of the userID field in Form B
             995 => $new_value,//Change 995 to the ID of a field in Form B you want to populate with a value from Form A
             997 => $value_email,
             998 => $_POST['item_meta'][55],
             1000 => $_POST['item_meta'][106],
             1001 => $_POST['item_meta'][274],
             999 => $_POST['item_meta'][22],
             1006 => $_POST['item_meta'][991],
           
           
        ),
    ));
}
Jon Stu replied 9 months ago

Forgot to say it errors our at: $draft_field_value = $entry->metas[ $draft_field_id ];

Rob LeVineRob LeVine replied 9 months ago

Normally you'd want to identify what the error is as well, however, it's pretty obvious from the code that $entry is null, because you didn't set it. You need to execute $entry = FrmEntry::getOne($entry_id, true); before the line with the error.

Jon Stu replied 9 months ago

Thanks! This is great.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right