Issue with Email Notification Script – Inconsistent Variable Passing

AkashD462166574
Tera Contributor

Hello Everyone,
I have implemented an email integration for a use case as described below:

Use Case:
When a task is created, an email should be sent to a specific recipient (XYZ ID). The email must contain all the variables and values submitted by the user.

To achieve this:

  • We configured a notification.
  • We wrote an email script to fetch variables from the task.
  • This script is then called within the notification.

Issue Faced:
The email is triggering correctly, and in most cases, it passes all the variables as expected. However, occasionally, it only includes 2–3 variables instead of all. This issue occurs inconsistently — for example, out of 10 requests, 9 work perfectly, but 1 fails to include all variables.

Has anyone faced a similar issue or can suggest what might be causing this intermittent behavior?

Below is email script:

(function runMailScript(current, template, email, email_action, event) {
    var task = current;
    if (task.variables.requested_by) {
        // Add the requested_for user to the CC list
        email.addAddress('cc', task.variables.requested_by.email.toString());
        gs.log("Email adresss of CC:" + task.variables.requested_by.email.toString());
    }
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(current.request_item.sys_id.toString());
    set.setTaskID(current.sys_id.toString());
    set.load();
    var vs = set.getFlatQuestions();
    gs.log("variables value:" + vs);
    for (var i = 0; i < vs.size(); i++) {
        if (vs.get(i).getLabel() != '' && vs.get(i).getDisplayValue() != '' && vs.get(i).getDisplayValue() != 'false') {
            template.space(4);
            template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");
            gs.log("Email Body isuue:" + (' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />"));
        }

    }
    template.print('</font>');
})(current, template, email, email_action, event);

Thanks in advance!

4 REPLIES 4

Hemanth M
Tera Sage

Hi @AkashD462166574 ,

 

when you say "This issue occurs inconsistently" are those similar request?? Same variable type?

 

To debug : Get the affected request, go to the actual notification, preview and check if its working correctly and see where is breaking!

 

 

 

Hello Hemant,
Not for similar Request, I happens for different request. I also observe 1-2 request which are raise for same category and sub category in which all variables are same but for 1 request its working fine and for another request of same type its not working

Hi @AkashD462166574 ,

 

Have you tried preview to debug the issue?

 

 

Hello Hemanth,
Yes I tried preview and I can see all variables.