How to display a Reference variable as "Last Name First Name" in a Record Producer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi everyone,
I have a Reference variable in a Record Producer that points to the sys_user table.
Currently, the selected user is displayed as:
Ahnaf Sheik
However, I want it to be displayed in the following format:
Sheik Ahnaf (Last Name First Name)
The variable label is already "Report Entered By (Last Name, First Name)", but the reference field still displays the user's display value (First Name Last Name).
Is there a way to configure the reference variable or reference qualifier so that the selected value is displayed as Last Name First Name instead of First Name Last Name? Is this possible without changing the global display value of the sys_user table?
Any suggestions or best practices would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hello @SatyamS81441163 ,
Yes this can be done using client script + glide ajax :
client script (onchange) :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('setName');
ga.addParam('sysparm_name', 'getFormattedName');
ga.addParam('sysparm_user_id', newValue);
ga.getXMLAnswer(function(displayName) {
g_form.setValue('user', newValue, displayName);
});
}
Script Include :
var setName = Class.create();
setName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getFormattedName: function() {
var userId = this.getParameter('sysparm_user_id');
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) {
return gr.getValue('last_name') + ' ' + gr.getValue('first_name');
}
return '';
},
type: 'setName'
});
So this will setting the name as per in your desired format :
If my response helped mark as helpful and accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
31m ago - last edited 31m ago
1) If your primary concern is helping users find records by last name while typing , you can add auto-complete columns via Variable Attributes. (NO CODE)
- Navigate to your reference variable,
- switch to the Advanced view under Related Links,
- and add this string into the Variable attributes field:
ref_auto_completer=AJAXTableCompleter,ref_ac_columns=last_name;first_name,ref_ac_columns_search=true
2)
If your functional requirements mandate keeping the variable type strictly as a Reference field, you can leverage an onChange Catalog Client script.
- Set the Type to onChange.
- Select your reference variable in the Variable name field.
- Set the UI Type to All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('YOUR_VARIABLE_NAME', function(userRecord) {
if (userRecord) {
var lastName = userRecord.last_name;
var firstName = userRecord.first_name;
var customDisplay = lastName + ", " + firstName;
g_form.setValue('YOUR_VARIABLE_NAME', newValue, customDisplay);
}
});
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti