How to call a script include function with a parameter in a catalog client script

pavani19
Kilo Expert

the script include is:

var RL_WkeyLinkDetails = Class.create();
RL_WkeyLinkDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
callRLapi : function(wkey){
var r = new sn_ws.RESTMessageV2('RL_rldbApi', 'Get_WkeyLinkDetails');
midserver = gs.getProperty('mid.server.rba_default');
r.setMIDServer(midserver);
r.setStringParameter('code',wkey);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var res= JSON.parse(responseBody);
var res1= JSON.stringify(res);
if(res.length>0){
alert("Do you want to update the site map link of :"+ wkey +":"+res[0].shortDesc);
} else{
alert(" please enter a valid wkey");
}

},

type: 'RL_WkeyLinkDetails'
});

 

i want to call this script include from catalog client script and pass the wkey parameter value from input value of catalog request field value.

catalog client script is:

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

//Type appropriate comment here, and begin script below
var wkey = g_form.getValue('ux_wkey') ;//geeting variable value


var ga = new GlideAjax('RL_WkeyLinkDetails'); // SI Name
ga.addParam( 'sysparm_name', 'callRLapi'); // Function Name


}

 

i am not getting an idea how to pass wkey parameter for callRLapi function.

 

please help..

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Pavani,

Your script include and catalog client script looks incorrect; please correct it as below

Script Include: Corrections

1) you need to call API and only send the response body from the function

2) you need to get the wkey using this.getParameter() terminology

var RL_WkeyLinkDetails = Class.create();
RL_WkeyLinkDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

callRLapi : function(){

// this is how you get the value passed from client script

var wkey = this.getParameter('sysparm_wkey');

var r = new sn_ws.RESTMessageV2('RL_rldbApi', 'Get_WkeyLinkDetails');
midserver = gs.getProperty('mid.server.rba_default');
r.setMIDServer(midserver);
r.setStringParameter('code',wkey);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

return responseBody;

},

type: 'RL_WkeyLinkDetails'
});

Catalog Client Script Corrections:

1) send sysparm_wkey with the form value

2) parse the json response body returned from the script include function

catalog client script is:

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

//Type appropriate comment here, and begin script below
var wkey = g_form.getValue('ux_wkey') ;//geeting variable value


var ga = new GlideAjax('RL_WkeyLinkDetails'); // SI Name
ga.addParam('sysparm_name', 'callRLapi'); // Function Name
ga.addParam('sysparm_wkey', wkey); // send the value to script include function

ga.getXML(getValue);

}

function getValue(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

var parsedData = JSON.parse(answer);

// parse the json and set the value wherever required

// parse for url, short description, app ID

// use g_form.setValue() to set the values

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

6 REPLIES 6

HI,


It should be g_form and not g_from.


Replace g_from.setValue with g_form.setValue.



Thanks,
Ashutosh

HI Pavani,

 

Use this line

g_form.setValue('ux_url',url);

 

Regards,

Sagar Pagar

The world works with ServiceNow