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

Can you share me your scripts?

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('requested_for'));
ga.getXMLAnswer(ans);
function ans(response) {
        if (response == '39089e421bdc2510f8104002cd4bcb3d') {
            g_form.setValue('company', 'barco_tw');
        }
    }
}


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'
});

function ans(response) {        - After this line try putting alert  as  - alert('response is '+ response)  and let me know what value it gives you.

Also let me know are you getting this info 

    gs.info('userGr.location is '+ userGr.location)