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

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try using javascript: gs.getUser();

 

piyushsain
Tera Guru
Tera Guru

Hi,

Please try 

g_form.setValue('requested_by',g_user.getUserID()); 

or 

fieldA.setValue(g_user.getUserID()); 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

Ramya Addala
Tera Contributor

Hi 

 

Make client script inactive and keep default value of requested by field as  javascript:gs.getUserID()

 

Thanks,

Ramya Addala 

Harish Bainsla
Tera Sage
Tera Sage

Try thi s

function onLoad() {
var currentUser = GlideUser.getUser();
var fieldA = g_form.getValue('requested_by');

fieldA.setValue(currentUser.getID());
}

if you get answer please mark helpful and accept solution