Email templates containing record fields

Viewing 9 reply threads
  • Author
    Posts
  • September 19, 2019 at 11:50 AM #36810

    john cesta
    Participant

    I need to send emails to members. I have a form which contains various pieces of text I routinely send mostly when there is no phone number associated with a members information.

    I’d like to include fields from the record in the email like…

    Dear [firstName]

    Your address is [city] [state] [zip]

    Well, you get the idea.

    Is there a way to do this in tapforms? Perhaps thru a calculation field or a script?

    Thanks,

    September 21, 2019 at 7:33 AM #36816

    Brendan
    Keymaster

    You can do this on the Mac version by using the Print function and click the PDF button then click the Mail PDF option. By putting your field names within the square brackets as you’ve done in your example, Tap Forms will substitute the value into the field when printed. You can attach the PDF to an email this way.

    You do this with a custom layout by the way. Add a static text object to your custom layout, stylize it, and then print it. The value substitution happens at print time. Either to paper or to PDF.

    Thanks!

    Brendan

    September 21, 2019 at 10:36 AM #36819

    Daniel Leu
    Participant

    Please have a look at https://www.tapforms.com/forums/topic/creating-emails-from-tapforms/ to send emails with content captured from your record.

    September 21, 2019 at 12:11 PM #36823

    john cesta
    Participant

    I looked Daniel and responded thanks.

    Brendan is determined to make me buy a Mac. I just bought my daughter a Mac and myself an iPad Pro. So I have to wait.

    If it wasn’t too much trouble for tapforms I think some would be willing to pay more (I would) for an iPad versus in that contained even some of the Mac features like calendar and or the pdf fields feature.

    I know it could be a lot of work porting it but what do I know?

    Is there a way, perhaps with a script, to set values on multiple fields at once. After I mail to a few hundred people I’d like to set a field with the name of the mailing and date.

    I use a child form to store the mailing content now.

    September 21, 2019 at 1:40 PM #36825

    Sam Moffatt
    Participant

    The challenge with porting some of that stuff over to iOS is whilst it makes sense for iPad it isn’t as useful on iPhone. That means it’s something useful for only half of the iOS market now that iOS apps are on both form factors. Perhaps an extra unlock for that as a feature so that those who only use an iPhone don’t have to pay for a feature they can’t use. That then leads to complications in the UI around enabling and disabling functionality based on the purchases which has their own challenges (and the inevitable restore purchases button). There’s also the other challenge that there is only one Brendan.

    I think you can use a calculation field to create the text you want and if you’re not doing a whole bunch of them at once, you can use the process Daniel Leu suggested as well. If you look at the bottom of the Calculations documentation, there is an example of formatting an address using a calculation field. You could use that as the basis of building out a fully formatted body. Alternatively you could use a script to build the template as well for creating the email link. Using a script, you’d put your template inside of the script and you can use variables inside of it.

    You can use a script to set multiple field values, setFieldValue or setFieldValues from the JS API can help you out there. Once you set a value, you also need to save those changes with document.saveAllChanges() otherwise they might not persist.

    On my iPad I did a quick test to build a form with a standard address layout and then made a script field to populate a “Web Site” field for me. The script looks like this:

    var name = record.getFieldValue('fld-f3d753238cf4457cb2ccd5c0eaa96b0f');
    var email = record.getFieldValue('fld-b1b7229002144634ae174c861b78ae72');
    var address_1 = record.getFieldValue('fld-ba8218dbc15741428af68f3e80c29fea');
    var address_2 = record.getFieldValue('fld-d11947b80b7a4592aa1815588275ef8e');
    var city = record.getFieldValue('fld-c22af0fe9d1a4fd3a587d96a83b352e5');
    var postcode = record.getFieldValue('fld-10d06c4cd8564b5a87ed365c5a307740');
    var state = record.getFieldValue('fld-35f71f806348488e8f755be48b8ce2f2');
    var country = record.getFieldValue('fld-0b6fe598592c428bab1010755d4bfa89');
    var text = record.getFieldValue('fld-8b5d1c927eca4daa89c7750848bd02c2');
    
    var addressLines = [];
    name ? addressLines.push(name) : null;
    address_1 ? addressLines.push(address_1) : null;
    address_2 ? addressLines.push(address_2) : null;
    addressLines.push(`${city} ${state} ${postcode}`);
    country ? addressLines.push(country) : null;
    var address = addressLines.join('\n');
    
    var subject = encodeURIComponent(`Hello ${name}! We missed you!`);
    var body = encodeURIComponent(`Hello ${name},
    
    We haven't seen you for a while and we wanted to 
    reach out to you. We don't  have a phone number 
    on file for you but we do have your address:
    ${address}
    
    If this isn't correct, please let us know so we 
    can keep in touch. 
    
    Thanks in advance!
    
    Your Friends`);
    
    var linkText = `mailto:${email}?subject=${subject}&body=${body}`;
    var send_email_template_id = 'fld-f03ed31415304eab814d9f137db2f598';
    record.setFieldValue(send_email_template_id, linkText);
    document.saveAllChanges();
    
    • The first chunk of the script gets all of the fields I care about via getFieldValue, that’s relatively straight forward.
    • The next section does a simple address composition as an example of how empty/unset fields could be handled (it’s not perfect, I took a shortcut on the City/State/Postcode line). It uses the Javascript ternary operators to determine if it should add to an array and then does a join on what was added to the array.
    • The next piece uses the Javascript backtick string interpolation feature to template the values out. It’s similar to the bracket format above except it uses a dollar sign and curly braces to wrap the variable. These values map to variables defined within the script itself. The first line is easy, it’s just the subject line. The second one shows how you can turn that into a multiline string to splice in values. Of note here I use encodeURIComponent to escape the text so that if you put in a special character it doesn’t munge the email.
    • The last few lines compose the link text that makes the email. This is essentially the process Daniel Leu suggested though not encapsulated as well as he has done.

    As you change the referenced fields, the web site field will update with the new values and then you can just tap on the globe icon and Tap Forms (after it asks “are you sure?”) will spin up Mail on the iPad prefilled with your email.

    Edit: Forgot that the backticks are special on Brendan’s forum too. I used an online HTML entity encoder/decoder to fix it up.

    Edit: I wrapped some of the lines in the message to better fit in the forum. Where you put line breaks in the script is where they will show up in the email. I’d generally want to manually wrap lines when doing plain text emails at less than 70 columns but that’s just me.

    September 21, 2019 at 3:18 PM #36828

    john cesta
    Participant

    Thanks Sam. I’ve started with just getting and setting one field. What I have is a form called written biz and want to send the client an email for each phase of the policy. In the email I put the policy number, their name, and a few other fields in the record. So what’s ever In the record will go when I send the email.

    It works the same for every one and every policy. So while the calculation may be too weak
    Th script would certainly do it.

    I understand the complexity of a partial feature set. I didn’t think of the iPhone aspect though.

    John

    September 21, 2019 at 3:28 PM #36829

    john cesta
    Participant

    It looks like I could make a script as a field type and the result would be the email body. What’s ever in the fields within the script would be available for me to simply copy into an email.

    I think that’s possible.

    September 21, 2019 at 4:29 PM #36830

    Sam Moffatt
    Participant

    You might want to use a form script though depending on what you are doing. The way I think of things is:

    • script fields update when the fields they refer to update and change their value based on that. They are useful for setting their own static value or even setting other fields. I use script fields to do stuff like take a notes field, parse it and then fill in other fields in a form. Script fields for me are the sort of thing that you should be able to run multiple times and they should have roughly the same output. Script fields are a little magical which is good and bad.*
    • form scripts I use for instances where I need a manual interaction OR the side effect of the script is in a sense variable. I use these for doing quick data entry, creating new records based on existing data or to automate mistake fixing.* The other use case is for building function libraries which allows re-usable code.

    In your case I think I’d actually model this as form scripts, create a form for logging and then when you generate your emails you put them in the log. In the log form you have the weblink to create the email and use that to send the email based on the log. That way you have a record of what you generated and when you sent it from within Tap Forms and you don’t have to go to your email to see what you sent.

    Then in terms of automating this I’d look to use form scripts with the prompter to ask for input. You could actually build a full state machine in the scripting engine and then use the log form mentioned above to be your audit trail as you go through everything. You could do some of this with script fields that watch and update a state field somewhere (and a previous state field as well) but in this case it sounds like you a form script with explicit intent is better than implicit state management.

    * When I say script fields are magical and I use form scripts for mistake fixing…I have a set of script fields that automatically propagate changes for like shipping dates into or from other records. Doing that means that I can’t simply change those fields because it detects those changes, runs it’s validation and then tries to undo the changes I just made because it makes the state inconsistent. To work around this, I have a few form scripts that reset these fields in a way that disables the script field updates so that I can change everything without my own automation undoing the changes.

    September 21, 2019 at 8:29 PM #36831

    Daniel Leu
    Participant

    I really should learn Javascript… Thank you, Sam, for showing and explaining how to make the code more elegant and robust!

    September 22, 2019 at 9:26 AM #36834

    Sam Moffatt
    Participant

    Thanks @daniel_leu! It’s picking up on what you did and extending it! I only learned about the string interpolation/string template feature recently when I picked Javascript up again with Tap Forms. I was looking for a better way of building larger templated strings because I figured there had to be something by now in Javascript and turns out there was! ES6 brought the language a long way.

Viewing 9 reply threads

You must be logged in to reply to this topic.