Based on the location of the requested for, set a value on the selectbox

mfhaciahmetoglu
Mega Sage

Hello, 

 

I have a form which has the two following fields:

 

requested_for: reference field to sys_user table (this is a variable set)

company: selectbox field, it has (value=) barco_tw question choice.

 

The requirement is that, if the location of the requested for is Taiwan, the company selectbox must be set to barco_tw value.

 

I have written the following simple client script to solve this issue but it does not work.

 

function onLoad() {


Get the value of the location using dot walking
var location = g_form.getValue('requested_for.location');

// Check if the location is Taiwan
if (location === '39089e421bdc2510f8104002cd4bcb3d') {
// Set the value of the "company" field to "barco_tw"
g_form.setValue('company', 'barco_tw');
}

// Attach the onLoad function to the onLoad event
if (window.g_form) {
g_form.addOnLoad(onLoad);
}

 

I also tried instead of sys_id, string 'TAI', it doesn't work either.

 

Any idea why this is not working? Is it the dot walking?

 

Thanks.

 

Best,

Firat

1 ACCEPTED SOLUTION

Siddhesh Gawade
Mega Sage
Mega Sage

 @mfhaciahmetoglu , I tried the below solution on reference type variable for user field. And it is working correctly.

 

 

onChange Client script:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

  var ga = new GlideAjax('CheckUserLocation');
ga.addParam('sysparm_name', 'getLocation');
ga.addParam('sysparm_user_sys_id', g_form.getValue('user'));
ga.getXMLAnswer(ans);
function ans(response) {
        if (response == '25b3d04b0a0a0bb300176b546c22db27') {
            g_form.setValue('company', 'barco_tw');
        }
    } 
}

 

 

Script Include: 

 

var CheckUserLocation = Class.create();
CheckUserLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getLocation: function() {
var userSysId = this.getParameter('sysparm_user_sys_id');
var userGr = new GlideRecord('sys_user');
if (userGr.get(userSysId)) {
	gs.info('userGr.location is '+ userGr.location)
return userGr.location;
}
},
    type: 'CheckUserLocation'
});

 

 

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh

 

View solution in original post

18 REPLIES 18

Chaitanya naram
Kilo Sage

Hi @mfhaciahmetoglu 

You can write an onchange client script on the variable and call script include to get the user location.


var ga = new GlideAjax('GetUserInfo'); // GetUserInfo is the script include name
ga.addParam('sysparm_name','locationName'); // managerName is the function in the script include that we're calling
ga.addParam('sysparm_user_sysid',newValue); // set user to Fred Luddy

ga.getXML(locationParse);
}

// callback function for returning the result from the script include
function locationParse(response) {
var comments = response.responseXML.documentElement.getAttribute("answer");

if(comments==true) g_form.setValue('company','barco_tw');
}


// GetUserInfo script include

var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

managerName: function() {
var usersysId = this.getParameter("sysparm_user_sysid");
var grUser = new GlideRecord('sys_user');
grUser.get(usersysId);

// Build the payload. You can return additional data if needed.
if(grUser.location.name=="Taiwan") return true;

else return false;

},
type: 'GetUserInfo'
});

Thanks & Regards | Chiranjeevi Chaitanya Naram
Kindly mark the answer Correct and Helpful if it helps to resolve your issue.

Hi Thanks a lot.

 

Here is the script include I wrote based on your answer:

mfhaciahmetoglu_0-1704209125947.png

I also tried location.name == 'TAI' 

And here is the client script:

mfhaciahmetoglu_1-1704209179460.png

None of them work. When I choose a user from TAI, company is not updated:

 

@mfhaciahmetoglu sorry, change the method name in script include from managerName to locationName

Thanks & Regards | Chiranjeevi Chaitanya Naram
Kindly mark the answer Correct and Helpful if it helps to resolve your issue.

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @mfhaciahmetoglu ,

 

The incorrect part in the client script is the way you are trying to get the value of the location using dot walking. In ServiceNow client scripts, you cannot use dot-walking directly to get the value of a field from a referenced table.

 

Use GlideAjax to call a script include that will return the value of the location from the server side.

function onLoad() {
// Create a new GlideAjax object
var ga = new GlideAjax('GetLocation');
// Pass the sys_id of the requested_for user to the script include
ga.addParam('sysparm_name', 'getLocation');
ga.addParam('sysparm_user_sys_id', g_form.getValue('requested_for'));
// Get the result asynchronously
ga.getXMLAnswer(function(answer) {
var location = answer;
// Check if the location is Taiwan
if (location === '39089e421bdc2510f8104002cd4bcb3d') {
// Set the value of the "company" field to "barco_tw"
g_form.setValue('company', 'barco_tw');
}
});
}

 

Create a Script Include named 'GetLocation' with the following code(make sure it is client callable). This script include will return the sys_id of the location of the requested_for user.

 

var GetLocation = Class.create();
GetLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocation: function() {
var userSysId = this.getParameter('sysparm_user_sys_id');
var userGr = new GlideRecord('sys_user');
if (userGr.get(userSysId)) {
return userGr.location;
}
return '';
},
type: 'GetLocation'
});

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh

 

I applied this solution but the company field is not updated. Can this be because the requested for is a variable set?