Email script for populating Opened by value in notification

may13
Tera Contributor

Hello Team

I have a requirement in my current project where I need to send an email notification after approvals rejected so that I have created one email notification the approval table. In the notification body, I want to display the name of the person who rejected the HR case (i.e., the "Opened By" name), along with the HR case number.
However, I'm unable to find the fields for "Opened By", "HR Case No", and the name of the person who rejected the approval. Does anyone know how to write email script?

2 REPLIES 2

Pankaj31
Tera Contributor

For the above requirement, the email script won't be required. You can achieve the same thing by dot walk "

Approval for Number: ${sysapproval.number}

Approval for Opened by: ${sysapproval.opened_by} "

 

Attached the notification XML for your reference.

kaushal_snow
Mega Sage

Hi @may13 ,

 

Please use the below email script..

(function runMailScript(current, template, email, email_action, event) {
  
    var hrCaseGR = new GlideRecord('sn_hr_core_case'); // Replace with your HR case table name
    hrCaseGR.get(current.hr_case); // 'current' is the approval record
    // Print HR Case Number
    template.print('<strong>HR Case Number:</strong> ' + hrCaseGR.getDisplayValue('number') + '<br/>');
    // Print Opened By
    template.print('<strong>Opened By:</strong> ' + hrCaseGR.opened_by.getDisplayValue() + '<br/><br/>');

    // Find approver who rejected
    var appr = new GlideRecord('sysapproval_approver');
    appr.addQuery('sysapproval', current.sysapproval);
    appr.addQuery('state', 'rejected'); // state that indicates rejection
    appr.query();
    if (appr.next()) {
        template.print('<strong>Rejected By:</strong> ' + appr.approver.getDisplayValue() + '<br/><br/>');
    }

    template.print('Your HR case approval was **rejected**. Please review and take necessary actions.');
})(current, template, email, email_action, event);

 

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/