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
0 notes
Adding Variables from the Variable Editor into the Description Field
Adding Variables from the Variable Editor into the Description Field
A multi-line text field like the Description field can be leveraged to manipulate variable information
Copying variables into the Description field provides a way to search on and parse through information from catalog item or record producer variables. The reasons you might want this sort of functionality:
Search for variable information from the list view
Parse and manipulate variable information
Save screen real-estate by hiding the variable editor
Variable information does not necessarily need to be added to a multi-line text field either. You can easily parse values out and use them to populate other field types as well, or to store as variables in a script. These are not covered in scope of this article, however, let us know in the comments if this is something you would like to see!
**Note: The following implementation only converts choice and reference values to their display values. Let us know if you would like a more robust version of this and which field types you would like covered!
First, create the below Business Rule on the Task table. If you would like to restrict this Business Rule to only run on a specific table, add a condition where Task type is .
"Copy Variables" Business Rule
Name: Copy Variables
Table: Task
When: display
Script:
(function executeRule(current, previous /*null when async*/) {
//Initialize the scratchpad variable
g_scratchpad.varJSON = '';
var type, name, value, reference, val, item;
//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 variable names and values
if(count > 0){
var varJSON = {};
var table = current.getTableName();
//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);
}
//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);
// Set Order
itemVars.orderBy('sc_item_option.order');
itemVars.query();
while(itemVars.next()){
//Add variable names and values to the varJSON object
type = itemVars.sc_item_option.item_option_new.type.toString();
name = itemVars.sc_item_option.item_option_new.question_text.toString();
val = itemVars.sc_item_option.value.toString();
// Reference
if (type == 8) {
reference = itemVars.sc_item_option.item_option_new.reference.toString();
value = getReferenceDisplayValue(reference,val);
}
// Select Box
else if (type == 5) {
item = itemVars.sc_item_option.item_option_new.sys_id;
value = getChoiceDisplayValue(item, val);
}
// Default
else {
value = val;
}
// Replace blank values with
if (value == '') {
value = '';
}
varJSON = value;
}
}
//All other variables pulled from 'question_answer' table
else{
var producerVars = new GlideRecord('question_answer');
producerVars.addQuery('table_sys_id', current.sys_id);
//Exclude Label and Container variables
producerVars.addQuery('question.type', '!=', 11);
producerVars.addQuery('question.type', '!=', 19);
producerVars.addQuery('question.type', '!=', 20);
// Set Order
producerVars.orderBy('order');
producerVars.query();
while(producerVars.next()){
//Add variable names and values to the varJSON object
type = producerVars.question.type.toString();
name = producerVars.question.question_text.toString();
val = producerVars.value.toString();
// Reference
if (type == 8) {
reference = producerVars.question.reference.toString();
value = getReferenceDisplayValue(reference,val);
}
// Select Box
else if (type == 5) {
item = producerVars.question.sys_id;
value = getChoiceDisplayValue(item,val);
}
// Default
else {
value = val;
}
// Replace blank values with
if (value == '') {
value = '';
}
varJSON = value;
}
}
// Convert varJSON into a string and store the result in the scratchpad
g_scratchpad.varJSON = JSON.stringify(varJSON);
}
// Function to convert reference value to display value
function getReferenceDisplayValue(table,value) {
var displayValue1 = '';
var gr1 = new GlideRecord(table);
gr1.addQuery('sys_id',value);
gr1.query();
if (gr1.next()) {
displayValue1 = gr1.getDisplayValue();
}
return displayValue1;
}
// Function to convert choice value to display value
function getChoiceDisplayValue(item_option,value) {
var displayValue2 = '';
var gr2 = new GlideRecord('question_choice');
gr2.addEncodedQuery('question=' + item_option + '^value=' + value);
gr2.query();
if (gr2.next()) {
displayValue2 = gr2.text + '';
}
return displayValue2;
}
})(current, previous);
Next, create the below Client Script on the Task table.
"Copy Variables" Client Script
Name: Copy Variables
Table: Task
Type: onLoad
Script:
function onLoad() {
// Check for undefined value
if (typeof g_scratchpad.varJSON == 'undefined') {
return;
}
// Copy variable labels and values from g_scratchpad.varJSON from Business Rule "Copy Variables"
if(g_scratchpad.varJSON != ''){
var varJSON = JSON.parse(g_scratchpad.varJSON);
var str = '';
for (var key in varJSON) {
if (varJSON.hasOwnProperty(key)) {
str += key + ": " + varJSON + "nn";
}
}
// Copies the variable labels and values to the Description field
g_form.setValue('description',str);
}
}
The end result should look like this:
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
0 notes
Searching for Text in the Activity Log
Searching for Text in the Activity Log
This article covers how to search the list view for a specific keyword in the activity log
The activity log tracks the history of changes made to a particular record, including changes to information within the record as well as any email notifications that get sent pertaining to that record. Since activity logs contain loads of valuable information, it quickly becomes a necessity to be able to search the activity logs in the list view. This article will show you how to add and search for text in the Journal fields. At the present moment, I don't know if there is a way to enable search for all text found in activity logs.
We've also added a quick tidbit about searching for text in the Variable Editor!
Searching for Text in the Activity Log
Searching for Text in the Variable Editor
Searching for Text in the Activity Log
Add the journal fields you want to add search capabilities for to the list view and click Save:
You should see the journal fields as columns on the list view:
Use the column search since journal fields don't appear in the search dropdown.
Searching for Text in the Variable Editor
The variable editor is a formatter and thus is not configured for search capabilities out-of-box. Configuring search for the variable editor can be achieved by adding the variable information to a multi-line text field and adding the multi-line text field to the list layout.
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
0 notes
Hiding Empty Variables in the Variable Editor (Scoped)
Hiding Empty Variables in the Variable Editor (Scoped)
In this article we will dive into scripting for the variable editor on both the client and server in a scoped implementation
Working with the variable editor in ServiceNow can be challenging. There aren't many ways to manipulate the variable editor without having to code. Therefore the learning curve to implement logic around variables can be steep. Luckily, there are ways to achieve most common business requirements via script which we will discuss. In this article we will discuss how to hide variables on a form's variable editor.
If you are looking to implement this in the global scope, check out this article instead.
Hide Empty Variables in the Variable Editor (Scoped)
If you are having difficulty hiding empty variables in a scoped implementation, try the following.
Create the Business Rule below in the global scope. Replace the in red below with the table you want to hide the variables on.
"Hide Empty Variables" Business Rule (Scoped Implementation)
Name: Hide Empty Variables
Application: Global
Table: Task
When: display
Filter Conditions: Task type is
Script:
//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();
}
Next, add a UI Policy in the global scope. Once again make sure you replace the in red with the table you are hiding the variables on.
"Hide Empty Variables" UI Policy (Scoped Implementation)
Table: Task
Application: Global
Short description: Hide Empty Variables
Conditions: Task type is
Global: True
onLoad: True
Inherit: True
Run Scripts: True
Execute if true:
function onCondition() {
// Check for undefined value
if (typeof g_scratchpad.emptyVars == 'undefined') return;
// Hide all empty variables using the scratchpad object passed from 'Hide Empty Variables' business rule
if(g_scratchpad.emptyVars != ''){
var emptyVars = g_scratchpad.emptyVars.split(',');
for(var i = 0; i g_form.setDisplay('variables.' + emptyVars, false);
}
}
}
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
0 notes
Hiding Empty Variables in the Variable Editor
Hiding Empty Variables in the Variable Editor
In this article we will dive into scripting for the variable editor on both the client and server
Working with the variable editor in ServiceNow can be challenging. There aren't many ways to manipulate the variable editor without having to code. Therefore the learning curve to implement logic around variables can be steep. Luckily, there are ways to achieve most common business requirements via script which we will discuss. In this article we will discuss how to hide variables on a form's variable editor via code. Or, if you're looking to give your users the ability to toggle empty variable visibility on and off, check out this article.If you are looking to implement this within a scoped application, read this article instead.
Hide Empty Variables in the Variable Editor
Much of the time, empty variables don't provide any value to your users while taking up valuable real estate in the variable editor. Hiding empty variables in the variable editor is simple. You should be able to create the below Business Rule and Client Script and paste in the code without having to make any modifications. I can't take credit for this implementation. It was created by Jacob Kimball awhile back and discussed on servicenowguru.First, create the following Business Rule on your table. Make sure it triggers on "Display".
"Hide Empty Variables" Business Rule
Name: Hide Empty Variables
Table: Task
When: displayScript:
(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);
Next, create the following Client Script on the same table.
"Hide Empty Variables" Client Script
Name: Hide Empty Variables
Table: Task
Type: onLoad
Inherited: TrueScript:
function onLoad() {
// Check for undefined value
if (typeof g_scratchpad.emptyVars == 'undefined') return;
// Hide all empty variables using the scratchpad object passed from 'Hide Empty Variables' business rule
if(g_scratchpad.emptyVars != ''){
var emptyVars = g_scratchpad.emptyVars.split(',');
for (var i = 0; i g_form.setDisplay('variables.' + emptyVars, false);
}
}
}
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
0 notes