need to restrict the location data on catalog form based on user location.

hari kumar
Tera Contributor

Hi Team,

i have a requirement on the catalog form i have two variables on the catalog form

1)requested_for(reference to user table)

2)location(reference to location table)

 

When ever requested for user is indian location referece variable  need to display few  indian location for selecting the user. for other than indians location field need to populate corresponding user location (in the sys user table).

 

I have written below script

javascript: new Populate_Indian_location().fetchingLocation(current.variables.cr_requested_for);//reference qulifier on the location variable.

 

Script include:

var Populate_Indian_location = Class.create();
Populate_Indian_location.prototype = {
fetchingLocation: function(userdata) {

var grUser1 = new GlideRecord('sys_user');

grUser1.get(userdata);
if (grUser1.isValidRecord())
country = grUser1.location.u_core_country.iso3166_2.getDisplayValue();
gs.log("user country list" + country);
if (country == 'IN') {
var array = [];

var gr2 = new GlideRecord('cmn_location');
gr2.addEncodedQuery('name=AP-IN-Hyderabad-CGSC^ORname=AP-IN-Savli-II^ORname=AP-IN-Lote');
gr2.query();
gs.log("list of records country" + gr2.getRowCount());
while (gr2.next()) {

array.push(gr2.sys_id);

 

}
var sysLoc = array.toString();
return 'sys_idIN' + sysLoc;

 

 

I am not able to get the solution

Could you please Help on this .

 

 

 

 

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@hari kumar Where are you facing the issue? Is your fetching location function not returning the sys Ids? Here is one one improvement I did in your script include.

 

var Populate_Indian_location = Class.create();
Populate_Indian_location.prototype = {
fetchingLocation: function(userdata) {

var grUser1 = new GlideRecord('sys_user');

grUser1.get(userdata);
if (grUser1.isValidRecord())
country = grUser1.location.u_core_country.iso3166_2.getDisplayValue();
gs.log("user country list" + country);
if (country == 'IN') {
var array = [];

var gr2 = new GlideRecord('cmn_location');
gr2.addEncodedQuery('name=AP-IN-Hyderabad-CGSC^ORname=AP-IN-Savli-II^ORname=AP-IN-Lote');
gr2.query();
gs.log("list of records country" + gr2.getRowCount());
while (gr2.next()) {

array.push(gr2.getValue('sys_id'));//Updated this line

 

}
var sysLoc = array.toString();
return 'sys_idIN' + sysLoc;

Whenever, you push a value in an array, always use getValue instead of dot walking otherwise the sys_id of the last record would get repeated on all array indexes.

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@hari kumar Where are you facing the issue? Is your fetching location function not returning the sys Ids? Here is one one improvement I did in your script include.

 

var Populate_Indian_location = Class.create();
Populate_Indian_location.prototype = {
fetchingLocation: function(userdata) {

var grUser1 = new GlideRecord('sys_user');

grUser1.get(userdata);
if (grUser1.isValidRecord())
country = grUser1.location.u_core_country.iso3166_2.getDisplayValue();
gs.log("user country list" + country);
if (country == 'IN') {
var array = [];

var gr2 = new GlideRecord('cmn_location');
gr2.addEncodedQuery('name=AP-IN-Hyderabad-CGSC^ORname=AP-IN-Savli-II^ORname=AP-IN-Lote');
gr2.query();
gs.log("list of records country" + gr2.getRowCount());
while (gr2.next()) {

array.push(gr2.getValue('sys_id'));//Updated this line

 

}
var sysLoc = array.toString();
return 'sys_idIN' + sysLoc;

Whenever, you push a value in an array, always use getValue instead of dot walking otherwise the sys_id of the last record would get repeated on all array indexes.

hari kumar
Tera Contributor

Hi Team,

i have a requirement on the catalog form i have two variables on the catalog form

1)requested_for(reference to user table)

2)location(reference to location table)

 

When ever requested for user is indian location referece variable  need to display few  indian location for selecting the user. for other than indians location field need to populate corresponding user location (in the sys user table).

 

I have written below script

javascript: new Populate_Indian_location().fetchingLocation(current.variables.cr_requested_for);//reference qulifier on the location variable.

 

Script include:

var Populate_Indian_location = Class.create();
Populate_Indian_location.prototype = {
fetchingLocation: function(userdata) {

var grUser1 = new GlideRecord('sys_user');

grUser1.get(userdata);
if (grUser1.isValidRecord())
country = grUser1.location.u_core_country.iso3166_2.getDisplayValue();
gs.log("user country list" + country);
if (country == 'IN') {
var array = [];

var gr2 = new GlideRecord('cmn_location');
gr2.addEncodedQuery('name=AP-IN-Hyderabad-CGSC^ORname=AP-IN-Savli-II^ORname=AP-IN-Lote');
gr2.query();
gs.log("list of records country" + gr2.getRowCount());
while (gr2.next()) {

array.push(gr2.sys_id);

 

}
var sysLoc = array.toString();
return 'sys_idIN' + sysLoc;

 

 

I am not able to get the solution

Could you please Help on this .

 

but form is onloading its not working

 

Below script is onload script:

function onLoad() {
 
var location = g_form.getValue('cr_location');
alert("hi heloo"+location);
 
var ga = new GlideAjax('CTVFitPCDetails');
ga.addParam('sysparm_name', 'getLocationDetails');
ga.addParam('sysparm_location', location);
ga.getXMLAnswer(function(answer){
var response = JSON.parse(answer);
alert(JSON.stringify(response));
 
 
 
//g_form.setValue('cr_street_address', response.street);
g_form.setValue('cr_city', response.city);
g_form.setValue('cr_state', response.state);
g_form.setValue('cr_postal_code', response.code);
g_form.setValue('cr_shipping_country', response.country);
 
});
Script include:
getLocationDetails: function(){
var location = this.getParameter('sysparm_location');
var gr = new GlideRecord('cmn_location');
gr.get(location);
 
var response = {};
 
response.street = gr.street.toString();
response.city = gr.city.toString();
response.state = gr.state.toString();
response.code = gr.zip.toString();
response.country = gr.u_core_country.toString();
 
return JSON.stringify(response);
},
harikumar_0-1690913799273.png

when ever login person is indian or other country person  by default user location based it auto populate. location variable by default :javacript:gs.getUser().getRecord().getValue('location')
but we have removed this default ::javacript:gs.getUser().getRecord().getValue('location')

Because of above onchange script based on location only display on location referece field 

How to fix the issue