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

daaspooja
Kilo Contributor

Hi,


I found the solution.



Thank you so much for the help.


Could you please tell me what is the solution for this?