How to auto populate Corporate Job Title in catalog item

Brian Dean
Tera Expert

Is there a way to auto-populate the "Title" in the catalog item after entering the requested for? 

 

BrianDean_0-1706332306753.png

 

5 REPLIES 5

Sai Shravan
Mega Sage

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.

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi  Sai,

 

Is it  best practice to use Glide Record in the client scripts ?

 

Vishal Birajdar
Giga Sage

Hello @Brian Dean 

 

Yes you can use Auto-populate option in 'Title' variable.

 

VishalBirajdar_0-1706334557600.png

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Amit Verma
Kilo Patron
Kilo Patron

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.