http query parameters dynamically

shabbir9
Tera Contributor

hii 

 

i need to do variable substitution my http query parameter name is enclave_name and my variable name is enclave_name..if i give any value in script restMessage.setStringParameterNoEscape('enclave_name',  'tes123');mannually its working fine iam getting response.. but i need to do dynamically when ever i select enclave names in enclave_name variable on catalog item i need to populate response in u_comment varible i tried in different ways but its not working sharing script and screen shots kindly help on this 

client script ;;;

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;

}
var ga = new GlideAjax('GetEnclaveData');
ga.addParam('sysparm_name', 'getData');
ga.addParam('sysparm_enclave',g_form.getValue('enclave_name'));
ga.getXMLAnswer(getResponse);
// Get the value of the 'response' variable
function getResponse(answer) {
g_form.setValue('u_comment',answer);

}

}

 

script include;;;

var GetEnclaveData = Class.create();

GetEnclaveData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getData:function(){
//gs.log('indise SI','sh1');
var queryParam = this.getParameter('sysparm_enclave');
gs.log("check query param 231"+enclaveValue);
var restMessage = new sn_ws.RESTMessageV2('network access', 'GET Enclave Data By Enclave Name');
restMessage.setStringParameterNoEscape('enclave_name','tes123');its working fine static
restMessage.setStringParameterNoEscape('enclave_name', queryParam);its not working dynamic..i have taken enclave_name values in queryParam..in log iam getting the value which i selected on the catalog item
restMessage.setHttpMethod('GET');
var response = restMessage.execute();
gs.log("check enclave response code"+response.getStatusCode(),'sh1');
var responseBody = response.getBody();
gs.log("check enclave response 1"+responseBody,'sh1');
var httpStatus = response.getStatusCode();
gs.log("check enclave response http"+responseBody,'sh1');
if (httpStatus == 200) {
// Store the response body in a variable
gs.setVariable('response', responseBody);
//return JSON.stringify(responseBody);
return (responseBody);

// gs.log("check enclave response"+responseBody);
}
},
type: 'GetEnclaveData'
});

 

shabbir9_0-1674478206388.png

 

 

shabbir9_1-1674478206441.png

 

 

3 REPLIES 3

Laszlo Balla
Mega Sage
Mega Sage

You screenshots are too small to see. I see you have variable substitution configured but I can't see what is actually the name of the query variable and the substitute.

 

You should have an query parameter with name 'enclave_name' and value '${enclave_name}', and a related variable substitute named 'enclave_name' as well.

Then make sure you comment out your static test 🙂

sorry i didn't check screen shots properly my query parameter name is enclave_name...if i send any static value in the value filed iam getting response but i have variable on catalog item enclave_name in that variable i have some names list i need place that variable in value so that  when ever user selects any value in that variable i need to the response can you please check this 

 

Then make sure you comment out your static test ... iam not getting this point 

 

shabbir9_0-1674536738330.png

shabbir9_1-1674536788614.png

 

 

 

I see. It actually looks good and I see no reason why it wouldn't work as expected... Do you know what is actually sent? Try creating an interceptor endpoint (e.g. by using beeceptor) so you can verify what are you actually sending out of ServiceNow.