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?

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@may13 

you cannot pick HR Case -> Opened By directly as notification is on sysapproval_approver table.

You will have to use business rule on sysapproval_approver table, query HR Case and get Opened By and then include in recipient list

where did you start with and where are you stuck?

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

Chavan AP
Kilo Sage

@may13  - i just tested below script it should work for you as well- 

 

(function runMailScript(current, template, email, email_action, event) {
    try {
        // Get the HR case record from the approval record
        var hrCase = current.sysapproval ? current.sysapproval.getRefRecord() : 
                     current.document_id ? current.document_id.getRefRecord() : null;
        
        if (hrCase && hrCase.isValidRecord()) {
            // HR Case details
            template.print('HR Case Number: ' + hrCase.getDisplayValue('number') + '\n');
            template.print('Opened By: ' + hrCase.getDisplayValue('opened_by') + '\n');
        } else {
            template.print('HR Case Number: Not available\n');
            template.print('Opened By: Not available\n');
        }
        
        // Rejection details
        template.print('Rejected By: ' + current.getDisplayValue('approver') + '\n');
        template.print('Rejection Date: ' + current.getDisplayValue('sys_updated_on') + '\n');
        
        // Include rejection comments if available
        if (current.comments) {
            var latestComment = current.comments.getJournalEntry(1);
            if (latestComment) {
                template.print('Rejection Comments: ' + latestComment + '\n');
            }
        }
        
    } catch (e) {
        template.print('Error retrieving case details: ' + e.message);
    }
})(current, template, email, email_action, event);

 

next step is to create notification on approval table, and also make sure to add filter condition where approving record -> task type  has to be hr case

Glad I could help! If this solved your issue, please mark it as ✅ Helpful and ✅ Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****