Conditional repeater email?

By: Jane Onorati | Asked: 11/15/2024
ForumsCategory: How-toConditional repeater email?
Jane OnoratiJane Onorati asked 4 weeks ago
If I set up an email action conditional on a field value in the repeater, the email does not send no matter if the condition is met or not.  I've made a very simple form to test this.  If I remove the condition, the email will send.  Also, if I make the condition on a field that is not in the repeater, the email will send.  The email is set to run this action for the repeater instead of the main form. Any ideas on how to make this work with a conditional value in the repeater?  I've tried it both on entry creation and on update, but I intend to use this for updates.  The repeater contains email addresses.  I want to be able to check the boxes to send emails to only select addresses in the repeater, and not all.
3 Answers
Jane OnoratiJane Onorati answered 3 weeks ago
I asked Formidable Support about why using a repeater field for a mail action condition did not work.  I was told that even though you can select a repeater field for a condition, only fields outside a repeater will work. I asked that Formidable's documentation be updated to inform users of this and remove repeater fields from the condition dropdown, or make repeater fields work as conditions in mail actions.
Victor Font replied 3 weeks ago

I'll bet it's because repeaters are actually embedded forms and not really a component of the parent form. I wish I had the time to do some reverse engineering and figure out how to add repeaters to the parent form conditionals.

There may be a work around you can try with frm_after_create_entry hook. Repeater forms are processed by Formidable before the parent form. All the hooks that work for parent forms work for repeaters individually as well. You may be able to trigger an email manually when the repeater is processed if you can send it before the parent form is fully processed.

Jane OnoratiJane Onorati answered 3 weeks ago

Hi Victor!  This is really hard but I'm really stubborn!  I can't use the frm_after_create_entry hook because I want this to happen on update.  So I thought I should try the frm_pre_update_entry hook.  I used that before to stop or allow an email to send successfully but not using a repeater. Below is the code I tested on an entry with just one repeater row.  I updated a name field in the repeater which this code is set to check.  It is basically the same as the first example here: https://formidableforms.com/knowledgebase/frm_pre_update_entry/

add_filter( 'frm_pre_update_entry', 'check_if_value_changed', 10, 2 );
function check_if_value_changed( $values, $entry_id ) {
	$display = "Entry ID " . $entry_id;
	console_log ($display);
	$field_id = 949; // name field in repeater
	$previous_value = FrmEntryMeta::get_entry_meta_by_field($entry_id, $field_id);
	$display = "Previous value " . $previous_value;
	console_log ($display);	
	if ( isset( $values['item_meta'][ $field_id ] ) && $values['item_meta'][ $field_id ] == $previous_value ) {
		// Stop email action if the repeater name field value did not change
		add_action( 'frm_trigger_email_action', 'frm_stop_email', 1 );
		$display = "No change in value, stop email from sending.";
		console_log ($display);
	} else {
		$display = "Updated value is " . $values['item_meta'][ $field_id ] . ",  don't stop email from sending";
		console_log ($display);
	}
	return $values;
}

What I discovered is that this code runs twice - first for the parent entry, and then for the repeater entry.  It does work, but I am testing a simple case first (only one repeater row).  My goal is for it to work if there are multiple rows and send emails only for each row that gets updated.  I'll try that next but I'm expecting it not to work that way because it appears that whatever the last result is on the comparison of previous and current values is the one that the action is set by.  I attached a screenshot of what was written to the console when I updated the name in the repeater entry.  Entry ID 363 is the parent entry (where the field values are empty because the field is not in that form), and Entry ID 362 is the repeater entry.

Attachments
Jane OnoratiJane Onorati replied 3 weeks ago

Ok, I tested this with two rows and as expected, it did not work the way I intended. I tested updating one of the repeater rows at a time and no emails were sent, but I did want an email for the one I updated to be sent. I had to update both rows to get emails to send, and one was sent for each row. That's normal behavior for a repeater email, but I want an email sent only to the email addresses in my repeater rows (entries) if a specific field value in each has been updated. Maybe this is just not possible?

Victor Font replied 3 weeks ago

Keep something in mind, each repeater row is an entry in its own right. if you have 10 repeaters rows, this will fire for every single entry and the parent. If you really want to limit the code, use the $_POST variable to get the repeater id and exit the function if it's not processing the repeater.

Jane OnoratiJane Onorati replied 3 weeks ago

This sounds like the fix, but the function has entry_id as an argument so I don't need to use $_POST, do I? Then, wouldn't I need a way to identify the entry_id as a repeater entry and not the parent entry? How can I do that?

Victor Font replied 3 weeks ago

I would use $_POST to get the repeater form ID is use it if (form_id = 'repeater') { do this } else exit function.

Jane OnoratiJane Onorati answered 2 weeks ago

I gave up on the solution I was pursuing and came up with a different one that does not require PHP.   In a repeater in my form, there is a field for an email address, and a Yes/No radio field called Send email?.  In settings there is an email action that is set to "Run this action for" the repeater instead of the main form.  I want to be able to conditionally trigger the email action for select rows in the repeater on entry update.  Then by selecting "Yes" for the Send email? field on one or more rows, the email should be sent to only the email addresses in those rows. My solution:

  1. Add a new field to the repeater to contain a copy of the email address, but make it conditionally display based on the value of the Send email? field.  Hide it from users by making it visible only to administrators.
  2. Change the value of the TO: field in the email action to be populated by this new conditional field.
  3. Add an API action to reset the Send email? field values back to "No" on update. I based this action on this example: https://formidableforms.com/knowledgebase/formidable-api/#kb-update-a-row-in-a-repeater

Now I can conditionally send emails to addresses in my repeater and automatically reset the trigger field in the repeater after each use.

Victor Font replied 2 weeks ago

That's a nice solution Jane. Good work!

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