
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2017 10:11 AM
Hello community.
I have a client script for a service catalog item that works perfectly when accessed via the regular ServiceNow UI.
When the same service catalog item is accessed via the Service Portal, the script is not working. We have the UI type set to "both" (see screenshot of script).
Here's what our script does and what we'd like it to do when the service catalog item is accessed via the Service Portal:
After a user selects a person in the reference field #1 (variable we called "LaptopUser"), the associated manager for the user in field #1 is pulled into reference field #2 (variable called "LaptopUserManager").
As mentioend before the client script works great when accessing this service catalog item through the non-service portal UI.
Any help would be most appreciated. Thank you all for your time and help.
//Uses "LaptopUser" value to look up "Manager" value in the sys_user form, add value to "LaptopUserManager".
function onChange(control, oldValue, newValue, isLoading) {
var clearvalue = '';
var id = g_form.getValue('LaptopUser');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if ( user.next() ) {
g_form.setValue('LaptopUserManager', user.manager);
}else{
g_form.setValue('LaptopUserManager', clearvalue); //If "LaptopUser" is null, sets "LaptopUserManager" to null value.
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2017 10:47 AM
Hello Antonio,
I have seen similar issues like that where clients scripts are working in regular view but not on portal. One common thing is portal expects some kind of call back function when you use either query or getreference.
Here is the script using getReference
function onChange(control, oldValue, newValue, isLoading) {
var clearvalue = '';
var user = g_form.getReference('LaptopUser', callback);}
function callback(user){
if(user){
g_form.setValue('LaptopUserManager', user.manager);
}}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2017 10:47 AM
Hello Antonio,
I have seen similar issues like that where clients scripts are working in regular view but not on portal. One common thing is portal expects some kind of call back function when you use either query or getreference.
Here is the script using getReference
function onChange(control, oldValue, newValue, isLoading) {
var clearvalue = '';
var user = g_form.getReference('LaptopUser', callback);}
function callback(user){
if(user){
g_form.setValue('LaptopUserManager', user.manager);
}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2017 11:19 AM
YEP service portal and mobile both require all queries to be asynch.. so they have to be wrapped in a function as he demonstrated... notice in the below script i have wrapped the gr.query in a function so it executes asynchronosly... keep in mind when doing this that it IS asynchronus so anything relying on the returned value needs to be INSIDE the function or you will get VERY strange results.
var gr = new GlideRecord('incident');
gr.addQuery('number', g_form.getValue('related_incident'));
gr.query(function(gr) {
gr.next();
g_form.setValue('u_related_incident_description', gr.short_description);
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2017 09:06 AM
Thank you dvp! I added the reference you provided and my lookup is now working when accessing that service catalog item via the portal.
Thanks so much for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2017 12:03 PM
Hi,
I was trying out a similar thing and the suggestion you gave worked for me and now the script works fine in service portal but has stopped working in non-portal. I did set the UI to All, but it doesn't work there. Can you please help me out.