How to pull only active fields using GlideappVariablePoolQuestionSet in email script

anushadande1793
Tera Contributor

Hi 

 

I would like to pull only active fields in email notification. There is one email notification script to pull all the variables associated to the RITM but it is pulling inactive fields as well

Below is the email script which is responsible to pull the variables associated to the RITM (notification written on sysapproval table)

 

var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.getValue('sysapproval'));
set.load();
var vs = set.getFlatQuestions();
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() + " : " + "<br>" + "&nbsp;" +"&nbsp;" +"&nbsp;" +"&nbsp;" + vs.get(i).getDisplayValue() + "<br>" + "<br/>");
   
}
}
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@anushadande1793 

it won't work directly.

you will have to enhance and query to know if it's active or not

something like this

var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.getValue('sysapproval'));
set.load();
var vs = set.getFlatQuestions();

// Step 1: Collect all variable sys_ids from the questions
var varSysIds = [];
for (var i = 0; i < vs.size(); i++) {
    varSysIds.push(vs.get(i).getID());
}

// Step 2: Query all active variables in one go
var activeVars = {};
var grVar = new GlideRecord('item_option_new');
grVar.addQuery('sys_id', 'IN', varSysIds.join(','));
grVar.addQuery('active', true);
grVar.query();
while (grVar.next()) {
    activeVars[grVar.getValue('sys_id')] = true;
}

// Step 3: Only print variables that are active
for (var j = 0; j < vs.size(); j++) {
    var question = vs.get(j);
    var varSysId = question.getID();
    if (activeVars[varSysId]) {
        if (question.getLabel() != '' && question.getDisplayValue() != '' && question.getDisplayValue() != 'false') {
            template.space(4);
            template.print(question.getLabel() + " : " + "<br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + question.getDisplayValue() + "<br><br/>");
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@anushadande1793 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader