I have to add all detail in table format in notification

niveditakumari
Mega Sage

Hi, 

 

I have to add all detail in table format in notification. When request is submitted for 'ABC' then it should send notification to group and it contain all detail in table format which is available in request submitted. 

When request is submitted it is storing request value in table('Visitor') parent and child table('Visitor Registration') and I want to take value from both table and need to send in notification. When email sent to user then it contain two table format in one table it contain value stored in Visitor table for submitted request and in other table it contain value for Visitor registration table for request submitted. 

 

Please see attached screenshot : 

   
    var visitGR = new GlideRecord('sn_wsd_visitor_visit');
    visitGR.addEncodedQuery('sys_id', current.sys_id);
    visitGR.query();
    while (visitGR.next()) {
        //gs.info('Visit Record: ' + visitGR.getDisplayValue());
        var short_desc = visitGR.short_description;
        var visitor_loc = visitGR.location;
        var host = visitGR.host;
    }
 
    // Fetch Visitor Registration Record
    var registrationGR = new GlideRecord('sn_wsd_visitor_visitor_registration');
    registrationGR.addEncodedQuery('source_visit', current.sys_id);
    registrationGR.query();
    while (registrationGR.next()) {
        //gs.log('Registration Record: ' + registrationGR.getDisplayValue() + registrationGR.co_host  + ' ' + registrationGR.first_name  );
        var visitor_external = registrationGR.visitor_external;
        var visitor_type = registrationGR.visitor_type;
        var first_name = registrationGR.first_name;
        var last_name = registrationGR.last_name;
        var company = registrationGR.company;
    }
    if(registrationGR.company != '') {

        var internal_user = registrationGR.first_name + registrationGR.last_name;
    } else if(registrationGR.company == ''){

        var external_user = registrationGR.first_name + registrationGR.last_name;
    }

  niveditakumari_0-1754219567524.png

 


   
Can anyone please help me with that. 
 
Regards, 
Nivedita
2 REPLIES 2

Nilesh Pol
Tera Guru

@niveditakumari 

You’ll write the email body using Scripted HTML and GlideRecord queries (like in your code).

But as per best practice you can use Mail Script for better reusability:

Use below code in you mail script: name - visitor_details_mail_script

(function runMailScript(current, template, email, email_action, event) {
var html = '';

html += '<h2>Visitor Request Summary</h2>';
html += '<table border="1"><tr><th>Short Description</th><th>Location</th><th>Host</th></tr>';
html += '<tr><td>' + current.short_description + '</td>';
html += '<td>' + current.location.getDisplayValue() + '</td>';
html += '<td>' + current.host.getDisplayValue() + '</td></tr></table>';

html += '<h2>Visitor Registrations</h2>';
html += '<table border="1"><tr><th>First Name</th><th>Last Name</th><th>Company</th></tr>';

var reg = new GlideRecord('sn_wsd_visitor_visitor_registration');
reg.addQuery('source_visit', current.sys_id);
reg.query();
while (reg.next()) {
html += '<tr><td>' + reg.first_name + '</td><td>' + reg.last_name + '</td><td>' + reg.company + '</td></tr>';
}

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

 

and then call above in you notification body:

${mail_script:visitor_details_mail_script}

 

if this works then plz mark helpful and access as a solution

kaushal_snow
Mega Sage

Hi @niveditakumari ,

 

Good day !!

 

Please create notification email script and attach that to your notification...

 

Name your script as - visitor_details_table

 

(function runMailScript(current, template, email, email_action, event) {
template.print("<h2>Visitor Request Summary</h2>");
template.print("<table border='1' cellpadding='4' cellspacing='0' style='border-collapse:collapse; width:100%;'>");
template.print("<tr><th>Short Description</th><th>Location</th><th>Host</th></tr>");
template.print("<tr><td>" + (current.short_description || "") + "</td>");
template.print("<td>" + (current.location.getDisplayValue() || "") + "</td>");
template.print("<td>" + (current.host.getDisplayValue() || "") + "</td></tr>");
template.print("</table>");

template.print("<h2>Visitor Registration Details</h2>");
template.print("<table border='1' cellpadding='4' cellspacing='0' style='border-collapse:collapse; width:100%;'>");
template.print("<tr><th>First Name</th><th>Last Name</th><th>Company</th><th>External?</th></tr>");

var reg = new GlideRecord("sn_wsd_visitor_visitor_registration");
reg.addQuery("source_visit", current.sys_id);
reg.query();
while (reg.next()) {
template.print("<tr>");
template.print("<td>" + reg.first_name.getDisplayValue() + "</td>");
template.print("<td>" + reg.last_name.getDisplayValue() + "</td>");
template.print("<td>" + reg.company.getDisplayValue() + "</td>");
template.print("<td>" + (reg.visitor_external ? "Yes" : "No") + "</td>");
template.print("</tr>");
}
template.print("</table>");
})(current, template, email, email_action, event);

 

Go to your notification as paste this script [ ${mail_script:visitor_details_table} ] wherever you want your table details to be shown...

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/