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

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/

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/

Sandeep Rajput
Tera Patron
Tera Patron

@SandeepKSingh Wow, you seem to be interested in answers from only one person. Good Luck!

@Sandeep Rajput I do mark your answer as Helpful and accepted it but yestrday my Servicenow community had some issues the reason its not accepted. you can check other quesytions where ever you replied I marked as helpful.

And please don't  put allegation on anyone.. apart from this forum help people like Ravi  and Atul is doing on Youtube or at many places .