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
Sending Email Notification Only Once on Update from a Custom Display
So I spent a good deal of time putting this system for sending out an email notification only one time, triggered by an update of a form entry, from an AJAX link in a custom display.
We have a entry form where entrants submit their entry information, then they have to mail in their entries (DVDs, Blu-Rays, Screenplays, etc.) via physical mail, and make a payment via Credit Card, Check, or PayPal. So we wanted to send out an email notification automatically once we've received their entry in the mail, and have received their payment for the correct amount, and have verified that everything is correct with their entry. So it needs to be an email notification triggered by an update, not by creation, but the challenge is, we definitely don't want to send the notification every time we update their entry, because we update it a lot with lots of information we don't want them to see (like whether they're getting an award or not). So here's how I swung it. Bear with me.
functions.php Code
The first thing you have to do if you want to make email notifications work using an AJAX link in a custom display is you have to throw the following bit of code into your theme's functions.php page:
/* FORMIDABLE PRO EMAIL NOTIFICATION FROM CUSTOM FIELD UPDATE */
add_action('frm_after_update_field', 'frm_trigger_entry_update');
function frm_trigger_entry_update($atts){
$entry = FrmEntry::getOne($atts['entry_id']);
do_action('frm_after_update_entry', $entry->id, $entry->form_id);
}
Just throw that in as is, save, and you're good to go.
Setting Up the Notification Email
So for our situation, there are (among other things) four radio buttons in the admin section of our entry form: 1) Entry Correct, 2) Payment Received, 3) Payment Correct, 4) All Materials Received. Once all four of those radio buttons are marked "Yes," we have a conditional checkbox appear that gives the option to Send a notification of receipt. We also have a conditional checkbox appear that gives the option never to send a notification again. The values for these two separate checkboxes are "Send" and "Do Not Send Again," respectively.
So I set up an email notification that sends based on conditional logic:
Send if ALL (not ANY!) of these statements are true:
"Send Notification of Receipt" is equal to: "Send"
"Prevent Further Notifications of Receipt" is not_equal to: "Do Not Send Again"
Also VERY IMPORTANT: Set the email notification to: "Send this notification when entries are: updated". You don't want to send it when they're created. That will be confusing for them. And not when "created and updated." Make sure you set this just to "updated."
So with my two required conditional logic statements, and it set to send only on "updated," it only sends the email if the first checkbox is checked, and will never send the email if the second checkbox is checked. This notification sends them an email basically saying, "We got your stuff and we're looking at it. Be happy." (That's a loose paraphrase.)
Making It User Friendly
But I didn't want the admins to have to go into the entry form editor, check the first checkbox as "Send," scroll down, hit submit, go back into the entry form editor, check the second checkbox as "Do Not Send Again," scroll down, hit submit. That's a pain. So I decided to have these two fields updated via Ajax on our "Manage Entries" custom display.
So the first thing I wanted to do was to hide these two fields in the entry form editor itself. It's just not user friendly to do this in the entry form editor, so why give admins the option at all? I think there's an easier way to do this, but all I did was go into Settings / Customize HTML, and I scrolled down to those two checkbox fields, and wrapped each of them with <div style="display:none;"> ... </div>. That way, they don't show up at all in the entry form editor.
Next thing is the fun part. Our "Manage Entries" custom display is set up to display a list of all entries, and links to individual entries. So we see a list the entry ID, the Title of the Entry, and options to View, Edit, or Delete each entry. This is in a table. I didn't want to put the AJAX links to send the email notification in the details page on each individual entry. To make it super easy, I wanted to put those links right there on the main part of the custom display, the table list of all entries.
So I decided to put the links in the same <td> where the entry Titles are. [100] is the field id for our Titles. Here's the whole code:
The Custom Display Code
<td>[100]
<font style="font-size:10px;">
[if 657 equals="Send"]<br>Notification of receipt has been sent.[/if 657]
[if 230 equals="Yes"][if 645 equals="Yes"][if 233 equals="Yes"][if 260 equals="Yes"]<br>
field_id=657 value="Send" label="Send Notification of Receipt |"] field_id=658 value="Do Not Send Again" label="Don't Send Again"]
[/if 260][/if 233][/if 645][/if 230]</font></td>
All right, now to decipher. I'll break it up into parts:
1) [if 657 equals="Send"]<br>Notification of receipt has been sent.[/if 657]
657 is the field ID for the checkbox named "Send Notification of Receipt." If it is marked as "Send" then the custom display with put a line break directly after the title, and display the text, "Notification of receipt has been sent." If 657 does not equal "send," then this line break and text will not be displayed.
2) [if 230 equals="Yes"][if 645 equals="Yes"][if 233 equals="Yes"][if 260 equals="Yes"]<br>
See that I have nested conditionals here. Yes, they do work, thank heavens. 230 is our radio button named "Entry Correct." 645 is our radio button named "Payment Received." 233 is our radio button named "Payment Correct." And 260 is our radio button named "All Materials Received." If the value for each of these is "Yes," then we're going to see the AJAX link for the email notification of receipt and the AJAX link to send no further email notifications of receipt. If the value for any of these is not "Yes," then no AJAX links will be displayed. All four have to be "Yes," or the option to send the notification doesn't show up anywhere (not even in the form editor, 'cause I display:none'd it, remember?). So those nested conditionals are important. We don't want someone to send an email notification saying, "We've got your entry and your money," if we have their entry but not their money, or we have some of their money, but not enough, or whatever.
3) field_id=657 value="Send" label="Send Notification of Receipt |"] field_id=658 value="Do Not Send Again" label="Don't Send Again"]
All right. Here are the two shortcodes for the two AJAX links. Note that there is a space between the two shortcodes. If there weren't, well, that wouldn't look good, would it? Also notice that in the "label" value of the first shortcode, at the end of the label, I put a space and one of these: "|". That acts as a separator between the two links. It's cheap, but does the job.
Now, the first important thing in the shortcode is id=[id]. If you don't have the id param set to equal [id], it won't know what entry to update. Then 657 is the field_id for the send notification checkbox. We want to update the value to equal "Send" so we of course add value="Send" to the shortcode. Note, with a checkbox, radio button, or dropdown, the value you put in the shortcode has to match an actual value already present in the field options. Finally, we add the label: label="Send notification of receipt |". This is what the link will actually say.
The cool thing about these is Formidable already has it designed so that if the value of the field in the entry matches the value designated in the shortcode, the shortcode won't appear. So once you click the link, it disappears, never to return again for that specific entry. I have it set up so there's no way for an ordinary admin to uncheck that field, because, again, I hid the field in the entry form editor. (I can of course uncheck it using Firebug in Firefox, or whatever you use in your browser.)
Then the next AJAX link updates the checkbox field 658. The field is named, "Prevent Future Notifications of Receipt," or something like that, and this AJAX link gives it its value of "Do Not Send Again." Once the admin clicks on this link, it will disappear, and another notification of receipt email will never be sent to the entrant.
4) [/if 260][/if 233][/if 645][/if 230]
Then don't forget to close all of your nested conditionals in reverse order from the order you opened them. That's kind of important, you know.
Further Awesomeness
Now the really cool thing (and yes I tested this out thoroughly) is that as long as you click them in the right order, it doesn't matter how long in between you click each one. You can click "Send Notification of Receipt" and then "Don't Send Again" like half a second later (half of half a second even), and the email will still send, but won't ever send again. It's super fast. Just make sure you don't click "Don't Send Again" first. If you do that, clicking the first link, "Send Notification of Receipt," won't have any effect (i.e., the email will never send). So as long as you click them in the right order, you can try your damndest to do it so fast that you break the internet, but the notification system will win every time.
Now because I put these links on the main page of the custom display, in the list of all entries, all the admin has to do is go down the list, clicking on each link in the right order, as fast as they want, on every entry where the links appear. Once they're done clicking all the links, all the links will be gone, and all the emails will have been sent, and no one who was sent that email will ever receive it again, and that will be that.
Then if you refresh/reload the page, on all the entries where you just sent an email, you'll get the message, right there under the title, saying, "Notification of receipt has been sent."
Simple as that. Obviously, everyone's situation is different, but hopefully this gives you some inspiration to set up a system that works for you.
I hope I didn't leave anything important out, but check the comments just in case.
August 12, 2013 at 11:51 pm
I'm not sure about this, but when you post code in the visual tab here on this forum, it automatically changes straight quotes (single and double quotes) to smart quotes. So it may be that if you try to copy and paste these the code won't work. I could be wrong, just look out for that.
August 13, 2013 at 12:04 am
Thank you so much for your awesome posts! That typo has been corrected.
August 13, 2013 at 12:19 am
Rad. Thanks much for that.
August 18, 2013 at 10:53 am
Hi,
I've been trying to do something similar to worldfest, but I needed it to be a little simpler. My form is a registration form with an admin/editor only acceptance field that gets set later when the user is approved. When this is set to 'Yes', an email is sent to the user on update, using conditional logic. I don't want the email to be sent again when further updates to the form are made, eg to other fields - as this could be very out of context for the user.
As this is a user registration form, I'm not keen on worldfest's method of having several links to press to achieve this (even for the admins), so I've been plugging away trying to work out how to do this with frm_after_update_entry and I think I have a solution, using the code below.
Like worldfest, I have a second Yes/No field that marks whether the email should be sent or not - it defaults to 'Yes' on creation.
When the form is updated, I check if both fields are set to 'Yes' and then send the email using $frmpro_notification, which (a bit surprisingly) works here and the conditional logic is correct at this point - both fields at 'Yes'. After this the code updates 'send again' to 'No'. This works out well here as normally the email is sent after the update, but the conditions are no longer correct, so it doesn't get sent. (This is where I got stuck for a long time until I tried $frmpro_notification before the update).
This seems to work well, unless anyone can see any problems with this approach?
add_filter('frm_after_update_entry', 'set_dont_resend_accepted', 30, 2);
function set_dont_resend_accepted($entry_id, $form_id){
global $wpdb, $frmdb, $frmpro_notification;
if ($form_id == 99) {
$accepted = $_POST['item_meta'][111];
$send_accepted = $_POST['item_meta'][112];
if ($accepted == 'Yes' && $send_accepted == 'Yes') {
$frmpro_notification->entry_created($entry_id, $form_id);
$wpdb->update($frmdb->entry_metas, array('meta_value' => 'No'), array('field_id' => 112, 'item_id' => $entry_id));
}
}
}
September 6, 2013 at 9:54 am
micrograde's solution works well; however, it only worked on the regular form for me. It did not work for when using the "link to update field" shortcode: formidablepro.com/knowledgebase/insert-a-link-to-change-the-value-of-a-single-field/
Discussion closed.