Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Are you getting the value for requested for in the client script ? try checking with logging.

Hi, I added the following line and checked with script debugger but I cannot see any of the jslog. Perhaps, I do something wrong. Do you have any suggestions?
 
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'));
var test = requested_for;
 jslog('can you see thiiiss' + test);
// 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');
}
});
}

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

 

Hello @mfhaciahmetoglu , 

 

Did you tried this solution, it should resolve your problem. Maybe the variable set is the issue, I will suggest make a reference type variable and try this way.

Hi Siddhesh,

 

I tried with a reference field named "user"  but still no response at all. I am not sure if it is triggered. How I can log this?