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

Oya Orhan
Giga Guru

Hi Firat,

dot walking doesn't work on the client side. 

Put the code below to onChange for requested_for variable:

   alert(g_form.getValue('requested_for'));
   alert(g_form.getValue('requested_for.location'));

You will see that first alert will bring the data, but not the second one. 

You should use AJAX.

 

Oya

SumanthDosapati
Mega Sage
Mega Sage

Hi @mfhaciahmetoglu 

 

You are correct. Dot walk is the issue.

var location = g_form.getValue('requested_for.location');

You cannot get location this way. You need to use GlideAjax or else getReference with callback.

 

Regards,

Sumanth

If you want to use get Reference then you can try this to get location

 

g_form.getReference('requested_for', getDetails); 

function getDetails(user) { 
  var location = user.getValue('location');
}

Community Alums
Not applicable

Hi @mfhaciahmetoglu ,

 

You can't dot-walk in a client script, you need to use GlideAjax for this, instead - try to avoid using getReference.

 

If this is useful, please mark as helpful.

 

Thanks, Jason.