displaying an alert on a record producer when the requester's location has a certain value

alexm3
Kilo Expert

Hello,

I'm currently working on a record producer and I would like to add an alert that will deny submitting the request if the requester is from a certain country, like UK.

I have a variable on the form named 'caller_id"(reference to sys_user table) which contains the requester's details and by default the value is populated with the user currently logged in the instance.

I was thinking to add an onSubmit client script that will display an alert if you're from UK and it will not allow you to submit the request. Unfortunately my scripting knowledge is limited and I don't know exactly what I need to do. Any thoughts would be much appreciated.

Thanks,

Alex

1 ACCEPTED SOLUTION

srinivasthelu
Tera Guru

Hi Alexandru,



You need to create a script include first with client callable enabled.


Screen Shot 2016-02-04 at 2.09.52 PM copy.jpg


Here is the script include code


--------


var LocationUtil = Class.create();


LocationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  isUserFromUK:function()


  {



  var requestor=this.getParameter('sysparm_user_id');


  var gr=new GlideRecord('sys_user');


  if(gr.get(requestor) && gr.location.country=='United Kingdom')


  return true;



  return false;



  },



  type: 'LocationUtil'


});


-----


You need to call that script include from the onsubmit client script. here is the code.



function onSubmit() {


    //Type appropriate comment here, and begin script below


var ga = new GlideAjax('LocationUtil');


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


ga.addParam('sysparm_user_id',g_form.getValue('caller_id'));


ga.getXMLWait();


var answer=ga.getAnswer();


  if(answer=='true')


  {


  alert("You cannot submit the request");


  return false;


  }




}




Hope that helps



Thanks


Srini


View solution in original post

4 REPLIES 4

srinivasthelu
Tera Guru

Hi Alexandru,



You need to create a script include first with client callable enabled.


Screen Shot 2016-02-04 at 2.09.52 PM copy.jpg


Here is the script include code


--------


var LocationUtil = Class.create();


LocationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  isUserFromUK:function()


  {



  var requestor=this.getParameter('sysparm_user_id');


  var gr=new GlideRecord('sys_user');


  if(gr.get(requestor) && gr.location.country=='United Kingdom')


  return true;



  return false;



  },



  type: 'LocationUtil'


});


-----


You need to call that script include from the onsubmit client script. here is the code.



function onSubmit() {


    //Type appropriate comment here, and begin script below


var ga = new GlideAjax('LocationUtil');


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


ga.addParam('sysparm_user_id',g_form.getValue('caller_id'));


ga.getXMLWait();


var answer=ga.getAnswer();


  if(answer=='true')


  {


  alert("You cannot submit the request");


  return false;


  }




}




Hope that helps



Thanks


Srini


Hi Srini,



It works! One more question: If I want to deny multiple countries, not just UK, do I just need to add   if(gr.get(requestor) && gr.location.country=='United Kingdom' || 'Sweden' || Germany) or do I need to change something else?



Thank you!


Alex


Hi Alexandru,



You need to do something like this.



if(gr.get(requestor))


var country=gr.location.country;


if(country=='United Kingdom' ||   country=='Sweden' ||   country=='Germany')


{


return true;


}



Hope this helps


Mark this answer as helpful/correct if it does so.


Srini



Sweet! Thanks!