Pulling variables into the email notification for an approval request

sarahjantz
Tera Expert
I'm trying to modify a script to include all variables for the RITM that approval is being requested for. Can somebody help me modify the script below to accomplish this? 
 
(function runMailScript(current, template, vemail, email_action, event) {
 
    var grReq = new GlideRecord('sc_req_item');
    grReq.get('sys_id', current.sysapproval); //checks the current sys_id of RITM in the "Approval for"
    for (var i in grReq.variables) { //to get the variables from the RITM
        if (grReq.variables[i]) {
            template.print(getQuestionLabel(i) + ":" + grReq.variables[i].getDisplayValue()); //gets and sets the question label of variable and also called the function "getQuestionLabel"
            template.print('<br></br>');
        }
    }

    function getQuestionLabel(question) { //declare the function "getQuestionLabel"
        var getVar = new GlideRecord('item_option_new');
        getVar.addQuery('name', question);
        getVar.query();
        if (getVar.next()) {
            var question_label = getVar.question_text; //sets the question label from variable
            return question_label; //returns the variable name

        }
    }
    //code for copying the attachment onto the email notification "IC - Approval Request  for RITM"

    attachLinks(); //function

    function attachLinks() {
        //Check for any attachments and add attachment links if they exist
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_sys_id', current.sysapproval.sys_id);
        gr.query();
        if (gr.hasNext()) {
            template.print("Attachments: <br />"); //prints the line as "Attachments:"
            while (gr.next()) {
                var attachLink = '<a href="' + gs.generateURL(gr.getTableName(), gr.sys_id) + '">' + gr.file_name + '</a>'; //generates the attachment link
                template.print(attachLink + "<br />"); //prints the attachment link
            }
            template.print("<hr/>");
        }
    }
})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @sarahjantz 

Ihaven't had the chance to test it yet, but you can give it a try. I made a minor correction in the script. Please verify and test if it resolves the issue.

(function runMailScript(current, template, email, email_action, event) {

    // Retrieve the RITM record
    var grReq = new GlideRecord('sc_req_item');
    if (grReq.get('sys_id', current.sysapproval)) {
        // Loop through the variables of the RITM
        for (var i in grReq.variables) {
            if (grReq.variables[i]) {
                template.print(getQuestionLabel(i) + ": " + grReq.variables[i].getDisplayValue() + "<br/>");
            }
        }
    }

    // Function to get the question label for a variable
    function getQuestionLabel(question) {
        var getVar = new GlideRecord('item_option_new');
        getVar.addQuery('name', question);
        getVar.query();
        if (getVar.next()) {
            return getVar.question_text;
        }
    }

    // Attach links for any attachments
    attachLinks();

    // Function to attach links for attachments
    function attachLinks() {
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_sys_id', current.sysapproval.sys_id);
        grAttachment.query();
        if (grAttachment.hasNext()) {
            template.print("Attachments: <br/>");
            while (grAttachment.next()) {
                var attachLink = generateAttachmentLink(grAttachment);
                template.print(attachLink + "<br/>");
            }
            template.print("<hr/>");
        }
    }

    // Function to generate an attachment link
    function generateAttachmentLink(attachment) {
        return '<a href="' + gs.generateURL(attachment.getTableName(), attachment.sys_id) + '">' + attachment.file_name + '</a>';
    }

})(current, template, email, email_action, event);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Regards,
Aniket

View solution in original post

3 REPLIES 3

Aniket Chavan
Tera Sage
Tera Sage

Hello @sarahjantz 

Ihaven't had the chance to test it yet, but you can give it a try. I made a minor correction in the script. Please verify and test if it resolves the issue.

(function runMailScript(current, template, email, email_action, event) {

    // Retrieve the RITM record
    var grReq = new GlideRecord('sc_req_item');
    if (grReq.get('sys_id', current.sysapproval)) {
        // Loop through the variables of the RITM
        for (var i in grReq.variables) {
            if (grReq.variables[i]) {
                template.print(getQuestionLabel(i) + ": " + grReq.variables[i].getDisplayValue() + "<br/>");
            }
        }
    }

    // Function to get the question label for a variable
    function getQuestionLabel(question) {
        var getVar = new GlideRecord('item_option_new');
        getVar.addQuery('name', question);
        getVar.query();
        if (getVar.next()) {
            return getVar.question_text;
        }
    }

    // Attach links for any attachments
    attachLinks();

    // Function to attach links for attachments
    function attachLinks() {
        var grAttachment = new GlideRecord('sys_attachment');
        grAttachment.addQuery('table_sys_id', current.sysapproval.sys_id);
        grAttachment.query();
        if (grAttachment.hasNext()) {
            template.print("Attachments: <br/>");
            while (grAttachment.next()) {
                var attachLink = generateAttachmentLink(grAttachment);
                template.print(attachLink + "<br/>");
            }
            template.print("<hr/>");
        }
    }

    // Function to generate an attachment link
    function generateAttachmentLink(attachment) {
        return '<a href="' + gs.generateURL(attachment.getTableName(), attachment.sys_id) + '">' + attachment.file_name + '</a>';
    }

})(current, template, email, email_action, event);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Regards,
Aniket

Hello Aniket,

 

Thank you for the quick response. This worked!

Hello @sarahjantz ,

Welcome & Glad to hear that it worked 😁