Hide variables on order guide based on user location

jennytaylor
Kilo Contributor

Hi All

I have a New Starter order guide which is used by 25 different countries.   Some of the countries would like me to hide certain options (for example the Mini Keyboard is not available in Russia or Costa Rica), and I would prefer not to create separate guides for each country.   What I would like to do is hide the variables based on the location of the requestor.   The problem I have is that we do not capture the requestor information on the order guide when it is first loaded (see screenshot below) so I can't use something like g_form.getvalue etc.

find_real_file.png

I have started creating the onload client script below, but I need help with what to enter in the xxxxx part to pull through the requestor location!   My scripting knowledge is not great, so apologies in advance if this is an obvious question.

function onLoad() {

var userloc = xxxxx;

if(userloc == 'Russia' || userloc == 'Costa Rica')
       
  {  
  g_form.setVisible("variables.mini_keyboard", false);  
}  
}  

If you could also confirm if the " g_form.setVisible("variables.mini_keyboard", false)" part will work, or advise a completely different way to achive, this I would be very grateful.

Any help you could give me would be very much appreciated.

Many thanks

Jenny

1 ACCEPTED SOLUTION

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Jenny



I agree with Goran


In order to obtain the user's location you need to create a async ajax call and a script include.


On your client script you need something like this



function onLoad() {


    //Type appropriate comment here, and begin script below



  var actualRequesterId = g_user.userID; //Supposing the actual user logged is also the requester


  //alert(actualRequesterId);



  var ga = new GlideAjax('myOrderGuideUtils');


  ga.addParam('sysparm_name', 'getUserLocation');


  ga.addParam('sysparm_usrId',actualRequesterId);


  ga.getXML(hideOpts);


   


}




function hideOpts(response) {


      var usrLoc = response.responseXML.documentElement.getAttribute("answer");


      //alert(usrLoc); //Uncomment in case you want to check the value returned from the script include


  if(usrLoc == 'Russia' || usrLoc == 'Costa Rica') {  


      g_form.setDisplay("variables.mini_keyboard", false);


  }


}







Than create a script include like this



myOrderGuideUtils___ServiceNow.png



var myOrderGuideUtils = Class.create();


myOrderGuideUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getUserLocation: function(){



  var country = '';


  var usrId = this.getParameter('sysparm_usrId');



  var usr = new GlideRecord('sys_user');


  usr.addQuery('sys_id', usrId);


  usr.query();


  if(usr.next()){



      //supposing the country is populated for each location


      country = usr.location.country;  


  }



  return country;


  }




});



Try and let me know


Cheers



R0b0


View solution in original post

9 REPLIES 9

Have you checked the "client callable" on the script include?



//Göran


Followup question... are you using a custom country field on location since you have u_country? OOB field name is just "country".


Hi Goran



Yes client callable is checked, and yes our country field is called u_country (not sure why, it was implemented before I joined).   However I did try it without the u_ and it still didn't work!



Thanks


Jenny


Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Jenny



The code has been tested on my instance and works properly.


Of course if you have u_country on your instance than you have to change the script.


About this u_country field...is this create on the user table or the location table ?


Because if this field has been created directly on the sys_user table the script include must be changed into



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


  getUserLocation: function(){


  var country = '';
  var usrId = this.getParameter('sysparm_usrId');


  var usr = new GlideRecord('sys_user');
  usr.addQuery('sys_id', usrId);
  usr.query();
  if(usr.next()){


      //supposing the country is populated for each location
      country = usr.u_country;  
  }


  return country;
  }


});



Cheers


R0b0


Hi R0b0



I finally have this working!   I realised this morning that I had called the client script 'hideOpts' and not 'myOrderGuideUtils' as you initially indicated.   Apologies for this, my only excuse is that it was a long week! 🙂   The only slight issue I have is that it is returning the sys id for the country, not the display name.   I don't want to take up any more of your time (you have helped me enough!) so I will just use these rather than the names.



Thanks again for all your help!



Jenny