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

To change it into text, add: gr.location.getDisplayValue()

I will test your script on my PDI.

Also note what I wrote about:
>>> Do note: this checks if it contains MG. So what if there is a location "AMG"... not what your where after, though still matches. So a small bug here to fix!

So maybe checking on sys_id might be more accurate?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Just added  indexOf('MG') > -1 to your script
+ getDisplayValue()
+ "parent=" + userLoc

Works here. Though... not as a reference qualifier. Because that should be parent=some_sys_id and not userLoc which is a string.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

I might be missing something. Does getDisplayValue replace UserID?

No with gr.location.getDisplayValue() you could get your hands on the Display Value of the Location. That way you could check "MG". With just gr.location, then it's a sys_id, so checking on "MG" would simply not work.

Though like I mentioned, maybe you actually would like to check on sys_id instead of "MG". What if there's a Location "OMG"... "MG" would then also match! Which actually isn't what you wanted, so buggy. Checking on the sys_id which is unique would prevent this.

Though then obviously, userLoc would be a string instead of a sys_id... while if using parent= on the ref qualifier, this should be a sys_id. So you've got some mix-ups in type here.

I see Elijah edited his post, so probably he now also noticed the type mix-up. Therefore he added an additional var.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Jaspal Singh
Mega Patron
Mega Patron

Hi Matt,

 

Can you try below.

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.indexOf("MG")>-1){
        locPar = "";
    }
    else {
        locPar = userLoc;
    }
return locPar
}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.