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

giuseppefalcone_0-1704366902235.png

It's the script that you suggested to me

Please comment line no 13

var manager = getManager(userToMove); 

 

This line is not required. I forget to remove that.

 

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

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

thank you! it's working 🙂 
I really appreciate your help

Glad to help!

Happy Learning !

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