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

Navigate to a list of country records

In the query, put

Sys ID IN <your sys_ids here>

What is the result?

 


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

Still no change 😕

I have even tested the script include code in the background - scripting window and it works as expected:

var countrySysIDs = [];
var countryrecord= new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
countryrecord.addQuery('country','Canada');
countryrecord.query();

while(countryrecord._next()) {
countrySysIDs.push( countryrecord.getValue('country') );
}

gs.info('sys_idIN,' + countrySysIDs.join(","));

Result:

x_tnmu2_nlsn_qem: sys_idIN,Canada

 

.... What else would prevent the results from being displayed in the lookup list?

To clarify, on your custom table are you in the global scope? If you aren't you may need to do javascript:new global.getCountryDetails().getCountryByRole()

That's the only thing I can think of. Definitely do some logging as well within your script include so you know if it is being called.

 

-Tyler

The SN Nerd
Giga Sage
Giga Sage

"Canada" is not a valid SYS_ID.

I had a feeling this whole query just didn't make any sense.

You are checking SYS ID on a string field. 

 

Just use the condition builder...

Country IS Canada...

 

Recommend following this Guide if you are confused

https://community.servicenow.com/community?id=community_blog&sys_id=392d66e5dbd0dbc01dcaf3231f96192c&view_source=searchResult


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

Hmmm ... ok, I used the condition builder, then copied the code into my script include ... and the results are the same.

Updated script:

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

initialize: function(){},

getCountryByRole: function(){
var encQry='country.countrySTARTSWITHCanada';
var countrySysIDs = [];
var countryrecord= new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
countryrecord.addEncodedQuery(encQry);
countryrecord.query();

while(countryrecord._next()) {
countrySysIDs.push( countryrecord.getValue('country') );
}

gs.info('sys_idIN,' + countrySysIDs.join(","));
return 'sys_idIN,' + countrySysIDs.join(",");
},

type: 'getCountryDetails'
};

 

Let me back up for a minute here, as maybe I did not describe the requirement clearly.

 

1.  I have a master table for countries

2.  Users will have access to 1 or more countries ... in which they will have a specific role assigned to them, which will match the role field value in the country table.

3.  When users fill out a Product form, they will select a country associated to the product.  However, they should only see the list of countries that they have access to see.

The issue I am having is returning a filtered list of countries back to the Product form.  I was attempting to use an advanced reference qualifier to execute a script include, that will build the list of countries based on the user role.  However, as an initial test, I was trying to hard-code one country value and have it returned to the lookup list on the country drop down on the Product form.

 

Therefore, I am not sure why using the condition builder would satisfy this request, since I have to build the list of countries on the fly, depending on the user accessing the form?  Also, I am a little confused by this statement : 'You are checking SYS ID on a string field.'  I assume that the script should be returning the sysIDs, not the string value, correct?  (Sorry, I am fairly new at Service Now development :)).

When I change the script include to return sysIDs, they appear to be NULL?!?

countrySysIDs.push( countryrecord.getValue('sysID') );

 

Backgroud script:

var encQry='country.countrySTARTSWITHCanada';
var countrySysIDs = [];
var countryrecord= new GlideRecord('x_tnmu2_nlsn_qem_tbl_Country_Master');
countryrecord.addQuery('country','Canada');
countryrecord.query();

while(countryrecord.next()) {
gs.info(countryrecord.getValue('sysID'));

}

gs.info('sys_idIN,' + countrySysIDs.join(","));

 

Result:

x_tnmu2_nlsn_qem: null
x_tnmu2_nlsn_qem: sys_idIN,

 

Suggestions?  (again, I really appreciate all the support :))