Printings variables in a notification

Snow-Man
Tera Contributor
 

 

    var result = new GlideRecord('sc_req_item');
    result.addQuery('sys_id', current.sysapproval);
    result.query();
    if (result.next()) {
var variables = result.variables.getElements();
        for (var i = 0; i < variables.length; i++) {
            var question = variables[i].getQuestion();
            var label = question.getLabel();
            var value = question.getDisplayValue();
            // labels = ['requester_economic_location', 'requested_for_economic_location', 'manager_location', 'request_for_other_user'];
            if ((label != '')) {
                template.space(4);
                template.print('  ' + label + " = " + value + "<br/>");
            }
        }

I need to print variables in a notification,

 

My variables are getting printed but there are few variables that I don't want them to be printed in the notification. 
what could be the logic for that?
 
I have created a list (LABELS) of variables with their backend names that I don't want them to be printed, please help!
 
  
4 REPLIES 4

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Snow-Man,

 

I had a look into your code and it seems to be fine. Made a little adjustment:

 

 

var result = new GlideRecord('sc_req_item');
result.addQuery('sys_id', current.sysapproval);
result.query();

var unwantedVariables = ['requester_economic_location', 'requested_for_economic_location', 'manager_location', 'request_for_other_user'];

if (result.next()) {
    var variables = result.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var value = question.getDisplayValue();
        
        // Check if the label is in the unwantedVariables list
        if (label !== '' && unwantedVariables.indexOf(label) === -1) {
            template.space(4);
            template.print('  ' + label + " = " + value + "<br/>");
        }
    }
}

 

 

 

This code creates an array unwantedVariables containing the labels you don't want to print. Inside the loop, it checks if the current label exists in this array using indexOf. If the label is not found in the unwantedVariables list, it will be printed in the notification. If it is found, it will skip printing that variable.

 

Test it and let me know!

Hi, I just tried this. now no variables are getting populated, I feel the if condition is getting false due to which variables are not getting populated.

Are you writing this code in email script under notifications right?

Prince Arora
Tera Sage
Tera Sage

@Snow-Man 

 

As the variables from the RITM are stored in "sc_item_option_mtom"

Can you try below script:

 

var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');

sc_item_option_mtom.addQuery("request_item", "sys_id_of_ritm");

sc_item_option_mtom.orderBy("sc_item_option.order");

sc_item_option_mtom.query();

while (sc_item_option_mtom.next()){

var details = GlideappQuestion.getQuestion(sc_item_option_mtom.sc_item_option.item_option_new);

details.setValue(sc_item_option_mtom.sc_item_option.value);

gs.info(details.getLabel() + ": " + details.getDisplayValue());

}

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.