- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 02:03 AM
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
${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} |
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:41 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 02:53 AM - edited 04-02-2024 05:02 AM
@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.
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 05:01 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 05:05 AM - edited 04-02-2024 05:06 AM
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:41 AM
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.