Toggle Show/Hide Empty Variables
Toggle Show/Hide Empty Variables
Add a checkbox to your form to show or hide empty variables in the variable editor
If you're running into issues with the variable editor taking up too much real estate on your form but need all variables in your variable editor to be visible to your users (even if empty), there are a couple of ways to accomplish this. Read this article if you're simply looking to hide all empty variables unconditionally. Put the variable editor in its own form section Show/hide empty variables with code
Put the Variable Editor in its Own Form Section
This is the easiest and recommended way to save real estate on your form. Simply putting the variable editor in its own form section will allow users to see all variables while giving your users the convenience of not having to scroll through an extra long form. Navigate to the form layout you want to configure > Form view and section > Section > New
Name your new form section Variables and click OK.
Add the Variable Editor from the left side to the right slide of the slushbucket.
**Note: Make sure the Variable Editor is not in any other sections before saving. If it is, remove it before saving the form. When I had the variable editor on two form sections and saved, I wasn't able to pull up the context menu by right clicking on the column header. Try this if you're running into this issue as well. Save Your form should now look like this.
**Note: If your form does not have tabbed form sections, it may be because you only have one form section on your form. If that doesn't work, check that you have the tabbed forms checkbox checked in your system settings.
Show/Hide Empty Variables with Code
If you want to take it a step further, you can add a checkbox which toggles any empty variables in the variable editor. You can even combine putting the variable editor in its own form section and hiding empty variables with code if you want. First create a field on the form called "Hide Empty Variables" Create the following business rule: "Hide Empty Variables" Business Rule Name: Hide Empty Variables Table: Task When: display Script: (function executeRule(current, previous /*null when async*/) { //Initialize the scratchpad variable g_scratchpad.emptyVars = ''; //Check to see if a variable pool exists var count = 0; for(vars in current.variable_pool){ count++; break; } //If a variable pool exists then collect empty variable names if(count > 0){ var emptyVars = ; var table = current.getTableName(); //Query for the empty variables for this record //Catalog item and task variables pull from 'sc_item_option_mtom' table if(table == 'sc_req_item' || table == 'sc_task'){ var itemVars = new GlideRecord('sc_item_option_mtom'); if(table == 'sc_req_item'){ itemVars.addQuery('request_item', current.sys_id); } if(table == 'sc_task'){ itemVars.addQuery('request_item', current.request_item.sys_id); } itemVars.addNullQuery('sc_item_option.value'); //Exclude Label and Container variables itemVars.addQuery('sc_item_option.item_option_new.type', '!=', 11); itemVars.addQuery('sc_item_option.item_option_new.type', '!=', 19); itemVars.addQuery('sc_item_option.item_option_new.type', '!=', 20); //itemVars.addQuery('cat_item', '!=', "New Move Request"); itemVars.query(); while(itemVars.next()){ //Add variable names to the emptyVars array emptyVars.push(itemVars.sc_item_option.item_option_new.name.toString()); } } else{ //All other variables pulled from 'question_answer' table var producerVars = new GlideRecord('question_answer'); producerVars.addQuery('table_sys_id', current.sys_id); producerVars.addNullQuery('value'); //Exclude Label and Container variables producerVars.addQuery('question.type', '!=', 11); producerVars.addQuery('question.type', '!=', 19); producerVars.addQuery('question.type', '!=', 20); producerVars.query(); while(producerVars.next()){ //Add variable names to the emptyVars array emptyVars.push(producerVars.question.name.toString()); } } //Store the result in the scratchpad g_scratchpad.emptyVars = emptyVars.join(); } })(current, previous); 3. Create the following Client Script: "Toggle Show/Hide Empty Variables" Client Script Name: Toggle Show/Hide Empty Variables Table: Requested Item (sc_req_item) Type: onChange Field name: Hide Empty Variables Script: function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading) { return; } // Check for undefined value if (typeof g_scratchpad.emptyVars == 'undefined') return; var emptyVars; // Hide all empty variables using the scratchpad object passed from 'Hide Empty Variables' business rule if(g_scratchpad.emptyVars != '') emptyVars = g_scratchpad.emptyVars.split(','); if (typeof emptyVars != 'undefined') { if (newValue == 'true') { for (var i = 0; i g_form.setDisplay('variables.' + emptyVars, false); } } else { for (var j = 0; j g_form.setDisplay('variables.' + emptyVars,true); } } } 4. Test out the functionality by checking and unchecking the "Hide Empty Variables" checkbox. When checked, it should hide all variables without values.
When unchecked, it should show all variables again including empty ones.
Let us know if there’s anything we missed or you would like us to expand upon in the comments! At the time of this writing, we are on the London release. The information presented in this article may not apply to your instance if you are on a different version of ServiceNow. Read the full article
















