How to autopopulate name of logged in user in field on a form?

Aditya Sinha
Tera Contributor

I have a form where a field needs to be populated with name of logged in user.  I am using an on load script however it is not working, (I have checked the UI type to ALL).

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   var gq=new GlideRecord("sys_user");
   //alert((g_form.getReference(g_user.userID)));
   var ab=g_user.userID;
   
   gq.addQuery("user_name",ab);
   gq.query();
   

   g_form.setValue("field name",gq.name);

   
}
11 REPLIES 11

Harish Bainsla
Tera Sage
Tera Sage

function onLoad() {
// Get the current user's name
var userName = g_user.getFullName();

// Set the value of the field
g_form.setValue('field_name', userName);
}

What if 2 users have the same name? Wouldn't sys_id be the unique value for every user?

 

In such cases, it's a good idea to use a more unique identifier, such as the user's email or user ID

Can you help with the code for user id? I have written one above but it doesn't seem to be workinh