Default value for Reference variable to auto-populate current logged-in user in Service Portal

AzeemullahA
Tera Contributor

Hi Everyone,

I have a Catalog Item with a Reference variable called Traveler Name that points to the user table. I want this field to automatically show the currently logged‑in user when the item is opened in Service Portal. I have tried setting a default value but it does not consistently work in Service Portal. Can someone please confirm if there is a supported way to set a default value so that the current user automatically appears or if using a Catalog Client Script on load is the recommended and reliable approach? Thank you.

2 ACCEPTED SOLUTIONS

ShouryaD
Tera Contributor

Hello Azeem, 

If possible then instead of going with the reference type variable you can go with requested for variable which will automatically populate currently logged in user. It references to the sys_user table itself.

 

Thanks! 

View solution in original post

DiveshTyagi
Mega Guru

Hi @AzeemullahA ,

 

  • Use onLoad Catalog Client Scripts or dynamic defaults like “current user,” “current date,” or values dependent on other variables.

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Why not just default value?

  • Default values on reference variables may work in the native UI but are not consistently honored in Service Portal.
  • Client scripts run reliably in both UI and Portal, making them the best practice for dynamic defaults.
function onLoad() {
    // Get the logged-in user sys_id
    var currentUser = g_user.getUserID;

    // Set Traveler Name variable to current user
    g_form.setValue('traveler_name', currentUser);
}

 

  • g_user.getUserID : returns the sys_id of the logged‑in user.
    • g_form.setValue() : sets the Reference variable to that sys_id, so the user’s display name will appear automatically.

 

--------------------------------------------------------------------------------------------------------------------------------------------

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

 

 

 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron

The recommended approach is to use the Default Value

BradBowman_0-1768649664801.png

Is this what you tried?  I have never heard of this not consistently working in Service Portal.  What are the conditions under which this is not working for you - only with certain users, browser, computer, or always when impersonating a certain user...?

 

You can use an onLoad Catalog Client Script as an alternate approach, but really shouldn't need to.

function onLoad() {
	g_form.setValue("variable_name", g_user.getUserID()); //use your variable name
}

 

Yaa I know Brad,

             But i want to perform this through attributes. I know it is possible through client script.

ShouryaD
Tera Contributor

Hello Azeem, 

If possible then instead of going with the reference type variable you can go with requested for variable which will automatically populate currently logged in user. It references to the sys_user table itself.

 

Thanks! 

DiveshTyagi
Mega Guru

Hi @AzeemullahA ,

 

  • Use onLoad Catalog Client Scripts or dynamic defaults like “current user,” “current date,” or values dependent on other variables.

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Why not just default value?

  • Default values on reference variables may work in the native UI but are not consistently honored in Service Portal.
  • Client scripts run reliably in both UI and Portal, making them the best practice for dynamic defaults.
function onLoad() {
    // Get the logged-in user sys_id
    var currentUser = g_user.getUserID;

    // Set Traveler Name variable to current user
    g_form.setValue('traveler_name', currentUser);
}

 

  • g_user.getUserID : returns the sys_id of the logged‑in user.
    • g_form.setValue() : sets the Reference variable to that sys_id, so the user’s display name will appear automatically.

 

--------------------------------------------------------------------------------------------------------------------------------------------

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