- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:20 AM
Hi,
As per subject I am trying to create an email script for populating assigned to value for a sc task form.
The email template that I have is using the Requested Item table..
I have the below script but its not working..
Appreciate the help and thank you in advance..
_________________________________________________________________________________
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// Retrieve the assigned_to from the last closed sc_task related to the current sc_req_item
var assignedToName = '';
var scTasks = new GlideRecord('sc_task');
scTasks.addQuery('request_item', current.sys_id); // Use the correct reference field name here
scTasks.addQuery('state', 3); // Closed state
scTasks.orderByDesc('sys_updated_on'); // Order by update time in descending order
scTasks.setLimit(1);
scTasks.query();
if (scTasks.next()) {
var assignedToUser = new GlideRecord('sys_user');
if (assignedToUser.get(scTasks.assigned_to)) {
assignedToName = assignedToUser.name.getDisplayValue();
}
}
// Print the assigned_to name in the email template
template.print(assignedToName);
})(current, template, email, email_action, event);
_________________________________________________________________________________
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:28 AM - edited 08-17-2023 01:33 AM
Hello @Happy S
can you try using the below script
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// Retrieve the assigned_to from the last closed sc_task related to the current sc_req_item
var assignedToName = '';
var scTasks = new GlideRecord('sc_task');
scTasks.orderByDesc('sys_updated_on'); // Order by update time in descending order
scTasks.addQuery('request_item', current.getUniqueValue()); // Use the correct reference field name here
scTasks.addQuery('state', 3); // Closed state
scTasks.setLimit(1);
scTasks.query();
if (scTasks.next()) {
var assignedToUser = new GlideRecord('sys_user');
if (assignedToUser.get(scTasks.assigned_to.toString())) {
assignedToName = assignedToUser.name.getDisplayValue();
}
}
// Print the assigned_to name in the email template
template.print(assignedToName);
})(current, template, email, email_action, event);
Hope this helps
Mark my answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:28 AM - edited 08-17-2023 01:33 AM
Hello @Happy S
can you try using the below script
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// Retrieve the assigned_to from the last closed sc_task related to the current sc_req_item
var assignedToName = '';
var scTasks = new GlideRecord('sc_task');
scTasks.orderByDesc('sys_updated_on'); // Order by update time in descending order
scTasks.addQuery('request_item', current.getUniqueValue()); // Use the correct reference field name here
scTasks.addQuery('state', 3); // Closed state
scTasks.setLimit(1);
scTasks.query();
if (scTasks.next()) {
var assignedToUser = new GlideRecord('sys_user');
if (assignedToUser.get(scTasks.assigned_to.toString())) {
assignedToName = assignedToUser.name.getDisplayValue();
}
}
// Print the assigned_to name in the email template
template.print(assignedToName);
})(current, template, email, email_action, event);
Hope this helps
Mark my answer correct if this helps you
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:30 AM - edited 08-17-2023 01:35 AM
Assumption is your notification is on RITM table
Try below:
(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
// Retrieve the assigned_to from the last closed sc_task related to the current sc_req_item
var assignedToName = '';
var scTasks = new GlideRecord('sc_task');
scTasks.addQuery('request_item', current.getUniqueValue()); // Use the correct reference field name here
scTasks.addQuery('state', 3); // Closed state
scTasks.orderByDesc('sys_updated_on'); // Order by update time in descending order
scTasks.setLimit(1);
scTasks.query();
if (scTasks.next()) {
assignedToName = scTasks.assigned_to.name.toString();
}
// Print the assigned_to name in the email template
template.print(assignedToName);
})(current, template, email, email_action, event);
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2023 01:33 AM
Hi,
Everything looks good except for template.print(assignedToName); which is trying to get value for assignedToName which is inside loop.
Either defined the var assignedToUser
outside loop or include the
template.print(assignedToName);
just after
assignedToName = assignedToUser.name.getDisplayValue();