check vibility of variable

r_gissarl_
Mega Expert

Hi,

I need to check a variable in a catalog client script onSubmit(). If the variable is visible, I need to check it. If it is not visible, the script does nothing.

So I need to access to the visibility of the variable, I thought about document.getElementById("IO:2bff02ec51ed21002b74916df91c253b").style.visibility or style.display.

But to get the status, I need to set it before... visibility or display returns nothing if I don't set it before (which is not what I need).

Any idea on how to get the visilibility (true / false) of a variable ?

Thanks.

1 ACCEPTED SOLUTION

ChrisRoyer
Kilo Guru

EDIT: I think the following function will be what you're looking for.



just call this in your client script and you should be good to go. I've only tested this in a few scenarios and directly on the catalog item, haven't looked at a submitted form yet or anything along those lines. I'll try and update if I end up doing any more work on this.



function isVisible(fieldName) {


  //This script does appear to return undefined if the variable name isn't present


  var field = fieldName;


  var id = g_form.resolveNameMap(field); //This will return IO:sys_id


  var clean = g_form._cleanupName(field); //this should return just the sys_id variable


  var nidot = gel('ni.' + id); //checkbox variables are hidden on this element.


  var ctrl = g_form.getControl(id); //not used in this script, but get's the prototypeJS elemnt


  var container = gel('container_' + clean); //getting the element for a container, one we actually don't have to crawl for parent nodes or anything along those lines


  var label = gel('label_' + id); //This should be all other field types. (testing was limited to single/multi line text, reference, and list collector) mileage may vary


  if (container) {


  return container.visible();


  }


  if (nidot) {


  return nidot.parentNode.visible();


  }


  if (label) {


  if (hasClassName(label, 'io_label_container')) {


  return label.visible();


  } else {


  return label.up(1).visible(); //same as label.parentNode.parentNode.visible(), just shorthand.


  }


  }


}


View solution in original post

12 REPLIES 12

Thanks a lot Chris, this is exactly what I was looking for.


Hi Regis,



I am also trying to achieve the same thing and tried calling the function in client script with no luck.



Could you please share the piece of code, which is used to call the above function?



Thanks,


Rohit


Hi Rohit,



Note that in recent version (maybe since Helsinki) we have a g_form function for that and the provided custom function is no longer working. This is how it is called now (in the following exemples, u_mac is the name of the variable when you create it in your variable set or in the catalog item) :


function onSubmit() {


      //Type appropriate comment here, and begin script below


      var mac = g_form.getValue('u_mac');


     


      if(g_form.isVisible('u_mac') && mac != ''){


Here, there is a check for the variable u_mac, and we enter the condition if the field is visible and the value is not empty.




For the old version with the custom version of the function isVisible, the code was very similar :


function onSubmit() {


      //Type appropriate comment here, and begin script below


      var mac = g_form.getValue('u_mac');


     


      if(isVisible('u_mac') && mac != ''){


Here, isVisible is declared in the onSubmit function.


Thank you for the quick update.


Tried g_form.isVisible and it's not working. When checked the docs site, isVisible method is not found, instead found isSectionVisible. Wondering if isVisible still a valid glide form method?



function onSubmit() {


    //Type appropriate comment here, and begin script below


    var mandatoryVar = g_form.getValue('ritm');


var mandatoryVar1 = g_form.getValue('additional_line');



if ((g_form.isVisible('ritm') && mandatoryVar ==" ") || g_form.isVisible('additional_line') && mandatoryVar1 == "")


{


alert("One of the mandatory variables (ritm, Additional line) is blank. Please populate the variable and resubmit the request.");


return false;


}


}


Hi,



I should have precised that we are migrating to Service Portal, and it is supported in Service Portal in Istanbul, Jakarta and Kingston.



But I don't have any problem with the custom function in the post in the CMS (because we support our old CMS also during migration).



If you still have trouble, I think that isSectionVisible applies to a variable set. If you can put your variables in a separate variable set (wihtout UI Policy), you should be able to perform what you need.