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

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/

Bhimashankar H
Mega Sage

Hi @may13 ,

 

You can use below mail script and use this in email notification

// Applies when used from a notification on sysapproval_approver
(function runMailScript(current, template, email, email_action, event) {
  try {
    // current is a sysapproval_approver record
    // Resolve the HR case from Approving/document_id
    var hr = current.sysapproval ? current.sysapproval.getRefRecord() : (current.document_id ? current.document_id.getRefRecord() : null);

    // HR case number and opened by
    var caseNumber = hr ? hr.getDisplayValue('number') : '';
    var openedBy = hr ? hr.getDisplayValue('opened_by') : '';

    // Rejecting approver and comments from the approval record that is rejected
    var rejectedBy = current.getDisplayValue('approver');
    var rejectionState = current.getValue('approval') || current.getValue('state'); // state/approval varies by instance
    var rejectionComment = '';

    // Safely pull the last journal entry if available
    try {
      if (current.comments) {
        // getJournalEntry(1) returns most recent entry
        rejectionComment = current.comments.getJournalEntry(1) || '';
      }
    } catch (e) {
      // ignore if no journal
    }

    // Print a compact block that the notification body can insert
    template.print('HR Case: ' + (caseNumber || '(none)') + '\n');
    template.print('Opened by: ' + (openedBy || '(none)') + '\n');
    template.print('Rejected by: ' + (rejectedBy || '(unknown)') + '\n');
    template.print('Approval state: ' + (rejectionState || '(unknown)') + '\n');
    if (rejectionComment)
      template.print('Rejection comments:\n' + rejectionComment + '\n');
  } catch (err) {
    template.print('HR Case: (error)\n');
  }
})(current, template, email, email_action, event);

 

It will have the rejection comment as well in email notificaton.

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

hey @may13 ,

 

Did you get a chance to go through my previous reply.


If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. It will help future readers as well having similar kind of questions and close the thread.

Thanks,
Bhimashankar H