Getting userid from username in a Catalog Item

Staxed
Giga Guru

I have a catalog item that uses a reference field to pull in a user's name.  I'm trying to use that reference to populate the userid into another field.  I'm not sure what the syntax is for pulling the userid from that existing field.  Here is the onchange script that I'm currently using.  Any help would be awesome, gotta be something simple I'm missing (hopefully).

 

The first part works (pulling the name from the reference field and adding it to another field on change, but the userID part isn't working as I'm not sure what syntax to be using to pull it.

 

 

 

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

var name = g_form.getDisplayValue('on_behalf_of');
g_form.setValue('requesters_name',name);
	
var netid = g_form.getDisplayValue('on_behalf_of.user_id');
g_form.setValue('requesters_netid',netid);
	
}

 

 

1 ACCEPTED SOLUTION

U_Viswa
Mega Sage

Hi,

I assume you are going set values for free form text variables.

can you try below script

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

   //Type appropriate comment here, and begin script below
   var reqfor=g_form.getReference('requested_for_user',callback);
}
	
	function callback(reqfor){
		if(reqfor)
		g_form.setValue('requester_user_name',reqfor.name);
		g_form.setValue('requester_user_id',reqfor.user_name);
	}

 

Thanks

Viswa

View solution in original post

4 REPLIES 4

Pedro Grilo1
Mega Sage

Hi!

 

I don't think the getDisplayValue takes any parameters. It will get the display value of the current record on the form:

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideRecordAPI#r_Glide...

 

You will need to get the reference of the on_behalf_of and the get the value of the user_name:

g_form.getReference('on_behalf_of').getValue('user_name');

 

I hope it helps!

 

Pedro

this produces a javascript error on form load:

(g_env) [SCRIPT:EXEC] Error while running Client Script "Update Requester": TypeError: can't access property "getValue", g_form.getReference(...) is undefined

 

 

not sure if I maybe implemented it incorrectly?

 

var netid = g_form.getReference('on_behalf_of').getValue('user_name');
	g_form.setValue('requesters_netid',netid);

 

U_Viswa
Mega Sage

Hi,

I assume you are going set values for free form text variables.

can you try below script

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

   //Type appropriate comment here, and begin script below
   var reqfor=g_form.getReference('requested_for_user',callback);
}
	
	function callback(reqfor){
		if(reqfor)
		g_form.setValue('requester_user_name',reqfor.name);
		g_form.setValue('requester_user_id',reqfor.user_name);
	}

 

Thanks

Viswa

this worked perfectly, thank you.