How do I script creating a table in a mail script?

Stacy1
Mega Guru

We would like to script the creation of a table in a mail script.  The reason I want to script it is I only want to add a row if there is a value for that particular variable.

The table should look something like this.  (If there is no Module then that row should not show up, etc.)

Thanks,

Stacy

Priority: current.priority
Product: current.product
Module: current.module
Activity: current.activity (if there was no data for this row I don't want this row to show)
Description: current.description
1 ACCEPTED SOLUTION

Stacy1
Mega Guru

I ended up not setting the font at all in my email script.

Instead I used a email layout and set the font there and called it in my email template.

Worked perfect.

View solution in original post

13 REPLIES 13

I tried that and my table does not print at all.

This really shouldn't be so hard. Not sure why it's not working.  

Thanks,

Stacy

Stacy1
Mega Guru

I ended up not setting the font at all in my email script.

Instead I used a email layout and set the font there and called it in my email template.

Worked perfect.

Hi! can you show me how did you do it? pleaseee

I can give you what I wrote.  My email also handled translations for Portuguese.  It also had clickable links that would open an email and populate the email with the info for accept/reject.  Basically you are going to create a variable for all the fields you might want to show.  Then you write conditions that if they aren't empty it will add the row to the table.  It's super lengthy so I hope you can modify it to what you need.

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    var language = "en";

    if (current.contact) {
        var usrPref = new GlideRecord('sys_user_preference');
        usrPref.addQuery('name', 'user.language');
        usrPref.addQuery('user', current.contact);
        usrPref.query();
        if (usrPref.next()) {
            language = usrPref.value;
        } else if (current.contact.preferred_language != ''){
            language = current.contact.preferred_language;
        }
		else {
			language = 'en';
		}
    }
    var type = current.u_case_type.getDisplayValue();
    email.setSubject(type + ' ' + current.number + ' - ' + transMessage('Proposed Solution', language) + ' - ' + current.short_description);

    var links = '';
    if (current.getValue('contact')) {
        links = (gs.getProperty('glide.servlet.uri') + 'csm?id=csm_ticket&table=sn_customerservice_case&sys_id=' + current.getUniqueValue() + '&view=csp');
    } else if (current.getValue('consumer')) {
        links = (gs.getProperty('glide.servlet.uri') + 'csp?id=csm_ticket&table=sn_customerservice_case&sys_id=' + current.getUniqueValue() + '&view=css');
    }
    //        var type = current.u_case_type.getDisplayValue();
    var priority = current.priority.getDisplayValue();
	var line = current.product.getDisplayValue();
    var prod = current.category.getDisplayValue();
    var mod = current.subcategory.getDisplayValue();
    var act = current.u_activity.getDisplayValue();
    var sub = current.short_description;
    var desc = current.description;
    var number = current.number;
	var solution = current.close_notes.toString();
	var state = current.state.getDisplayValue();

    var myhtml = "<html><body><table><tr>";
	myhtml = myhtml + "<tr><td><td valign='top'; width=17%><strong>" + transMessage('Proposed Solution', language) + ':' + "</strong></td><td><td width=83%>" + solution + "</td></tr>";
    myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Number', language) + ':' + "</strong></td><td><td width=83%>" + '<a href="' + links + '">' + current.number + '</a>' + "</td></tr>";
	myhtml = myhtml + "<tr><td><td valign='top'; width=17%;><strong>" + transMessage('Subject', language) + ':' + "</strong></td><td><td width=83%>" + sub + "</td></tr>";
	myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('State', language) + ':' + "</strong></td><td><td width=83%>" + state + "</td></tr>";
    if (priority) {
        myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Priority', language) + ':' + "</strong></td><td><td width=83%>" + priority + "</td></tr>";
    }
	if (line != '') {
        myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Product Line', language) + ':' + "</strong></td><td><td width=83%>" + line + "</td></tr>";
    }
    if (prod != '') {
        myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Product', language) + ':' + "</strong></td><td><td width=83%>" + prod + "</td></tr>";
    }
    if (mod != '') {
        myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Module', language) + ':' + "</strong></td><td><td width=83%>" + mod + "</td></tr>";
    }
    if (act != '') {
        myhtml = myhtml + "<tr><td><td width=17%><strong>" + transMessage('Activity', language) + ':' + "</strong></td><td><td width=83%>" + act + "</td></tr>";
    }
    if (desc != '') {
        myhtml = myhtml + "<tr><td><td valign='top'; width=17%;><strong>" + transMessage('Description', language) + ':' + "</strong></td><td><td width=83%>" + desc + "</td></tr>";
    }
    myhtml = myhtml + "</tr></table></body></html>";

    template.print('<p>' + transMessage('Hello', language) + ' ' + current.contact.first_name + ',' + '</p>');
    template.print('<p>' + transMessage('The following solution has been proposed for your', language) + ' ' + type + '.' + '</p>');
    template.print(myhtml);
    var asubjectTxt = encodeURI('Accept ' + current.number + ' - ' + current.short_description);
    var abodyTxt = encodeURI('I accept the proposed solution for' + ' ' + current.number + ' - ' + current.short_description + '\n\n\n\n\n' + 'DO NOT DELETE THE LINE BELOW' + '\n' + email.watermark);
    var rsubjectTxt = encodeURI('Reject ' + current.number + ' - ' + current.short_description);
    var rbodyTxt = encodeURI('I reject the proposed solution for' + ' ' + current.number + ' - ' + current.short_description + '.' + '\n\n' + 'Please provide reject reason below:' + '\n\n\n\n\n' + 'DO NOT DELETE THE LINE BELOW' + '\n' + email.watermark);

    template.print('<p>' + transMessage('To <b>accept</b> the proposed solution and close the', language) + ' ' + type + ' ' + '<a title="click here" href="mailto:instancename@service-now.com?SUBJECT=' + asubjectTxt + '&amp;body=' + abodyTxt + '">click here</a>' + '.' + '</p>');
    template.print('<p>' + transMessage('To <b>reject</b> the proposed solution and keep the', language) + ' ' + type + ' open ' + '<a title="click here" href="mailto:instancename@service-now.com?SUBJECT=' + rsubjectTxt + '&amp;body=' + rbodyTxt + '">click here</a>' + '.' + '</p>');

    template.print('<p>' + transMessage('Thank you', language) + ',' + '</p>');
    template.print('<p>' + transMessage('Application Management Services', language) + '</p>');


    function transMessage(key, lang) {
        var msg = new GlideRecord('sys_ui_message');
        msg.addQuery('language', lang);
        msg.addQuery('key', key);
        msg.query();
        if (msg.next()) {
            return msg.message;
        } else {
            return key;
        }
    }
})(current, template, email, email_action, event);