The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Need to hide a service catalog item if they have any records in a certain table?

ksmithdev
Mega Expert

Hello!

 

Thanks for your time. I am trying to write a onLoad client script that checks a table called u_user_devices, and if it pulls even a single record tied to that user, the catalog item must become hidden on load. If no records are found, they are able to see it, and complete the request. For some reason, I can only ever get it to hide from everyone nomatter what, or be completely visible. I have what I wrote so far below, but I am wondering if anyone can help me. I do not need to pull a specific record from the u_user_devices table, it is just that if they have any records there, they are not allowed to order this item.

 

function onLoad()

{

  var userID = g_user.userName;

  var gr = new GlideRecord('u_user_device');

  gr.addQuery('u_user_name', userID);

  gr.query();

  alert("User : " + userID);

  alert("Username in Table : " + gr.u_user_name);

  if (gr.u_user_name != '') {

  if (gr.hasNext())

  {

  alert("Sys ID of Device User : " + gr.sys_id);

  g_form.setDisplay('lbl_service_req_form',false);

  g_form.setDisplay('shipping_info',false);

  g_form.setDisplay('service_plan',false);

  g_form.setDisplay('user_information',false);

  g_form.setDisplay('carrier_information',false);

  g_form.setDisplay('policy_acceptance',false);

  g_form.setReadonly('not_auth',true);

  x = gel('cart');

  x.style.display = 'none';

  console.log("Hidden");

  }

  if (!gr.next) // Has no device

  {

  g_form.setDisplay('not_auth',false);

  console.log("You should be able to see form.");

  }

 

 

  }

}

 

Thanks for your help!

6 REPLIES 6

ben_hollifield
Tera Guru

Have you considered using an Entitlement Script on the catalog item to hide the item entirely if these conditions are true? It seems unnecessary to present the item to the customer at all if you're only going to hide every field once they attempt to access it.


Jim Coyne
Kilo Patron

I would do it with the Entitlement Script on the catalog item - Service Catalog Access Controls - ServiceNow Wiki



The catalog item would not even be viewable or selectable by the user if the script evaluates to false.



This script should work for you (not tested):


answer = (function() {


  var gr = new GlideRecord('u_user_device');


  gr.addQuery('u_user_name', gs.getUserID());


  gr.query();


  return !gr.hasNext();


})();



You may have to tweak the script a bit.


I am not using roles or anything. The only thing I am doing is if they have ordered an iPad or a mobile device in the past, then they are not allowed to order this router item. The only way of deciding if they can see it or not, is if they have a item in the table assigned to them.


Right, and this is where the Entitlement Script is a perfect fit.   It allows you to determine if the item should be restricted or not based on some custom script and not a role-based restriction.



The way you are wanting to do it is hide the item after they have selected it.   The Entitlement Script allows you to hide it so they can't even see or select it.



And I just realized there is a logic issue with the script I added earlier.   Line 5 should be "return !gr.hasNext()" because you want it to be true if there are no records found.