Reference Qualifier to filter locations based on User Location

mattfollowell
Tera Expert

I have a variable on a catalogue item that references the locations table. I want that variable to only list locations whose parent = the signed in user's location. EXCEPT if the signed in User's location = MG. If the signed in user's location is MG then I want the variable to list all locations on the table.

function advloc() {
var locPar;
var userLoc='';
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',gs.getUserID());
gr.query();
    while(gr.next()){
        userLoc=gr.location;
    }
    if(userLoc.includes("MG")){
        locPar = "";
    }
    else {
        locPar = "parent = userLoc";
    }
return locPar
}

I think it is not working because it returns the name of the location and not the Sys_Id, but I'm not 100%.

1 ACCEPTED SOLUTION

You're on the right track. That script belongs in a script include that is client callable:

And in your reference qualifier you would just do:

new getLocation().locations();

View solution in original post

39 REPLIES 39

Ah yes. So just waiting for the other test case when it's "MG". Though looking at the scripting, should be fine.

@mattfollowell if it works fine, don't forget to close this topic. The reply with Elijah script would be most appropriate to mark as correct in that case.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

So I just reversed the return values and that gives me exactly what I am looking for.

Thank you both for your help with this and sticking with me for 4 hours.

Hmmm maybe you are using now:

== -1

Can you use:

> -1

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

As I already mentioned: gr.location is a sys_id. So indexOf("MG")... no go!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Right, I tried replacing it with the sys_id of MG instead of the string value as well, but still no luck.