How to get the Current logdin user using both client side and server side??

SandeepKSingh
Kilo Sage

I have a requirnmnet , on Incident Form caller should be autopopulated based on current logdin user in ServiceNow.

Can anyone help with code ?

4 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Yes you can use g_user API in client script and gs.getUser() will work 
g_form.setValue('caller_id', g_user.userID);

 

 

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@SandeepKSingh You can create an onLoad client script on the incident form to populate logged in user on the incident form.

 

function onLoad() {
    // Check if the form is new (to avoid changing the caller on existing incidents)
    if (g_form.isNewRecord()) {
        // Set the 'Caller' field to the logged-in user
        g_form.setValue('caller_id', g_user.userID);
    }
}

Please mark the solution helpful and accepted solution if it manages to address your question.

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

As you requested :-

PFB full script :-

 

function onLoad() {

if (g_form.isNewRecord()) {

if (!g_form.getValue('caller_id')) {

g_form.setValue('caller_id', g_user.userID);
}
}
}

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

You can you the above in case of Service Catalog also as in Service catalog we have variables which you can access based on catalog client script  and auto-populate current lodging user on the catalog form

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

8 REPLIES 8

Ravi Gaurav
Giga Sage
Giga Sage

Yes you can use g_user API in client script and gs.getUser() will work 
g_form.setValue('caller_id', g_user.userID);

 

 

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Sandeep Rajput
Tera Patron
Tera Patron

@SandeepKSingh You can create an onLoad client script on the incident form to populate logged in user on the incident form.

 

function onLoad() {
    // Check if the form is new (to avoid changing the caller on existing incidents)
    if (g_form.isNewRecord()) {
        // Set the 'Caller' field to the logged-in user
        g_form.setValue('caller_id', g_user.userID);
    }
}

Please mark the solution helpful and accepted solution if it manages to address your question.

@SandeepKSingh My response addresses your question in an efficient way. It should also be an accepted solution.

Soni Tushar
Tera Guru

Hello @SandeepKSingh ,

 

Use this script to auto-populate the Caller field:

function onLoad() {

    // Check if the form is new (Caller field should be empty)
    if (g_form.isNewRecord()) {
        // Set the Caller field with the sys_id of the logged-in user
        g_form.setValue('caller_id', g_user.userID);
    }
}

 

Alternative Approach :

Set a Default Value for the Caller Field -- At the dictionary level have the default value as javascript:gs.getUserID()

 

If you found my rsponse helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!