We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to get the loggedin user location as default in scoped app

rahul16
Giga Guru

I have a form in the scoped app. 

rahul16_0-1665147649166.png

The requested for field gets auto-populated when I use this in the default field 

rahul16_1-1665147793041.png

 

But for the location field it is not working 

rahul16_2-1665147842966.png

what is wrong with the javascript code

1 ACCEPTED SOLUTION

Murthy Ch
Giga Sage

Hi @rahul16 

Yes it will only work in global application. It won't work for scoped application.

SO, please try below logic in default value field:

javascript: var loc; var gr=new GlideRecord("sys_user"); gr.addQuery("sys_id",gs.getUserID()); gr.query(); if(gr.next()) { loc=gr.location;} loc;

See  below screenshot for your reference:

MurthyCh_0-1665149037074.png

Hope it helps..

 

Thanks,
Murthy

View solution in original post

4 REPLIES 4

Not applicable

Hi @rahul16 ,

gs.getUser().getLocation(); should fetch you with the correct location details.

Check if the user(rahul p) which you are referring has "Location" in it's user record.

yes it has a location I created the form in the scoped app not global app does it work only on global 

rahul16_0-1665148726412.png

 

Not applicable

HI @rahul16 ,

Just like gs.getUser().getDepartmentID() , for Location also , i don't think it works for Scoped App and gs.getUser().getLocation(); would work for Global Scope.

For scoped App, you will require a custom a custom Table to store the location, then you can refer it.

Murthy Ch
Giga Sage

Hi @rahul16 

Yes it will only work in global application. It won't work for scoped application.

SO, please try below logic in default value field:

javascript: var loc; var gr=new GlideRecord("sys_user"); gr.addQuery("sys_id",gs.getUserID()); gr.query(); if(gr.next()) { loc=gr.location;} loc;

See  below screenshot for your reference:

MurthyCh_0-1665149037074.png

Hope it helps..

 

Thanks,
Murthy