Show the content in a table format in notification

Rose17
Tera Contributor

Hi All,

The requirement is to show the below content in tabular format in notification-

Mobile Request

Submitted By: Abel Tuter on 2022-04-05 00:20:28 PDT

Who is this request for?:Abel Tuter

Requested by:Abel Tuter

Mobile Request: iPhone, Android

Additional Comments: test 

Expected output-

Mobile Request

Submitted By:Abel Tuter on 2022-04-05 00:20:28 PDT
Who is this request for?:Abel Tuter
Requested by:Abel Tuter
Mobile Request:iPhone, / Android
Additional Comments:test3

 

Below is the email script-

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template, /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action, /* Optional GlideRecord */ event) {
template.print('<table class="test-notif-summary" align="center">');

var ritm = new GlideRecord('sc_req_item');
ritm.get(current.sysapproval);

var catalogItem = ritm.cat_item.name;
var requestedFor = ritm.request.requested_for.getDisplayValue();
var submittedOn = ritm.request.sys_created_on.getDisplayValue();
var submittedBy = ritm.request.opened_by.getDisplayValue();
var picture = ritm.cat_item.picture.getDisplayValue();
var imgSrc = '';

if (JSUtil.notNil(picture)) {
imgSrc = picture + '?t=small';
} else {
imgSrc = 'test-default.png';
}

// Get Owned Variables for Requested Item and sort by Order.
var variableSection = '';

//TO remove false variabes
var ownvar = new GlideRecord('sc_item_option_mtom');
ownvar.addQuery('request_item.number', ritm.number);
ownvar.orderBy('sc_item_option.item_option_new.getDisplayValue()', '!=', 'Order Guide');
ownvar.addQuery('sc_item_option.value', '!=', '');
var removefalse=ownvar.addQuery('sc_item_option.value','!=',false); //added
removefalse.addOrCondition('sc_item_option.value','!=','false'); //added
ownvar.orderBy('sc_item_option.order');
ownvar.query();

while (ownvar.next()) {
var field = ownvar.sc_item_option.item_option_new;
var fieldValue = ownvar.sc_item_option.item_option_new.name;
var fieldapprover = ownvar.sc_item_option.item_option_new.u_visible_on_approvers;

if (fieldapprover == true && fieldValue != 'tande_card_type' ) {
variableSection = variableSection + '<p class="test-notif-summary-row-details-field"><span class="test-notif- bold">' + field.getDisplayValue() + ':</span><span>' +
ritm.variables[fieldValue].getDisplayValue() + '</span></p>';

}

}

if (ritm.cat_item.price > 0) {
variableSection = variableSection + '<b>Cost:</b> ' + ritm.cat_item.price;
}


template.print(
'<tr class="test-notif-summary-row">' +
'<td class="test-notif-summary-row-img"><img src="' + imgSrc + '" /></td>' +
'<td class="test-notif-summary-row-details">' +
'<p class="test-notif-summary-item">' + catalogItem + '</p>' +
'<p class="test-notif-summary-row-details-field"><span class="test-notif-bold">Submitted By:</span> <span>' + submittedBy + ' on ' + submittedOn + '</span></p>' +
variableSection +
'</td>' + 
'</tr>' 
);

template.print('</table>');
})(current, template, email, email_action, event);

Note- Variable 'variableSection' has field name and values of last 2 fields shown in the content. For reference, see line no 34 and 39 of the script.

Could someone help me to achieve this?

@Ankur Bawiskar Your help is highly appreciated.

Thanks in advance.

1 ACCEPTED SOLUTION

Hi,

I have provided the logic below to remove false values

aligning can be done from your side

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

    // Add your code here
    template.print("<table><tbody>");
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.sysapproval);
    ritm.query();
    if(ritm.next()){
        var variables = ritm.variables.getElements();
        for (var i=0;i<variables.length;i++) {
            var question = variables[i].getQuestion();
            var label = question.getLabel();
            var value = question.getDisplayValue();
            if(label != '' && value != 'false'){
                template.print("<tr>");
                template.print("<td>");
                template.print(label);
                template.print("</td>");
                template.print("<td>");
                template.print(value);
                template.print("</td>");
                template.print("</tr>");
            }
        }
    }
    template.print("</table></tbody>");

})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So what's the output of the script you shared?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This-

Submitted By: Abel Tuter on 2022-04-05 00:20:28 PDT

Who is this request for?:Abel Tuter

Requested by:Abel Tuter

Mobile Request: iPhone, Android

Additional Comments: test 

Rose17
Tera Contributor

The if condition(line 39), var field and fieldValue is storing the variable name and its value based on what user selects while submitting request. For eg- if user selects Iphone, Android(fieldValue) in Mobile Request(field), then accordingly it is stored in var variableSection. And at the end of the script variableSection value is printed.

So can we do something like that to iterate field and fieldValue using collection and store it in some variable and print it at the end?

Could you help me to show the content in tabular format?

Hi,

I have used this sample script earlier; enhance it as per your requirement

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

	// Add your code here
	template.print("<table><tbody>");
	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('sys_id', current.sysapproval);
	ritm.query();
	if(ritm.next()){
		var variables = ritm.variables.getElements(); 
		for (var i=0;i<variables.length;i++) { 
			var question = variables[i].getQuestion();
			var label = question.getLabel();
			var value = question.getDisplayValue();
			if(label != ''){
				template.print("<tr>");
				template.print("<td>");
				template.print(label);
				template.print("</td>");
				template.print("<td>");
				template.print(value);
				template.print("</td>");
				template.print("</tr>");
			}
		} 
	}
	template.print("</table></tbody>");

})(current, template, email, email_action, event);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader