How to display MRVS table in Approval Notification Email

Si Min
Giga Expert

Hi All, 

Would like to seek some help here as I'm not able to display the MRVS table in the Approval email notification but those variables that outside the MRVS do display accordingly. 

Here is the code, and if someone can point me out where goes wrong? 

Thanks in advance. 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	template.print("Summary of Requested items:<br />");
	
	var set = new GlideappVariablePoolQuestionSet();
	
	set.setRequestID(current.getValue('sysapproval'));
	set.load();
	
	var vs = set.getFlatQuestions();
	
	for (var i = 0; i < vs.size(); i++) {
		
		if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='' && vs.get(i).getDisplayValue()!='false') {
			
			template.space(4);
			template.print(vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br/>");
			
		}
		var mrvs;
		var rowCount;
		var ritmGR = new GlideRecord('sc_req_item');
		if (ritmGR.get(current.sys_id)) {
			mrvs = ritmGR.variables.book_details; //multi row variable set info
			rowCount = mrvs.getRowCount();
		
		if(rowCount >= 0){
			
			template.print('<table border="1px solid black" style="font-family:Arial">');
			template.print( "<tr bgcolor='#ddd'align='center'>" );
			template.print("<td style='text-align:center' colspan='10'><strong>Book Details</strong></td>");
			template.print( "</tr>" );
			
			template.print( "<tr>" );
			template.print( "<td><center><b>Type</b></center></td>" );
			template.print( "<td><center><b>Title </b></center></td>" );
			template.print( "<td><center><b>Subtitle</b></center></td>" );
			template.print( "<td><center><b>Author / Editor</b></center></td>" );
			template.print( "<td><center><b>Year of Published</b></center></td>" );
			template.print( "<td><center><b>International Standard Book Number</b></center></td>" );
			template.print( "<td><center><b>Publisher</b></center></td>" );
			template.print( "<td><center><b>URL</b></center></td>" );
			template.print( "<td><center><b>Estimated Price (SGD)</b></center></td>" );
			template.print( "</tr>" );
			
			var mvrs = current.variables.book_details;
			var ritm = current.getUniqueValue();
			var rowCount2 = mvrs.getRowCount();
			for (var a = 0; a < rowCount2; a++) {
				var row = mvrs.getRow(a);
				template.print( "<tr>" );
				template.print( "<td><center>" +row.type + "</center></td>" );
				template.print( "<td><left>" +row.title + "</left></td>" );
				template.print( "<td><left>" +row.subtitle + "</left></td>" );
				template.print( "<td><left>" +row.author_editor + "</left></td>" );
				template.print( "<td><center>" +row.year_of_published + "</center></td>" );
				template.print( "<td><left>" +row.international_standard_book_number_isbn + "</left></td>" );
				template.print( "<td><left>" +row.publisher + "</left></td>" );
				template.print( "<td><left>" +row.url + "</left></td>" );
				template.print( "<td><center>" +row.estimated_price_sgd + "</center></td>" );
				template.print( "</tr>" );
			}
			template.print('</table>');
			
		}}})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

instead of using current.sys_id , replace it with current.sysapproval

try now. 

View solution in original post

33 REPLIES 33

Harsh Vardhan
Giga Patron

script looks fine. may be its not going inside some condition block, can you add some logs ?

 

 Sample Code. : try to add some more logs this way you can troubleshoot it. 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	template.print("Summary of Requested items:<br />");
	
	var set = new GlideappVariablePoolQuestionSet();
	
	set.setRequestID(current.getValue('sysapproval'));
	set.load();
	
	var vs = set.getFlatQuestions();
	
	for (var i = 0; i < vs.size(); i++) {
		
		if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='' && vs.get(i).getDisplayValue()!='false') {
			
			template.space(4);
			template.print(vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br/>");
			
		}
		var mrvs;
		var rowCount;
		var ritmGR = new GlideRecord('sc_req_item');
		if (ritmGR.get(current.sys_id)) {
			mrvs = ritmGR.variables.book_details; //multi row variable set info
			rowCount = mrvs.getRowCount();
		
		if(rowCount >= 0){
			
			template.print('<table border="1px solid black" style="font-family:Arial">');
			template.print( "<tr bgcolor='#ddd'align='center'>" );
			template.print("<td style='text-align:center' colspan='10'><strong>Book Details</strong></td>");
			template.print( "</tr>" );
			
			template.print( "<tr>" );
			template.print( "<td><center><b>Type</b></center></td>" );
			template.print( "<td><center><b>Title </b></center></td>" );
			template.print( "<td><center><b>Subtitle</b></center></td>" );
			template.print( "<td><center><b>Author / Editor</b></center></td>" );
			template.print( "<td><center><b>Year of Published</b></center></td>" );
			template.print( "<td><center><b>International Standard Book Number</b></center></td>" );
			template.print( "<td><center><b>Publisher</b></center></td>" );
			template.print( "<td><center><b>URL</b></center></td>" );
			template.print( "<td><center><b>Estimated Price (SGD)</b></center></td>" );
			template.print( "</tr>" );
			
			var mvrs = current.variables.book_details;
			var ritm = current.getUniqueValue();
			var rowCount2 = mvrs.getRowCount();
			for (var a = 0; a < rowCount2; a++) {
				var row = mvrs.getRow(a);

gs.log('Row Value is '+ row.type);
				template.print( "<tr>" );
				template.print( "<td><center>" +row.type + "</center></td>" );
				template.print( "<td><left>" +row.title + "</left></td>" );
				template.print( "<td><left>" +row.subtitle + "</left></td>" );
				template.print( "<td><left>" +row.author_editor + "</left></td>" );
				template.print( "<td><center>" +row.year_of_published + "</center></td>" );
				template.print( "<td><left>" +row.international_standard_book_number_isbn + "</left></td>" );
				template.print( "<td><left>" +row.publisher + "</left></td>" );
				template.print( "<td><left>" +row.url + "</left></td>" );
				template.print( "<td><center>" +row.estimated_price_sgd + "</center></td>" );
				template.print( "</tr>" );
			}
			template.print('</table>');
			
		}}})(current, template, email, email_action, event);

Varsha21
Giga Guru

Hi

len=mrvs.getRowCount();

if(len>0){

for(i=0;i<len;i++)

gs.print(mrvs[i].your_variable_name);//use like line you ll get expected result 

}

 

OR 

Changes in your code

 

template.print( "<td><center>" +row[a].type + "</center></td>" ); template.print( "<td><left>" +row[a].title + "</left></td>" ); template.print( "<td><left>" +row[a].subtitle + "</left></td>" ); template.print( "<td><left>" +row[a].author_editor + "</left></td>" ); template.print( "<td><center>" +row.year_of_published + "</center></td>" ); template.print( "<td><left>" +row[a].international_standard_book_number_isbn + "</left></td>" ); template.print( "<td><left>" +row[a].publisher + "</left></td>" ); template.print( "<td><left>" +row[a].url + "</left></td>" ); template.print( "<td><center>" +row[a].estimated_price_sgd + "</center></td>" ); template.print( "</tr>" );

 

Thanks

Hi Varsha, 

Where should I add your first part of code? 
And I did try to add the row[a] into my code but still not working. 

Not sure what went wrong. 

Thanks. 

try now. 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	
	template.print("Summary of Requested items:<br />");
	
	var set = new GlideappVariablePoolQuestionSet();
	
	set.setRequestID(current.getValue('sysapproval'));
	set.load();
	
	var vs = set.getFlatQuestions();
	
	for (var i = 0; i < vs.size(); i++) {
		
		if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue()!='' && vs.get(i).getDisplayValue()!='false') {
			
			template.space(4);
			template.print(vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br/>");
			
		}
		var mrvs;
		var rowCount;
		var ritmGR = new GlideRecord('sc_req_item');
		if (ritmGR.get(current.sys_id)) {
			mrvs = ritmGR.variables.book_details; //multi row variable set info
			rowCount = mrvs.getRowCount();
		
		if(rowCount >= 0){
			
			template.print('<table border="1px solid black" style="font-family:Arial">');
			template.print( "<tr bgcolor='#ddd'align='center'>" );
			template.print("<td style='text-align:center' colspan='10'><strong>Book Details</strong></td>");
			template.print( "</tr>" );
			
			template.print( "<tr>" );
			template.print( "<td><center><b>Type</b></center></td>" );
			template.print( "<td><center><b>Title </b></center></td>" );
			template.print( "<td><center><b>Subtitle</b></center></td>" );
			template.print( "<td><center><b>Author / Editor</b></center></td>" );
			template.print( "<td><center><b>Year of Published</b></center></td>" );
			template.print( "<td><center><b>International Standard Book Number</b></center></td>" );
			template.print( "<td><center><b>Publisher</b></center></td>" );
			template.print( "<td><center><b>URL</b></center></td>" );
			template.print( "<td><center><b>Estimated Price (SGD)</b></center></td>" );
			template.print( "</tr>" );
			
			var mvrs = current.variables.book_details;
			var obj = JSON.parse(mrvs);
			
			for (var i=0 ; i < obj.length; i++) {

				template.print( "<tr>" );
				template.print( "<td><center>" +obj[i].type + "</center></td>" );
				template.print( "<td><left>" +obj[i].title + "</left></td>" );
				template.print( "<td><left>" +obj[i].subtitle + "</left></td>" );
				template.print( "<td><left>" +obj[i].author_editor + "</left></td>" );
				template.print( "<td><center>" +obj[i].year_of_published + "</center></td>" );
				template.print( "<td><left>" +obj[i].international_standard_book_number_isbn + "</left></td>" );
				template.print( "<td><left>" +obj[i].publisher + "</left></td>" );
				template.print( "<td><left>" +obj[i].url + "</left></td>" );
				template.print( "<td><center>" +obj[i].estimated_price_sgd + "</center></td>" );
				template.print( "</tr>" );
			}
			template.print('</table>');
			
		}}})(current, template, email, email_action, event);