The CreatorCon Call for Content is officially open! Get started here.

How to set default value of a reference field based on another reference field in the same form

Hari25
Kilo Contributor

I have a reference variable for location of the user and another reference variable within the form to display the 'requested for' user. I need to set the default value of the location variable as the location of the user in 'requested for' variable. Is it possible to get this done using the 'Default value' field in the location reference variable? Kindly help!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

default value for location would work only for logged in user

if you are populating requested for user with logged in user then set default value in location variable as this

javascript: gs.getUser().getLocation();

if someone changes requested for then you need to write onChange client script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

default value for location would work only for logged in user

if you are populating requested for user with logged in user then set default value in location variable as this

javascript: gs.getUser().getLocation();

if someone changes requested for then you need to write onChange client script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you for your prompt response!

onChange client script to populate if someone changes the requested for

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

	if(newValue == ''){
		g_form.clearValue('location');	// location variable name here
	}

	var ref = g_form.getReference('requested_for', callBackMethod);	// requestedFor variable name
}

function callBackMethod(ref){
	if(ref.location)
		g_form.setValue('location', ref.location); // location variable name here
}
Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I had a requirement to also set the users current location as the default value in a reference variable. This worked for me as well. 

Thank you