Dynamically Populate email subject with customer name

hanumesh
Tera Contributor

There are existing two email notification on the sc_task and SLA Table
1. sc task mail subject : Task ${number} has been assigned to your group

2. sla task subject : SLA "{sla}" associated with {task.number} has breached

These are two existing mail subjects, Now for this subject I need to Write an email script to dynamically populate subject with customer name along with task number if the catalog type is SSR/CSR. For others, it should remain as is,

to find the catalog item type we have an field in the ScTask called item which is highlighted in green color, If I click on the item , it will be re-directed to sc_cat-item, here we have an catalog type field,

And for the customer name, in the ritm we have an account variable that should be pulled.

Can anybody help me with the script for both of the table SLA & Sc_task And how to call the mail script dynamically.  in the subject, if the catalog is SSR or CSR then should subject should contain customer name.

 

hanumesh_0-1742745424450.png

 


Thanks,

Looking forward for the solution

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@hanumesh 

you can set the email subject using email script like this

Also include the email script like this, remove subject from email notification as it will be set via the email script now

${mail_script:mailScriptName}

1) email script for notification on sc_task

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

    // Add your code here
    var task = current;
    var item = task.item.getRefRecord();
    var catalogType = item.u_catalog_type; // Assuming 'u_catalog_type' is the field name for catalog type
    var customerName = task.variables.account; // Assuming 'account' is the variable name for customer name

    if (catalogType == 'SSR' || catalogType == 'CSR') {
        email.subject = 'Task ' + task.number + ' has been assigned to your group - ' + customerName;
    } else {
        email.subject = 'Task ' + task.number + ' has been assigned to your group';
    }

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

2) email script for notification on task_sla

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

    // Add your code here
    var slaTask = current.task.getRefRecord();
    var item = slaTask.item.getRefRecord();
    var catalogType = item.u_catalog_type; // Assuming 'u_catalog_type' is the field name for catalog type
    var customerName = slaTask.variables.account; // Assuming 'account' is the variable name for customer name

    if (catalogType == 'SSR' || catalogType == 'CSR') {
        email.subject = 'SLA "' + current.sla + '" associated with ' + slaTask.number + ' has breached - ' + customerName;
    } else {
        email.subject = 'SLA "' + current.sla + '" associated with ' + slaTask.number + ' has breached';
    }

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

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

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@hanumesh 

you can set the email subject using email script like this

Also include the email script like this, remove subject from email notification as it will be set via the email script now

${mail_script:mailScriptName}

1) email script for notification on sc_task

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

    // Add your code here
    var task = current;
    var item = task.item.getRefRecord();
    var catalogType = item.u_catalog_type; // Assuming 'u_catalog_type' is the field name for catalog type
    var customerName = task.variables.account; // Assuming 'account' is the variable name for customer name

    if (catalogType == 'SSR' || catalogType == 'CSR') {
        email.subject = 'Task ' + task.number + ' has been assigned to your group - ' + customerName;
    } else {
        email.subject = 'Task ' + task.number + ' has been assigned to your group';
    }

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

2) email script for notification on task_sla

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

    // Add your code here
    var slaTask = current.task.getRefRecord();
    var item = slaTask.item.getRefRecord();
    var catalogType = item.u_catalog_type; // Assuming 'u_catalog_type' is the field name for catalog type
    var customerName = slaTask.variables.account; // Assuming 'account' is the variable name for customer name

    if (catalogType == 'SSR' || catalogType == 'CSR') {
        email.subject = 'SLA "' + current.sla + '" associated with ' + slaTask.number + ' has breached - ' + customerName;
    } else {
        email.subject = 'SLA "' + current.sla + '" associated with ' + slaTask.number + ' has breached';
    }

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

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