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

darius_koohmare
ServiceNow Employee
ServiceNow Employee

var gr = new GlideRecord('sys_user');  


gr.get(g_user.userID);


var userloc = gr.location.getDisplayValue();



Performance wise this is not as good as using an AJAX call as noted by Goran.



However even AJAX is less performant than using session client data


Session client data



This is a great blog on the topic: Two ways to Reduce Client Side lookups


well, since it's in a client script I wouldn't recommend doing a gliderecord call.



For this you would need to do for example a GlideAjax call to fetch the user location.


https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_GlideAjaxAPI



//Göran


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


Hi All



Firstly, thanks so much to those of you who have responded, I really appreciate it!



@R0b0, I have created the script include and catalog client script as suggested, but it is not working.   I have actually removed the hide variables part for now (just to see what values are being returned), and nothing is coming back for the 'function hideOpts(response)' part.   I even tried just putting a standard text alert in it and still nothing returned.



I've attached both the script include (which needs to be called hideOpts right?) and the client script below.   The first part of the client script is returning a value for "alert(actualRequesterId)", when uncommented but nothing else.



I'm sure it's probably something really simple like a missing semicolon, and I have tried picking it apart but I am well and truely stuck!   🙂   Can you please advise?



Thanks


Jenny




Client Script



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


}




Script Include



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.u_country;  
  }


  return country;
  }


});