How to populate one field with information from 2 fields?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 01:57 PM
Hello all,
I am trying to populate 4 fields on my catalog item with information from the user record. These 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)
Also, which variable does this catalog client script apply to? I am very new with scripting, any assistance would be greatly appreciated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 02:13 PM
Hi,
It's not recommended or best practice to use GlideRecord, which is a server side API in the client (so, in client script).
Instead, you'd want to utilize GlideAjax to communicate from the client, to the server, then return that information back to the client.
Here's a cheat sheet to help you.
You'd create an onChange client script on the variable that is...changed...that would kick this whole thing off.
Please attempt yourself and if you get stuck on a particular piece, let us know specifically, and we can help further.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 03:26 PM
Thank you so much! As I am very new to scripting, I was unaware of this mistake and I appreciate the link. I have modified my script below. Please offer any assistance you can as it would be greatly appreciated.
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 06:43 AM
Hi,
Thanks for providing your script. Great job jumping right in.
As you've seen from the other replies about using GlideAjax, they're all saying the same thing I did, haha. I say that because this requires a bit more effort to do, but you'll be setup for the future getting used to doing it.
As far as your script, everything looked mostly fine until the last part where you're creating that fullNumber variable, you still used "user.getValue" and really that information should be coming back from your script include that you instantiated via GlideAjax.
Also, you didn't provide your script include script at all, so we can't really help?
You'd need to make sure that before you started writing the script, you clicked the "Client" checkbox on the script include:
SKX_getUserDetails
Please include that script, you'd always want to try troubleshooting with something like this:
// 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 = answer.business_phone + ' x' + answer.business_phone_extension;
alert("title: " + answer.title + " office_employee: " + office_employee + " user_id: " + user_id + " fullNumber: " + fullNumber);
g_form.setValue('phone_number', fullNumber);
}
If the alert comes back good to go, then you can start to setValue on all those fields.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 01:53 PM
Is there anywhere I can get more information to create my script include from scratch? I have no idea where to begin, I can share a template for my script include and my updated client script, however they are not populating the correct fields. (Phone field is being populated by the title, other fields are saying "undefined")
Script include:
var SKX_UserDetails = Class.create();
SKX_UserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function(){
var title = '';
var phone = '';
var officeemp = '';
var usrid = '';
var sys = this.getParameter('usr_id');
var usr = new GlideRecord('sys_user');
usr.addQuery('sys_id',sys);
usr.query();
if(usr.next()){
title = usr.title;
phone = usr.phone_number;
officeemp = usr.office_employee;
usrid = usr.user_id;
}
return title+','+phone+','+officeemp+','+usrid;
},
type: 'SKX_UserDetails'
});
I am unsure how to move forward. Where can I get more information on each function? Thanks so much for your help!
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('SKX_UserDetails');
ga.addParam("sysparm_name", "getUserDetails");
ga.addParam("usr_id", g_form.getValue('user_1'));
ga.getXML(getResponse);
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer');
var val_split = values.split(',');
g_form.setValue('phone_number',val_split[0]);
g_form.setValue('title',val_split[1]);
g_form.setValue('office_employee',val_split[2]);
g_form.setValue('user_id',val_split[3]);
}
}
These were shared with me by my colleague, however, he could not explain due to lack of time. Where can I go to learn more to know how to complete this?