How to display multiple task details in notification body

Karishma Dubey
Tera Expert

Hi,

I have a requirement to display task(for ex sc_task) short desc and assigned to in email body for custom application. There will be two task. Need to display both(task1 & task2)  short desc and assigned to if it is closed complete state. Notification will be on parent table(for ex sc_req_item). Is it achievable from email script?

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Karishma Dubey 

you will have to use email script for this as notification is on RITM table

something like this in email script to show in tabular format but please enhance

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    // Email script for sc_req_item notification - HTML table format

    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id); // Link to parent sc_req_item
    taskGR.addQuery('state', '3'); // 3 = Closed Complete (adjust if needed)
    taskGR.query();

    var output = '';
    output += '<table border="1" cellpadding="4" cellspacing="0" style="border-collapse:collapse;">';
    output += '<tr>';
    output += '<th>Task Short Description</th>';
    output += '<th>Assigned To</th>';
    output += '</tr>';

    var found = false;
    while (taskGR.next()) {
        found = true;
        output += '<tr>';
        output += '<td>' + taskGR.short_description + '</td>';
        output += '<td>' + (taskGR.assigned_to ? taskGR.assigned_to.getDisplayValue() : 'Not Assigned') + '</td>';
        output += '</tr>';
    }
    output += '</table>';

    if (!found) {
        template.print('No closed complete tasks found for this request item.');
    }

    template.print(output); // The table will be rendered in the email body

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

Without tabular format

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    // Email script for sc_req_item notification

    // Get all related sc_task records
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id); // Link to parent sc_req_item
    taskGR.addQuery('state', '3'); // 3 = Closed Complete (adjust if your state values are different)
    taskGR.query();

    var output = '';

    while (taskGR.next()) {
        output += '<b>Task:</b> ' + taskGR.short_description + '<br/>';
        output += '<b>Assigned To:</b> ' + (taskGR.assigned_to ? taskGR.assigned_to.getDisplayValue() : 'Not Assigned') + '<br/><br/>';
    }

    if (!output) {
        template.print('No closed complete tasks found for this request item.');
    }

    template.print(output); // This will be included in the email body


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

It will print like this

Task: Approve hardware request
Assigned To: John Smith

Task: Configure laptop
Assigned To: Jane Doe

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

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

View solution in original post

3 REPLIES 3

Hristo Ivanov
Kilo Sage

you can create a mail script that queries the records you need and use the mail script in your notification

how to return multiple values and call in notification?

Ankur Bawiskar
Tera Patron
Tera Patron

@Karishma Dubey 

you will have to use email script for this as notification is on RITM table

something like this in email script to show in tabular format but please enhance

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    // Email script for sc_req_item notification - HTML table format

    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id); // Link to parent sc_req_item
    taskGR.addQuery('state', '3'); // 3 = Closed Complete (adjust if needed)
    taskGR.query();

    var output = '';
    output += '<table border="1" cellpadding="4" cellspacing="0" style="border-collapse:collapse;">';
    output += '<tr>';
    output += '<th>Task Short Description</th>';
    output += '<th>Assigned To</th>';
    output += '</tr>';

    var found = false;
    while (taskGR.next()) {
        found = true;
        output += '<tr>';
        output += '<td>' + taskGR.short_description + '</td>';
        output += '<td>' + (taskGR.assigned_to ? taskGR.assigned_to.getDisplayValue() : 'Not Assigned') + '</td>';
        output += '</tr>';
    }
    output += '</table>';

    if (!found) {
        template.print('No closed complete tasks found for this request item.');
    }

    template.print(output); // The table will be rendered in the email body

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

Without tabular format

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    // Email script for sc_req_item notification

    // Get all related sc_task records
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('request_item', current.sys_id); // Link to parent sc_req_item
    taskGR.addQuery('state', '3'); // 3 = Closed Complete (adjust if your state values are different)
    taskGR.query();

    var output = '';

    while (taskGR.next()) {
        output += '<b>Task:</b> ' + taskGR.short_description + '<br/>';
        output += '<b>Assigned To:</b> ' + (taskGR.assigned_to ? taskGR.assigned_to.getDisplayValue() : 'Not Assigned') + '<br/><br/>';
    }

    if (!output) {
        template.print('No closed complete tasks found for this request item.');
    }

    template.print(output); // This will be included in the email body


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

It will print like this

Task: Approve hardware request
Assigned To: John Smith

Task: Configure laptop
Assigned To: Jane Doe

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

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