Create Advanced Script on User Criteria for locations

mkader
Kilo Guru

Hello,

I am trying to create an Advanced Script for User Criteria to make a content item available for certain locations. There are about 100 locations that the content item should be available for. Below is the script that I am using and is not working.

checkCondition();

function checkCondition() {
    var queryString = 'city=Sacramento^ORcity=Wichita';
    var gr = new GlideRecord('cmn_location');
        gr.addEncodedQuery(queryString);
        gr.query();
    if (gr.hasrNext()) {
        answer = true;
    } 
	
	return false;
}

Any idea on what I am doing wrong?

Thanks!

16 REPLIES 16

@Alikutty - Thanks for your response.

 

The location information on sys_user is (empty), and therefore this script is not working. In User Criteria, there is a location field that we can use to set on the Available For. My question is why would the script not work when checking for those locations?

First of all the user location cannot be empty, How does system know what is his location? You need to populate locations on user table if the user critieria should validate the access.

Secondly you wont need the advanced script for validating locations. There is a Location list field on the user criteria which can be updated with all required locations for which your catalog item requires access.

@Alikutty - When I try to search for one of the 2 locations on sys_user table using the search on the location column nothing comes up. When I run a filter on sys_user table for the location column, It opens up the cmn_location table, and from there, I am able to see the location information. The location information on the cmn_location table is titled differently "Market Name". 

I am sorry, but I am new to ServiceNow, so still learning the basics. 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I believe this is what is required.

If logged in user has location city as either Sacramento or Wichita then show the catalog

if yes the update script as below

answer = checkCondition();

function checkCondition() {

var user = new GlideRecord('sys_user');
user.get(gs.getUserID());

var city = user.location.city;

if(city == 'Sacramento' || city == 'Wichita')
return true;
else
return false;

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Thanks for your response.

I just tested out the script and it did not work. The location records on sys_user is (empty). Are there any other work arounds?