- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2019 09:01 AM
Hello,
I have a country variable in a request item which is automatically filled thanks to the country of the location (location which is another variable in the same request item).
When location is filled, country is automatically filled.
This works thanks to a catalog client script. It only works on the platform but not in the portal.
I know that g_form.getReference can’t be used in catalog client script and so we need to create a callback function.
I created it but I still have the problem : ok on the platform but not in the portal.
Here is the catalog script I wrote:
function onChange(control, oldValue, newValue, isLoading) {
var caller = g_form.getReference('user_location', doAlert); // doAlert is our callback function
}
function doAlert(caller) { //reference is passed into callback as first arguments
g_form.setValue('user_country',caller.country);
}
Do you have any idea why it is not working in the portal ?
Thanks
Vanessa Heux
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2019 05:34 AM
if above solution not working , replace the code with script include + glide ajax .
try now with below sample code.
Sample Code:
Script Include:
var getCountry = Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCont : function(){
var cnt = this.getParameter('sysparm_user_name');
var gr = new GlideRecord('cmn_location');
gr.get(cnt);
var cmn = new GlideRecord('core_country');
cmn.addQuery('name',gr.country.getDisplayValue());
cmn.query();
if(cmn.next()){
return cmn.sys_id;
}},
type: 'getCountry'
});
Catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var caller = g_form.getValue('user_location'); // make sure about the variable name
var ga = new GlideAjax('getCountry');
ga.addParam('sysparm_name', 'getCont');
ga.addParam('sysparm_user_name',caller);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('user_country',answer); // make sure about the variable name
}
}
Note: Validate the variable name in above client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2019 05:16 AM
I tried also but this does not work, not on the platform nor on the portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2019 03:23 AM
Try this:
Note that you cannot "setDisplayValue" client side - that only works server side
function onChange(control, oldValue, newValue, isLoading) {
g_form.getReference('user_location', function(caller){
g_form.setValue('user_country',caller.country);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2019 05:12 AM
Hello
This is exactly the same, it works on platform but still not on portal !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2019 05:20 AM
Try and create an alert to see if that triggers
function onChange(control, oldValue, newValue, isLoading) {
g_form.getReference('user_location', function(caller){
alert('Inside the callback');
alert('Caller country ' + caller.country);
g_form.setValue('user_country',caller.country);
});
}
Im 100% sure that this works on the ServiceNow under normal circumstances
Either it doesnt run at all because something else stops it.
Else there might be typo's in the variables or something.
when you trigger the client script from the portal then click "F12" and open your browser console.
See if there are any errors what so ever like this (Community error)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2019 05:34 AM
if above solution not working , replace the code with script include + glide ajax .
try now with below sample code.
Sample Code:
Script Include:
var getCountry = Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCont : function(){
var cnt = this.getParameter('sysparm_user_name');
var gr = new GlideRecord('cmn_location');
gr.get(cnt);
var cmn = new GlideRecord('core_country');
cmn.addQuery('name',gr.country.getDisplayValue());
cmn.query();
if(cmn.next()){
return cmn.sys_id;
}},
type: 'getCountry'
});
Catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var caller = g_form.getValue('user_location'); // make sure about the variable name
var ga = new GlideAjax('getCountry');
ga.addParam('sysparm_name', 'getCont');
ga.addParam('sysparm_user_name',caller);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('user_country',answer); // make sure about the variable name
}
}
Note: Validate the variable name in above client script.