Retrieve Data from Variables for Approval Email Notification

Please find the script to get the latest/updated data from the Variables.


Email script:

(function runMailScript(current, template, email, email_action, event) {
// Function to convert reference value to display value
function getReferenceDisplayValue(table, value) {
var displayValue = '';
var gr = new GlideRecord(table);
gr.addQuery('sys_id', value);
gr.query();
if (gr.next()) {
displayValue = gr.getDisplayValue();
}
return displayValue;
}
// Function to convert choice value to display value
function getChoiceDisplayValue(question, value) {
var displayValue = '';
if (question.choice_table) { //Choices are configured on sys_choice
var grSysChoice = new GlideRecord('sys_choice');
grSysChoice.addEncodedQuery("element=" + question.choice_field.toString() + "^name=" + question.choice_table.toString() + "^value=" + value);
grSysChoice.setLimit(1);
grSysChoice.query();
if (grSysChoice.next()) {
displayValue = grSysChoice.getValue('label');
}
} else { //Choices are configured on RP
var gr = new GlideRecord('question_choice');
gr.addEncodedQuery('question=' + question.sys_id + '^value=' + value);
gr.query();
if (gr.next()) {
displayValue = gr.text + '';
}
if (displayValue == '') {
displayValue = value + '';
}
}
return displayValue;
}
// Function to convert choice value to display value for List Collector
function getChoiceDisplayValueList(value) {
var displayValue = '';
var gr = new GlideRecord('question_choice');
gr.addEncodedQuery('sys_id=' + value);
gr.query();
if (gr.next()) {
displayValue = gr.text + '';
}
return displayValue;
}
// Function to format checkbox value as "Yes" or "No"
function formatCheckboxValue(checkboxValue) {
// Convert true/false to "Yes"/"No"
return checkboxValue === 'true' ? 'Yes' : 'No';
}
// Get the document_id from the current record
var documentId = current.document_id;
//var documentId = 'd03df2aa1bf6ca503af60e1ce54bcbd4';
// Retrieve the associated case
var hrCaseGr = new GlideRecord('sn_hr_core_case');
if (!hrCaseGr.get(documentId)) {
template.print("No associated HR case found.");
return;
}
// Initialize variables
var type, name, value, reference, val, item;
// Query for producer variables
var producerVars = new GlideRecord('question_answer');
producerVars.addQuery('table_sys_id', documentId);
producerVars.addEncodedQuery("question.active=true");
// Exclude certain question types
producerVars.addQuery('question.type', 'NOT IN', [11, 19, 20, 14]);
producerVars.orderBy('order');
producerVars.query();
// Generate HTML table
var htmlTable = '<table style="border-collapse:collapse;border-spacing:0" class="tg">';
htmlTable += '<thead>';
htmlTable += '<tr>';
htmlTable += '<th style="background-color:#205cba;border-color:inherit;border-style:solid;border-width:1px;color:#ffffff;font-family:Verdana, geneva, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:5px 5px;text-align:left;vertical-align:top;word-break:normal">Name</span></th>';
htmlTable += '<th style="background-color:#205cba;border-color:inherit;border-style:solid;border-width:1px;color:#ffffff;font-family:Verdana, geneva, sans-serif;font-size:14px;font-weight:normal;overflow:hidden;padding:5px 5px;text-align:left;vertical-align:top;word-break:normal">Value</span></th>';
htmlTable += '</tr>';
htmlTable += '</thead>';
htmlTable += '<tbody>';
// Iterate over producer variables
while (producerVars.next()) {
// Check if variable has a value
if (producerVars.value) {
// Extract variable detailsHone
type = producerVars.question.type.toString();
name = producerVars.question.question_text.toString();
val = producerVars.value.toString();
// Process based on variable type
switch (type) {
// Select Box type
case '5':
item = producerVars.question;
value = getChoiceDisplayValue(item, val);
break;
// Checkbox
case '7':
value = formatCheckboxValue(val);
break;
// Reference type
case '8':
reference = producerVars.question.reference.toString();
value = getReferenceDisplayValue(reference, val);
break;
// List Collector type
case '21':
var valSplit = val.split(',');
var arr = valSplit.map(function(item) {
return getChoiceDisplayValueList(item);
});
value = arr.join(',');
break;
// Default
default:
value = val;
break;
}
htmlTable += '<tr>';
htmlTable += '<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Verdana, geneva, sans-serif;font-size:14px;overflow:hidden;padding:5px 5px;text-align:left;vertical-align:top;word-break:normal">' + name + '</td>';
htmlTable += '<td style="border-color:inherit;border-style:solid;border-width:1px;font-family:Verdana, geneva, sans-serif;font-size:14px;overflow:hidden;padding:5px 5px;text-align:left;vertical-align:top;word-break:normal">' + value + '</td>';
htmlTable += '</tr>';
}
}
htmlTable += '<tbody>';
htmlTable += '</table>';
// Replace the placeholder with the generated HTML table
template.print(htmlTable);
})(current, template, email, email_action, event); Output Example: