Running multiple encoded queries on the same GlideRecord

Dubz
Mega Sage

Hi All,

I'm running the below code as part of a script to return an array of choice list options. I want to return a second array of choice list options, must i declare a new GlideRecord or is there a way of clearing the original query and starting again?

var choices = [];

var gr2 = new GlideRecord("sys_choice");

gr2.addEncodedQuery("nameSTARTSWITHincident^elementSTARTSWITHclose_code^dependent_valueSTARTSWITHIncident^u_opco=" + comp);

gr2.query();

while (gr2.next()) {

choices.push(gr2.value.getDisplayValue());

}

1 ACCEPTED SOLUTION

Or this



var comp='cat';


var incidentChoices = [],


requestChoices[],


qryFormat = "nameSTARTSWITH{0}^elementSTARTSWITHclose_code^dependent_valueSTARTSWITH{0}^u_opco={1}";



var incidentQuery = gs.getMessage(qryFormat, [


"incident", //{0}


comp //{1}


]);



var requestQuery = gs.getMessage(qryFormat, [


"request", //{0}


comp //{1}


]);



function getChoiceValues(encodedQuery) {


var choiceValues=[];


var grChoice = new GlideRecord("sys_choice");


grChoice.addEncodedQuery(encodedQuery);


grChoice.query();


  gs.print(grChoice.getEncodedQuery());


while (grChoice.next()) {


choiceValues.push(grChoice.getDisplayValue('value'));


}


return choiceValues;


}




incidentChoices = getChoiceValues(incidentQuery);


requestChoices = getChoiceValues(requestQuery);



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

8 REPLIES 8

Sharique Azim
Mega Sage

Hi David,




Please elaborate what are you trying to achieve?



on the code part :


You can replace.


choices.push(gr2.value.getDisplayValue());



to


choices.push(gr2.getValue('name'));



}


choices.join(',');


return choices;


Hi Sharique,



I want to create 2 different arrays of choice options, i'm just wondering if i have to create 2 different gliderecords on the script include as per below, or if i can use the same gliderecord and just clear the original encodedQuery and add in a new one?



var choices = [];


var gr2 = new GlideRecord("sys_choice");


gr2.addEncodedQuery("nameSTARTSWITHincident^elementSTARTSWITHclose_code^dependent_valueSTARTSWITHIncident^u_opco=" + comp);


gr2.query();


while (gr2.next()) {


choices.push(gr2.value.getDisplayValue());


}


var requests = [];


var gr3 = new GlideRecord('sys_choice');


gr3.addEncodedQuery("nameSTARTSWITHincident^elementSTARTSWITHclose_code^dependent_valueSTARTSWITHRequest^u_opco=" + comp);


gr3.query();


while (gr3.next()) {


requests.push(gr3.value.getDisplayValue());


}


Hi David,



You are on the right track, you only need to send the choice 'name' instead of getDisplayValue


Hi Sharique,



The script is working with getDisplayValue but thanks for the info on getValue, it looks a bit cleaner.



My question is if i have to use 2 different GlideRecords for the same table or if i can just use 1 GlideRecord, generate an encoded query for the first array, then clear that encoded query and generate a new one for the second array?



Something like this:



var choices = [];


var gr2 = new GlideRecord("sys_choice");


gr2.addEncodedQuery("nameSTARTSWITHincident^elementSTARTSWITHclose_code^dependent_valueSTARTSWITHIncident^u_opco=" + comp);


gr2.query();


while (gr2.next()) {


choices.push(gr2.value.getDisplayValue());


}


var requests = [];


gr2.clearQuery(); //something here to clear the original query on gr2 so i can create the new query for the second array.


gr2.addEncodedQuery("nameSTARTSWITHincident^elementSTARTSWITHclose_code^dependent_valueSTARTSWITHRequest^u_opco=" + comp);


gr2.query();


while (gr2.next()) {


requests.push(gr2.value.getDisplayValue());


}