How to hide variable editor conditionally

Ramkumar Rajend
Tera Expert

Hi Folks,

I need to hide variable editor based on variable values on RITM form level. Can anyone suggest some inputs on this? Also I need to display variable editor for some other conditions.

Regards,

Ram

12 REPLIES 12

The SN Nerd
Giga Sage
Giga Sage

You could also place it in a Form Section and hide the section based on a condition.



function onChange(control, oldValue, newValue, isLoading){


    //this example was run on a form divided into sections (Change form)


    // and hid a section when the "state" field was changed


    var sections = g_form.getSections();


    if (newValue == '2') {


  sections[1].style.display = 'none';


    } else {


  sections[1].style.display = 'block';


    }


}



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hey,



How about if I wanted to hide variables within a section that are blank without hiding the whole section?



Ashley


Kalaiarasan Pus
Giga Sage

First option is to issue different views.. Add the editor in the view you need and remove where you dont ... If that's not possible , this hack could help



var formatterIdentifier = document.getElementsByName('ni.WATERMARK');


var i;


for (i = 0; i < formatterIdentifier.length; i++)


{


if (formatterIdentifier[i].type == "hidden")


{


formatterIdentifier[i].parentNode.style="display:none";


break;


}


}



Note : DOM manipulation is not recommended by service-now. Works on Fuji. Not sure of forward and backward compatibility on other releases.


poyntzj
Kilo Sage

I have just had to do this for a variable editor on the sc_request table


In the end I added a new field so that when the producer runs it can add a u_show_variable_editor = true


if that is true I switch the view to be the one with the variable editor otherwise the one without



I did look at adding a tab and hiding that, but for this requirement decided against it


I have a routine here that is a global Client Script (yeah, I know) that we use for all sorts of manipulation (when I arrived at this client, there were so many versions of code to hide / show / or change colours that were all different and some broke, I had to standardise and make it easier to maintain)
We can then call the code as and where needed


the options for elements and classes are here



function changeElementVisible(elementID, bolTF)


{


      try


      {


              $$('[id=' + elementID+']' ).each(function(elmt) {


                      if (bolTF == true)


                              elmt.show();


                      else


                              elmt.hide();


              });


             


      }


      catch(e){}


}



function changeElementVisible2(elementID, bolBN)


{


      try


      {


              document.getElementById(elementID).parentNode.style.display=bolBN;


                     


      }


      catch(e){}


}



function changeClassVisible(classID, bolTF)


{


      try


      {


              $$('[class=' + classID+']' ).each(function(elmt) {


                      if (bolTF == true)


                              elmt.show();


                      else


                              elmt.hide();


              });


             


      }


      catch(e){}


}



function changeClassVisible2(classID, bolBN)


{


      try


      {


              document.getElementByClassName(classID).parentNode.style.display=bolBN;


                     


      }


      catch(e){}


}




We have an option on the catalog item called u_show_variable editor


In the ui policy, it looks for this field and then runs either




function onCondition() {


      changeClassVisible('veditor_header', true);


      changeClassVisible('veditor_body', true);


}


or


function onCondition() {



      changeClassVisible('veditor_header', false);


      changeClassVisible('veditor_body', false);


}



so we can now determine if we want to show the variable editor dynamically