Catalog Item getting SYS ID instead of their actual item name

Peter Williams
Kilo Sage

Good Day everyone,

i created a catalog item that is used for internal changes to an employee.

In my form i have a reference field linked to the sys_user table and when you choose a person their information will get populated into single text fields via a client script.

What is happening, the person Manager (linked to the sys_user table) and their Location(linked to the cmn_location table) is only coming back with the Sys_ID as seen below:

 

PeterWilliams_0-1692795441778.png

 

The script i am using is the following:

PeterWilliams_1-1692795503846.png

 

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

   var userDetails = g_form.getReference('preferred_name', myFunc);

   function myFunc(userDetails){
    g_form.setValue('current_title', userDetails.title);
    g_form.setValue('current_persona', userDetails.u_persona);
    g_form.setValue('current_share_team', userDetails.u_share_team);
    g_form.setValue('current_department', userDetails.department);
    g_form.setValue('current_office_workstation_id', userDetails.u_desk_location);
    g_form.setValue('current_location', userDetails.location);
    g_form.setValue('current_phone_number', userDetails.phone);
    g_form.setValue('current_manager', userDetails.manager);

   }
   
}
 
 
how can i modify the manager and location field to read the Name instead of the sys_id?
2 ACCEPTED SOLUTIONS

Peter Williams
Kilo Sage

I was able to figure it out,

 

here is the updated code for anyone to use.....

 

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

   var userDetails = g_form.getReference('preferred_name', myFunc);

   function myFunc(userDetails){
    var locInfo = new GlideRecord('cmn_location');
    locInfo.get(userDetails.location);

    var userInfo= new GlideRecord('sys_user');
    userInfo.get(userDetails.manager);

    g_form.setValue('current_title', userDetails.title);
    g_form.setValue('current_persona', userDetails.u_persona);
    g_form.setValue('current_share_team', userDetails.u_share_team);
    g_form.setValue('current_department', userDetails.department);
    g_form.setValue('current_office_workstation_id', userDetails.u_desk_location);
    g_form.setValue('current_location', locInfo.name);
    g_form.setValue('current_phone_number', userDetails.phone);
    g_form.setValue('current_manager', userInfo.u_preferred_name);

   }
   
}

View solution in original post

Try this:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var userDetails = g_form.getReference('preferred_name', myFunc);
	
	function myFunc(userDetails){
		
		var loc = new GlideRecord('cmn_location');
		loc.addQuery('sys_id', userDetails.location);
		loc.query(getLocation); 
		
		function getLocation(loc){
			loc.next();
			
			var man = new GlideRecord('sys_user');
			man.addQuery('sys_id', userDetails.manager);
			man.query(getManager); 
			
			function getManager(man){
				man.next();
				
				g_form.setValue('current_title', userDetails.title);
				g_form.setValue('current_persona', userDetails.u_persona);
				g_form.setValue('current_share_team', userDetails.u_share_team);
				g_form.setValue('current_department', userDetails.department);
				g_form.setValue('current_office_workstation_id', userDetails.u_desk_location);
				g_form.setValue('current_location', userDetails.location, loc.name);
				g_form.setValue('current_phone_number', userDetails.phone);
				g_form.setValue('current_manager', userDetails.manager, man.u_preferred_name);
			}
		}	
	}
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

17 REPLIES 17

it worked but with a slight change,

the code should be this:

 

                g_form.setValue('current_location', loc.name);
                g_form.setValue('current_phone_number', userDetails.phone);
                g_form.setValue('current_manager', man.u_preferred_name);
 
we didnt need to ref to the userDetails when setting the values as it will override the display values with the SYS ID

here is the completed code for anyone to use:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    var userDetails = g_form.getReference('preferred_name', myFunc);
    
    function myFunc(userDetails){
        
        var loc = new GlideRecord('cmn_location');
        loc.addQuery('sys_id', userDetails.location);
        loc.query(getLocation); 
        
        function getLocation(loc){
            loc.next();
            
            var man = new GlideRecord('sys_user');
            man.addQuery('sys_id', userDetails.manager);
            man.query(getManager); 
            
            function getManager(man){
                man.next();

                var dep = new GlideRecord('cmn_department');
                dep.addQuery('sys_id', userDetails.department);
                dep.query(getDepartment); 

                function getDepartment(dep){
                    dep.next();
                                
                    g_form.setValue('current_title', userDetails.title);
                    g_form.setValue('current_persona', userDetails.u_persona);
                    g_form.setValue('current_share_team', userDetails.u_share_team);
                    g_form.setValue('current_department', dep.name);
                    g_form.setValue('current_office_workstation_id', userDetails.u_desk_location);
                    g_form.setValue('current_location', loc.name);
                    g_form.setValue('current_phone_number', userDetails.phone);
                    g_form.setValue('current_manager', man.u_preferred_name);

                }
            }
        }   
    }
}

Jim Coyne
Kilo Patron

Your solution could be simplified by using Reference variables instead of the Single Line Text ones I figure you are using to display the info.  That's forcing you to use a Client Script that is way to complicated and going back to the server 4 different times to get some data.

 

A simpler Client Script like so could be used:

 

function onChange(control, oldValue, newValue, isLoading) {
    //ignore when we first load the form
    if (isLoading) {
        return;
    }

    //clear the user details if the User field is cleared
    if (newValue == "") {
        g_form.setValue("title", "");
        g_form.setValue("department", "");
        g_form.setValue("location_text", "");
        g_form.setValue("location", "");
        return;
    }

    //display the User details when an actual User record is selected
    var userRecord = g_form.getReference("user", displayUserDetails);

    function displayUserDetails(userRecord) {
        g_form.setValue("title", userRecord.title);
        g_form.setValue("department", userRecord.department);
        g_form.setValue("location_text", userRecord.location);
        g_form.setValue("location", userRecord.location);
    }
}

 

 

JimCoyne_0-1692804148285.png

 

Using Reference fields mean you do not need to get the display name, just the sys_id, which is what is available from the User record, and what was causing you issues in the first place.  Unfortunately, this means a few round trips back to the server behind the scene to get the display name as well, but at least your code is simple and clean.  Plus it might be more useful having reference fields for your Flow or Workflow later on.

 

The absolute best way of doing it would be with a GlideAjax call that gets all the information so you can populate the fields in the most efficient manner possible.  That means only 1 server call, improving performance, which is something we all need to keep an eye on at all times.