Catalog Client Script - Auto-fill value

giuseppefalcone
Tera Contributor

Hey there, 

i've created a new catalog item to make a change department request.

My catalog item has 3 variables (type: reference): user_to_move, approver_user and move_in_department.

On approver_user variable, i've setted "Auto-populate" to question "User to move" and my dot walk path is 'Manager'.

 

Now i want this interaction:

If i choose an user (user_to_move) without Manager (manager field is empty, in user form), the Approver user field in form mustn't be read-only and I can choose a value selecting an user.

Insted, if the user choosen (user_to_move) has a manager, the field 'Approver user' must auto-compile with the name of the user's manager.

 

I've created a Catalog Client Script, but nothing change.

 

giuseppefalcone_0-1704363878965.png

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == 'user_to_move') {
      onChangeUserToMove();
   }

function onChangeUserToMove() {
	var userToMove = g_form.getValue('user_to_move');		
	var manager = getManager(userToMove); 

	var approverField = g_form.getControl('approver_user');

	if (manager !== null) {
		g_form.setValue('approver_user', manager);
		g_form.setReadOnly('approver_user', true);
	} else {
		g_form.setReadOnly('approver_user', false);
		approverField.removeAttribute('disabled');
	}

	function getManager(userID) {								
		var userRecord = new GlideRecord('sys_user');
		userRecord.addQuery('sys_id', userID);
		userRecord.query();
		if (userRecord.next()) {
			return userRecord.getValue('manager');
		}
		return null; 
		}
}
}

 

Thank you.

1 ACCEPTED SOLUTION

Hi,

Try below script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if(newValue!=''){
onChangeUserToMove();
}

function onChangeUserToMove() {
	var userToMove = g_form.getReference('user_to_move',getManager);		

	function getManager(userToMove) {	
        var manager = userToMove.manager;
	if (manager =='' || manager == undefined) {
                g_form.setReadOnly('approver_user', false);	
	} else {
		g_form.setValue('approver_user', manager);
		g_form.setReadOnly('approver_user', true);
	}							
		}
}
}
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

9 REPLIES 9

Anil Lande
Kilo Patron

Hi,

Not sure why you are using GlideRecord and getControl() API's

You can use g_form.setReadonly('field_name', false); to make make field editable (remove disable)

use g_form.getReference to get manager of user.

https://servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Anil Lande
Kilo Patron

Please try below script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if(newValue!=''){
onChangeUserToMove();
}

function onChangeUserToMove() {
	var userToMove = g_form.getReference('user_to_move',getManager);		
	var manager = getManager(userToMove); 
	function getManager(userToMove) {	
var manager = userToMove.manager;
	if (manager !== null) {
		g_form.setValue('approver_user', manager);
		g_form.setReadOnly('approver_user', true);
	} else {
		g_form.setReadOnly('approver_user', false);
	}							
		}
}
}

 

if you are testing it on Service Portal then change UI Type on your client script configuration. You have selected 'Desktop' in UI Type on your client script.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

giuseppefalcone_0-1704365640665.png

i encountered this error on the employee center page when selecting a user on the form.


In the 'approver_user' variable, I removed the read-only check and the 'auto-populate' function.

When I select a user in the form, the 'approver_user' field becomes read-only. If the selected user has a manager, it displays properly. However, if the user doesn't have a manager, the 'approver_user' field remains read-only, and I can't change it.

 

However, i really appreciate your help. Thank you very much.

Hi,

Can you please share your latest script?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande