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

Assumptions:


  • You want all values in a single array
  • Assuming that your request choice query is intended to be nameSTARTSWITHincident and not nameSTARTSWITHrequest , otherwise just change it to {1}


Solution


var choices = [];


//NQ = Grouped OR


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


var encodeQuery = gs.getMessage(qryFormat, [


        "incident", //{0}


        "request", //{1}


        comp //{2}


]);




var grChoice = new GlideRecord("sys_choice");


grChoice.addEncodedQuery(encodeQuery);


grChoice.query();


while (grChoice.next()) {


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


}



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

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

Hi Paul,



Thanks a lot for that, it works perfectly and it's way more extensible than writing duplicate gliderecords with different queries!


Happy to help



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