How to populate one field with information from 2 fields?

astanley
Tera Contributor

Hello all,

I am trying to populate 4 fields on my catalog item with information from the user record. find_real_file.pngThese four fields I have highlighted are all fields that should be populated with information. The "office" field is a reference field for the cmn_location table. The last field, "Phone Number", and is meant to populate information from 2 fields on the sys_user table: business phone and business phone extension with a space and an 'x' between (i.e. 999-999-9999 x9999). The business phone field contains the phone number (ex. 999-999-9999) and the business phone extension contains the extension number (ex. 9999).  The way I have my script set up now just calls the business phone. So the field auto-populates like this: 999-999-9999. My goal is to have all of these fields populated, with the last field being populated with information from two fields. How can I achieve this? Please review my script below:

function onChange(control, oldValue, newValue, isLoading) {

var id = g_form.getValue('ConfirmInfoReqName');

var user = new GlideRecord('sys_user');

user.addQuery('sys_id',id); user.query();

if ( user.next() ) {

g_form.setValue('title', user.title);

g_form.setValue('office_employee', user.location);

g_form.setValue('user_id', user.user_name);

g_form.setValue('phone_number', user.phone);

}

}

(If a picture is easier, see below)

find_real_file.png

Also, which variable does this catalog client script apply to? I am very new with scripting, any assistance would be greatly appreciated.

11 REPLIES 11

Hi,

My apologies, I'm a bit confused where you may be struggling because I provided a GlideAjax cheat sheet link that walks through both the client script and script include and is color coded, with details and explanations and more.

Other than the cheat sheet, I'd recommend reading my response to another person who posted basically the same thing here: https://community.servicenow.com/community?id=community_question&sys_id=1a8dd9acdb870150fd8d2b691396...

I literally did the client script and script include and all that and include screenshots. You can compare yours, to that, and adjust as needed.

Here's also the SN documentation as well, to help for the script include, etc.

https://docs.servicenow.com/bundle/rome-application-development/page/script/ajax/topic/p_AJAX.html

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Jan Cernocky
Tera Guru

Hi,

normally, if you get a data from GlideRecord, you should use getValue or toString() to make sure you are actually working with a string and not a different type.

So you code should look like

g_form.setValue('title', user.getValue('title'));

or

g_form.setValue('title', user.title.toString());

And for your particular phone number you just need to merge 3 strings together

var fullNumber = user.getValue('business_phone') + ' x' + user.getValue('business_phone_extension');
g_form.setValue('phone_number', fullNumber);

 

HOWEVER ...

You should not be using server scripting (query user data) in client script. This may have a huge impact on performance. You would need to use GlideAjax to query particular user record when the field is changed.

I could give you a ready solution but this is an important topic you should be aware of for any future use, so I advise you to check this particular video about it. 

Hello, thank you so much for your assistance! I have modified my script, however, I am still not confident it is correct. I am trying my best to have this completed by today. Is there anything in my script that looks incorrect?

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

	// To set the user title, office, user id, and phone number
	var ga = new GlideAjax("SKX_getUserDetails");
	ga.addParam("sysparm_name","getData");
	ga.addParam("sysparm_user",newValue);
	ga.getXMLAnswer(getDetail);

	function getDetail(result){
		var answer = JSON.parse(result);

		var title = answer.title;
		var office_employee = answer.location;
		var user_id = answer.user_name;
		var fullNumber = user.getValue('business_phone') + ' x' + user.getValue('business_phone_extension');
g_form.setValue('phone_number', fullNumber);

	}
}

Hi,

are you getting the title and other stuff?

Have you created the script include? Can you share it? 

From what you pasted I assume the script include is called

SKX_getUserDetails

and function

getData

You can build the full phone number already in the script include, if you are building it in client script, you can't use 'user.getValue' because user is not defined, that would be answer.business_phone or something you defined in the script include. 

Please share it.

Aman Kumar S
Kilo Patron

Below code snippet should help:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
     var userDetails = g_form.getReference("user_field_name",getUserInfo);// name of field where you have user value

     function getUserInfo(userDetails){
	g_form.setValue("title", userDetails.title);
	g_form.setValue("office_employee", userDetails.location);
	g_form.setValue("user_id", userDetails.user_name);
	g_form.setValue("phone_number", userDetails.phone);
	
	}
}
Best Regards
Aman Kumar