The CreatorCon Call for Content is officially open! Get started here.

Hi, can you pls help me

sheddyshiri
Tera Contributor

This question was asked in an interview :

we have a catalog form, where we have employee, email, company fields so, when employee field is something then email and company fields should auto populate 

9 REPLIES 9

@sheddyshiri 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Mark Manders
Mega Patron

People keep answering with scripting solutions, while the auto-populate has been around for a while. Please use that, as it is an OOB, no code solution, easily added through the form itself.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

M Iftikhar
Tera Sage

Hi @sheddyshiri ,

For this scenario there are two main approaches:

  • OOB Auto-populate (Recommended): If Employee is a reference to sys_user, you can use the Auto populate tab on the Email and Company variables. This will automatically pull the values from the user record without scripting.

  • Scripting Option: If you need more flexibility, you can add an onChange catalog client script on Employee and use getReference() (or GlideAjax + Script Include) to fetch email and company, then set those values.

OOB auto-populate is cleaner and easier to maintain, while scripting is useful if you need custom logic.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

AshishKM
Kilo Patron
Kilo Patron

Hi @sheddyshiri , 

The best to use OOTB 'auto populate' option for email and company field as they both available in User [sys_user] table. 

 

Create Simple Text type field for Email and Company and use dot walk path for email and company value respectively.

 

AshishKM_0-1758890758073.png

 

-Thanks,

AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

pranita-24
Tera Guru

Hi @sheddyshiri 

You can achieve this by using a Catalog Client Script with onChange on the Employee field.

  • When the user selects an employee, use a GlideAjax / g_form.getReference() call to fetch the related details.

  • Then set the values of Email and Company automatically with g_form.setValue().

    example script-

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

    g_form.getReference('employee', function(emp) {
    if (emp) {
    g_form.setValue('email', emp.email);
    g_form.setValue('company', emp.company.name);
    }
    });
    }

    If I answered your question please mark (Correct ) to my answer.

    Thanks you ,
    Pranita D