How to Autofill User Reference Field with name from logged in user.

Hm10
Tera Contributor

HI everyone !

I am trying to autopopulate a field name requested_by type reference to sys_user table with the current logged in user. 

I have created a Catalog Client Script with this code. 

 

function onLoad() {
    // Get the current logged-in user
    var currentUser = GlideUser.getUser();

    // Get the field element you want to autofill
    var fieldA = g_form.getControl('requested_by'); // Replace 'field_a' with the actual field name

    // Set the value of Field A to the current user's name
    fieldA.setValue(currentUser.getFullName());
}

But this gives me ID instead. 

I have also set defaultvalue with javascript:gs.getUserID();. Both of them are not working. What am I doing wrong ?

1 ACCEPTED SOLUTION

Gaurav Shirsat
Mega Sage

Hello @Hm10 

is this a Catalog Item Variable or Record Producer Variable?

It should suffices by setting default value of your requested_by variable as javascript:gs.getUserID();

if you are in Record Producer then in the Script Section try producer.requested_by=gs.getUserID();

Thanks and Regards

Gaurav Shirsat

View solution in original post

8 REPLIES 8

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Hm10 ,

 

The syntax you shared in the question description for default value works well in my instance.

 

GunjanKiratkar_0-1693068596657.png

 

You can try checking if there are any other catalog client scripts or UI policy might be clearing that value.

Please let me know if anything else is needed.

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

SN_Learn
Kilo Patron
Kilo Patron

Hi @Hm10 ,

 

Please follow the steps below to populate the logged-in user in a reference field:
1. Right-click on the field and navigate to 'configure dictionary > go to the default value and type as below:
Screenshot 2023-08-26 221613.png

 

2. The second way to setup the same in catalog item is explained in the below link:


Get User Details based on the Logged in user/ User selected in Client Script 

 

Please accept my answer and mark this helpful if it helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Hm10

If you want the client script to set logged-in user value use the below syntax

 

 

function onLoad() {

    g_form.setValue('req_for',g_user.userID);

}

 

 


OR simply you can use  g_form.setValue('req_for',g_user.userID);  anywhere in client side.

For server side:  javascript:gs.getUserID();  in the default value

 

Please mark as Accepted Solution & Helpful 🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Gaurav Shirsat
Mega Sage

Hello @Hm10 

is this a Catalog Item Variable or Record Producer Variable?

It should suffices by setting default value of your requested_by variable as javascript:gs.getUserID();

if you are in Record Producer then in the Script Section try producer.requested_by=gs.getUserID();

Thanks and Regards

Gaurav Shirsat