How to populate a catalog variables in email body for Approval Notification

shareef_223
Tera Contributor

Hell Everyone,

 

I am trying to populate a 4 variables on the email body in approval notification.

I created a Approval email notification on sysapproval_approver table. and i have a catalog item with 4 variables like "First Name, Last Name, Email Id, Type of Access. I want to populate these variables on the email body.

I created a email script for this.

 

email script:  servicenow_access_varibles_on_approval

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {

    // Check if the current record is an approval record
    if (current.getTableName() === 'sysapproval_approver') {
        // Fetch the user's approval record
        var approvalRecord = new GlideRecord('sysapproval_approver');
        approvalRecord.addQuery('approver', current.approver);
        approvalRecord.query();

        // Log debug statement
        gs.log("Number of approval records for current user: " + approvalRecord.getRowCount());

        // If there's at least one approval record for the user
        if (approvalRecord.next()) {
            // Log debug statement
            gs.log("Approval record found for current user");

            // Fetch variables from the approval record
            var firstName = approvalRecord.variables.first_name.getDisplayValue();
            var lastName = approvalRecord.variables.last_name.getDisplayValue();
            var emailId = approvalRecord.variables.email_id.getDisplayValue();
            var typeOfAccess = approvalRecord.variables.type_of_access.getDisplayValue();
           
            // Print variables to the template
            template.print("First Name: " + firstName + "<br>");
            template.print("Last Name: " + lastName + "<br>");
            template.print("Email ID: " + emailId + "<br>");
            template.print("Type of Access: " + typeOfAccess + "<br>");
        } else {
            // If no approval record is found for the user
            template.print("No approval record found for the current user.");
        }
    } else {
        // If the current record is not an approval record
        template.print("This script is intended to run on approval records.");
    }

})(current, template, email, email_action, event);
 
And I created a notification for this.
 
Email Body:
 

 

${sys_created_by}'s request is awaiting for your approval
 
Hello ${approver},
${sys_created_by} has submitted a ${document_id} and awaiting for your approval.
${mailto:mailto.btn.approval} ${mailto:mailto.btn.rejection}
 
 
${mail_script:servicenow_access_varibles_on_approval}
 
 
Click on this ${URI+&sysparm_record_target=sysapproval_approver} to view Approval Request.
 
Click on this ${sysapproval.URI_REF} to view ${sysapproval.sys_class_name}

Thanks,

Team

 

I tried this but i am not able to populate variables. It is showing undefined. 

 

First Name: undefined
Last Name: undefined
Email ID: undefined
Type of Access: undefined

 

Can any one please help me out this.

 

Thanks,

Shareef

 

 
1 ACCEPTED SOLUTION

Abhijeet_Pawar
Tera Guru

Hello @shareef_223 

You can access variables of the request item without needing to write any code in the email script.

Please paste the below code into the notification body:

${document_id.variables.first_name}

Let me know if you need further clarification or assistance.

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct!!

thank you.

View solution in original post

8 REPLIES 8

SANDEEP28
Mega Sage

@shareef_223 From your code what I can see is you are trying to variables from "sysapproval_approver" table but variables are present in RITM record which is present in "Approving" field. 

 

SANDEEP28_0-1712050843722.png

 

You need to glide through "sc_req_item" table using sys_id present in "Approving" field to get the variables value. Try below script

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {

    // Check if the current record is an approval record
    if (current.getTableName() === 'sysapproval_approver') {
        // Fetch the user's approval record
        var ritmRecord = new GlideRecord('sc_req_item');
        // Log debug statement
        gs.log("Number of approval records for current user: " + approvalRecord.getRowCount());

        // If there's at least one approval record for the user
        if (ritmRecord.get(current.document_id)){
            // Log debug statement
            gs.log("RITM record found for current user");

            // Fetch variables from the approval record
            var firstName = ritmRecord.variables.first_name.getDisplayValue();
            var lastName = ritmRecord.variables.last_name.getDisplayValue();
            var emailId = ritmRecord.variables.email_id.getDisplayValue();
            var typeOfAccess = ritmRecord.variables.type_of_access.getDisplayValue();
           
            // Print variables to the template
            template.print("First Name: " + firstName + "<br>");
            template.print("Last Name: " + lastName + "<br>");
            template.print("Email ID: " + emailId + "<br>");
            template.print("Type of Access: " + typeOfAccess + "<br>");
        } else {
            // If no approval record is found for the user
            template.print("No RITM record found for the current user.");
        }
    } else {
        // If the current record is not an approval record
        template.print("This script is intended to run on approval records.");
    }
	
})(current, template, email, email_action, event);

 

 

 If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Hello @SANDEEP28 

I noticed a small mistake in your script. You seem to be querying the "sc_cat_item" table instead of the "sc_req_item" table. I believe this might be a typo.

Thank You.

Yes it was typo 🙂 corrected it

 

 

@shareef_223  I hope you got my point. You have created notification record in "sysapproval_approver " table so you don't have to do a glide query on "sysapproval_approver " table. You can access fields of it using current object. 

 

Also variables on stories in requested item table record, so you need to glide this table.

 

 If I could help you with your Query then, please hit the Thumb Icon and mark above answer as Correct !!

 

 

Abhijeet_Pawar
Tera Guru

Hello @shareef_223 

You can access variables of the request item without needing to write any code in the email script.

Please paste the below code into the notification body:

${document_id.variables.first_name}

Let me know if you need further clarification or assistance.

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct!!

thank you.