Need help with email script for populating assigned to value

Happy S
Tera Expert

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);

 

_________________________________________________________________________________

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

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

View solution in original post

3 REPLIES 3

Mohith Devatte
Tera Sage
Tera Sage

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

Aman Kumar S
Kilo Patron

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);

 

 

 

Best Regards
Aman Kumar

Jaspal Singh
Mega Patron
Mega Patron

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();