Query business rule for user based location, country restriction

Mattikus4901
Tera Contributor

I have created a query business rule to restrict viewing of incidents, catalog requests, and catalog tasks based on user location. I need users to only be able to view incidents, catalog requests, and tasks that were created by users with the same location and country code. US locations should be able to see all countries. 

 

Please see the attached code. I could use some help troubleshooting. 

This is a before query business rule.

3 REPLIES 3

Community Alums
Not applicable

HI @Mattikus4901 ,

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Mattikus4901 

 

Try this script.

 

(function executeRule(current, previous /*null when async*/) {
	var userCountry = "";
	var query = "";
	var currentUser = gs.getUserID();
	var userGR = new GlideRecord('sys_user');
	if (userGR.get(currentUser)) {
		userCountry = userGR.location.country + '';
	}
	var userCountrySm = userCountry.toLowerCase();
	if(userCountrySm == 'us' || userCountrySm == 'united states' || userCountrySm == 'usa' || userCountrySm == 'united states of america'){
		return;
	}
	
	var taskType = current.sys_class_name + '';
	
	if (taskType == "incident") {
		query = "ref_incident.caller_id.location.country=" + userCountry;
	} else if(taskType == "sc_req_item"){
		query = "ref_sc_req_item.requested_for.location.country="+userCountry;
	} else if(taskType == "sc_request"){
		query = "ref_sc_request.requested_for.location.country="+userCountry;
	} else if(taskType == "sc_task"){
		query = "ref_sc_task.request_item.requested_for.location.country="+userCountry;
	} else {
		query = "opened_by.location.country="+userCountry;
	}
	current.addEncodedQuery(query);
})(current, previous)

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh