The CreatorCon Call for Content is officially open! Get started here.

Show record's related list in Notification's body

DevYadav
Tera Contributor

Hi everyone,

I’m working on a requirement where I need to trigger a notification for an Incident record, and in the email body, I want to display not only the Incident fields but also the fields from the related Change Task records (which appear in the Incident’s related list).

Currently, the Incident fields are appearing correctly in the notification preview, but I’m unable to display the Change Task details. Based on my understanding, I may need to create a Notification Script to retrieve and format the related Change Task data, but I’m not sure:

  1. How exactly to write that Notification Script, and

  2. How to call or reference that script within the Notification so the data appears in the body during preview and when triggered.

Additionally, since an Incident can have multiple Change Tasks, I need the script to handle that scenario — ideally by listing all related Change Task numbers and their key details (e.g., short description, state) dynamically.

Can someone please guide me with the correct approach or example on how to achieve this?

Thanks in advance!

DevYadav_0-1760024325501.png

DevYadav_1-1760024346739.png

 

2 REPLIES 2

M Iftikhar
Giga Sage

Hi @DevYadav,

Here’s how you can proceed to implement the solution for displaying the related Change Task details in the notification:

  1. Create the Notification Email Script:

    • Navigate to System Notification > Notification Email Scripts.

    • Click New to create a new script.

    • Name the script something like related_change_requests.

    • Check the Newlines to HTML checkbox to preserve line breaks in your email.

    Add the following code to the script:

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

    var changeRequestGR = new GlideRecord('change_request');
    changeRequestGR.addQuery('parent', current.sys_id);
    changeRequestGR.query();
    if (changeRequestGR.hasNext()) {
        template.print("Related Change Requests: <br>");
        while (changeRequestGR.next()) {
            var changeRequestURL = changeRequestGR.getTableName() + ".do?sys_id=" + changeRequestGR.sys_id;
            template.print('<a href="' + changeRequestURL + '">' + changeRequestGR.number + '</a><br>');
            template.print('Short Description: ' + changeRequestGR.short_description + '<br>');
            template.print('State: ' + changeRequestGR.state.getDisplayValue() + '<br><br>');
        }
    }
})(current, template, email, email_action, event);

 

In the Email Body section of the notification, use the following code to include the script:

${mail_script:related_change_requests}

Notification Preview:

MIftikhar_0-1760028227556.png

 

If my response helped, please mark it as the accepted solution so others can benefit as well.
 
 

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

aruncr0122
Mega Guru

Hi @DevYadav ,

 

You can follow below steps :

Step 1: Create a Notification Email

Table: Incident [incident]

When to send: e.g., “State changes to Resolved” (your choice)

In the email body, include your usual Incident fields, such as:

Incident: ${number}
Short Description: ${short_description}


And where you want the Change Tasks to appear, add:

Related Change Tasks:
${mail_script:incident_change_tasks}

Step 2: Create the Mail Script

Go to:
System Notification → Email → Notification Email Scripts → New

Create a new script record:

Name: incident_change_tasks

Applies when: incident

Active:

Script:

(function() {
var result = '';
try {
// Get the current incident sys_id
var incSysId = current.sys_id.toString();

// Query related Change Tasks (task table where parent = current incident)
var grChangeTask = new GlideRecord('change_task');
grChangeTask.addQuery('parent', incSysId);
grChangeTask.query();

if (grChangeTask.hasNext()) {
result += '<table style="width:100%; border-collapse:collapse;" border="1">';
result += '<tr><th style="text-align:left; padding:4px;">Number</th>' +
'<th style="text-align:left; padding:4px;">Short Description</th>' +
'<th style="text-align:left; padding:4px;">State</th></tr>';

while (grChangeTask.next()) {
result += '<tr>' +
'<td style="padding:4px;">' + grChangeTask.getDisplayValue('number') + '</td>' +
'<td style="padding:4px;">' + (grChangeTask.short_description || '') + '</td>' +
'<td style="padding:4px;">' + grChangeTask.getDisplayValue('state') + '</td>' +
'</tr>';
}
result += '</table>';
} else {
result = 'No related Change Tasks found.';
}
} catch (e) {
gs.error('Error building Change Task list for incident ' + current.number + ': ' + e.message);
result = 'Error fetching related Change Tasks.';
}

// Return the generated HTML to the notification
template.print(result);
})();

Step 3: Reference the Script in Notification

In your email body (HTML or plain text):

<h3>Incident Details</h3>
<p>Number: ${number}</p>
<p>Short Description: ${short_description}</p>

<h3>Related Change Tasks</h3>
${mail_script:incident_change_tasks}