Email bodies with conditional content

Uncle Rob
Kilo Patron

So I have a legal team which has all kinds of conditional text they want to send depending on conditions of submission.

That is...

if you submit with X checked, include messagetext1 in email body

If you submit with Y checked, include messagetext2 in email body

If you submit with Z checked, include messagetext3 in email body

... and so on.

I want to handle this with one notification if I can.   I know I can call an email script to do all the logic evaluation, but its not a nice way to work with text (would prefer Rich HTML editor).   So my question is, can a mail   script call an email template that acts as a *component* of a body, rather than the entire email?

7 REPLIES 7

Uncle Rob
Kilo Patron

The idea is something like this:  



Notification:


Subject:   Thanks for requesting legal intervention


Body:


bla bla bla


{mail_script:body_builder}



Email Script (body_builder)


if current.checkbox1 = true


  template.print(template1)


if current.checkbox2 = true


template.print(teplate2)


... and so on.


Definitely using scripts to drive the logic.   The problem is the output of the script is (hopefully), rich HTML.   Since email script is a javascript editor, that makes it a little more clunky to work with formatted text.


jason_mckee
ServiceNow Employee
ServiceNow Employee

Just spitballing here, but what if you created a Template Component table to manage your HTML fragments in like:




Component - Choice (checkbox1,checkbox2...etc.)


Fragment - HTML



then have your email body_builder script along the flavor of:


var checkboxes = (checkbox1 ? 'checkbox1' : '') + ',' + (checkbox2 ? 'checkbox2' : '')


var gr = new GlideRecord('x_template_components');


gr.addQuery('component','IN',checkboxes);


gr.orderBy('component');


gr.query();


while(gr.next()) {


      template.print(gr.fragment);


}



Then you get a rich HTML editor for your fragments, but it's still all data driven.   I'd got a bit further and add in a reference to which email/template the fragments are associated with and maybe an Order field so you can have multiple content fragments associated with the same checkbox.   I've seen a similar structure used in contract management applications to generate contract boilerplate for different use cases.



Shades of the disaster recovery Task templates we did in ApocalypseNow.