Custom notification display of variable in email

kumar G
Tera Contributor

We are facing some challenges to populate all variables in email notification using 

var set = new GlideappvariablePoolQuestionSet();

set.setRequest(item.sys_id);

set.load();

 

Is it possible to load some set of variable for that item specific based on business needs insteads of entire form varirables.

 

2 REPLIES 2

Anirudh Pathak
Mega Sage

Hi @kumar G ,

Do you want to show only few variables based on some conditions?

 

You can create an email script using below code, I've added a comment in code where you can specify the condition to show only specific variables. Replace it with your own conditions - 

var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', 'your ritm sys id');
    ritm.query();
    if (ritm.next()) {
        var variables = ritm.variables.getElements();
        for (var i = 0; i < variables.length; i++) {
            var question = variables[i].getQuestion();
            var label = question.getLabel();
            var value = question.getDisplayValue();


            if (label != '' && value != '') {
                 // Specify condition here which variables should be visible
               if(value == 'xyz')  {
                template.print(label + " = " + value + "<br/>");
            }
               }
        }
 }

 Call the email script in your notification - ${mail_script:email_script_name}

 

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @kumar G 

Yes, you can load specific sets of variables for a given item (such as an incident, request, or task) based on your business needs. The GlideappvariablePoolQuestionSet API allows you to work with sets of variables associated with a particular item.

Here's an example of how you can load specific sets of variables for an item:

 

// Create a new GlideappvariablePoolQuestionSet object
var set = new GlideappvariablePoolQuestionSet();

// Set the item (record) ID for which you want to load variables
var itemId = 'your_item_sys_id_here'; // Replace 'your_item_sys_id_here' with the actual item Sys ID
set.setRequest(itemId);

// Load the variables for the specified item
set.load();

// Now you can work with the loaded variables
// For example, you can iterate over the variables and retrieve their values
while (set.hasNext()) {
    var question = set.next();
    var questionText = question.getQuestionText();
    var answer = question.getDisplayValue();
    
    // Do something with the question and answer
}

 

In this script:

  • Replace 'your_item_sys_id_here' with the actual Sys ID of the item for which you want to load variables.
  • The set.load() method loads the variables associated with the specified item.
  • You can then iterate over the loaded variables using methods like set.hasNext() and set.next() to retrieve each variable's details, such as the question text and display value.

By using this approach, you can target specific sets of variables associated with an item and work with them according to your business needs, rather than loading all variables for the entire form.

 

Mark Accept as Solution & HIT helpful !!! 

 
Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)