Script Include not returning any values

reagz
Kilo Contributor

Hi All,

I am losing my mind trying to figure out what is wrong with my script include 😕  Any help would be greatly appreciated 🙂

This is related to a custom scoped application, the deliverable table has a reference field called 'Country', which comes from the x_tnmu2_nlsn_qem_tbl_Country_Master table. 

When the form is loaded, and I click on the lookup using list icon (magnifying glass), the script runs successfully but produces no results in the lookup window.  However, I know the script is querying correctly as the gs.log statement produces the record I want retured:

System Logs - Script Log Statements:

sys_idIN ,Canada

 

Here is the code I have in the country field advanced reference qualifier:

javascript:(new getCountryDetails).getCountryByRole()

 

Here is the code I have in the Script Include (setup as 'Client Callable'):

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

getCountryByRole: function(){
var gr=new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
gr.addQuery('country','Canada');
gr.query();

var answer = ' ';
while(gr.next()) {
var country = gr.getValue('country');
gs.info(country);

if (answer.length > 0) {
answer += (',' + country);
}
else {
answer = country;
}
}
gs.info('sys_idIN' + answer);
return 'sys_idIN' + answer;
},

type: 'getCountryDetails'
});

 

Please let me know if you require more info ... but I'm sure it is something simple that I am doing wrong.  I have wrote numerous other script includes in this same way which are working fine, so I am at a loss as to why this one is not working.

Thanks in advance!!!!

1 ACCEPTED SOLUTION

function getCountryByRole: function() { 
  var countrySysIDs = [];
  var countryrecord= new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master'); 
  countryrecord.addEncodedQuery('country.countrySTARTSWITHCanada');
  countryrecord.query();
  while(countryrecord._next()) { 
    countcountrySysIDsry.push( countryrecord.getValue('sys_id') ); //sys_id
  }
  return 'sys_idIN' + countrySysIDs.join(","); 
}

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

View solution in original post

18 REPLIES 18

SanjivMeher
Kilo Patron
Kilo Patron

Reference qualifier should be javascript:new getCountryDetails.getCountryByRole()

and the script include shouldn't be client callable. Because you are not calling it from a Client script.

 

Also correct the script as below

 

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

getCountryByRole: function(){ 
var gr=new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master'); 
gr.addQuery('country','Canada');
gr.query();

var answer = ' ';
while(gr.next()) { 
var country = gr.getValue('country');
gs.info(country);

if (answer.length > 0) { 
answer += ',' + country;
}
else {
answer = country;
}
}
gs.info('sys_idIN' + answer);
return 'sys_idIN' + answer; 
},

type: 'getCountryDetails'
});


Please mark this response as correct or helpful if it assisted you with your question.

Thanks for the quick reply 🙂

But when I make your suggested changes, I now get a warning error on the script:

org.mozilla.javascript.EcmaError: undefined is not a function.
Caused by error in sys_dictionary.9bfbaa4d6f5453c07a5877131c3ee40f at line 1

==> 1: new getCountryDetails.getCountryByRole()

 

Here is the updated ref qualifier:

javascript:new getCountryDetails.getCountryByRole()

 

Here is the updated script include:

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

getCountryByRole: function(){
var gr=new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
//gr.addQuery('access_role','!=','');
gr.addQuery('country','Canada');
gr.query();

var answer = ' ';
while(gr.next()) {
var country = gr.getValue('country');
gs.info(country);
if (answer.length > 0) {
answer += ',' + country;
}
else {
answer = country;
}
}
gs.info('sys_idIN' + answer);
return 'sys_idIN' + answer;
},

type: 'getCountryDetails'
});

 

 

Can you change javascript:new getCountryDetails.getCountryByRole() to javascript:new getCountryDetails().getCountryByRole()


Please mark this response as correct or helpful if it assisted you with your question.

@sanjivmeher .... I tried that and the script executes without errors/warnings ... but the list is empty (see attachment), even though the gs.log message produces the correct value:

sys_idIN ,Canada

 

Also, I am confused when to make the script 'client callable' as the below document says it should be in order to use in a reference qualifier:

https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html

 

Updated Ref Qualifier:

javascript:new getCountryDetails().getCountryByRole()

 

This is the updated script, which currently does NOT have 'client callable' selected:

var getCountryDetails = Class.create();
getCountryDetails.prototype={

initialize: function(){},

getCountryByRole: function(){
var countryrecord=new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
countryrecord.addQuery('country','Canada');
countryrecord.query();

var answer = ' ';
while(countryrecord._next()) {
var country = countryrecord.getValue('country');
gs.info(country);
if (answer.length > 0) {
answer += ',' + country;
}
else {
answer = country;
}
}
gs.info('sys_idIN' + answer);
return 'sys_idIN' + answer;
},

type: 'getCountryDetails'
};