How to auto populate Corporate Job Title in catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 09:12 PM
Is there a way to auto-populate the "Title" in the catalog item after entering the requested for?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 09:40 PM
Hello @Brian Dean ,
Assuming the variable name is requested_for and title in the catalog item, please find below script
function onChange(control, oldValue, newValue, isLoading) {
// Check if the form is loading or if the field is empty
if (isLoading || newValue === '') {
return;
}
// Get the requested for user's title
var requestedFor = g_form.getValue('requested_for');
// Check if a user is selected
if (requestedFor) {
// Query the user record to get the user's title
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(requestedFor)) {
// Set the Title field with the user's title
g_form.setValue('title', userRecord.getValue('title'));
} else {
// Clear the Title field if user record is not found
g_form.setValue('title', '');
console.log("User record not found for requested user.");
}
} else {
// Clear the Title field if no user is selected
g_form.setValue('title', '');
}
}
Above is the onchange catalog item script with UI type - All and variable name is requested_for.
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 10:26 PM
Hi Sai,
Is it best practice to use Glide Record in the client scripts ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 09:49 PM
Hello @Brian Dean
Yes you can use Auto-populate option in 'Title' variable.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2024 11:34 PM
Hi @Brian Dean
The easiest and preferred way to do this is using the Auto Populate feature. You can make use of Auto Populate feature for your title variable where the dependent question would be your Requested for variable and you can dot walk to the title field.
This way, whenever your requested for field undergoes a change on the catalog item, you will get the title updated.
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.