The CreatorCon Call for Content is officially open! Get started here.

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
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

Thanks @Abhijeet_Pawar . I tried this and now I able to get the variables on the email.

 

 

You're welcome @shareef_223 I'm glad to hear that you were able to get the variables in your email.

If you have any more questions or need further assistance, feel free to ask!

Thanks,

Abhijeet.

 

Hello @Abhijeet_Pawar 

 

I tried this and now I able to get the variables on the email.

And I have a request here where we have a variable (check box) it contains more than 40 options, How can I get them in email notification.

 

For ex: What is your state and it is having 30 options but I have to populate the selected option in the email over "What is your state" field.

 

 

Saved so much time brother , thanks