Service Catalog: onchange client script not working in Service Portal

Antonio Maradi1
Mega Expert

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.

}

}

Set-LaptopUserManager_ClientScript.JPG

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

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);
}


}


View solution in original post

6 REPLIES 6

dvp
Mega Sage
Mega Sage

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);
}


}


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);


});


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.


daaspooja
Kilo Contributor

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.