How to only show the variables that has value in Short Description

Mark Lanning
Tera Guru

I am Tasked to only show the Variables that was Mark as True, or has Values.

Some of my Variables are Reference Fields so the .getDisplayValue() has to be used to add it to the Short Description

Using this type of script works when the Variables are few

current.description = "Action: " + current.variables.action

+ '\n' + "Date Required: " + current.variables.date_required

+ '\n' + "Setup User Like: " + current.variables.set_up_like.getDisplayValue()

+ '\n' + "Requestor's Call Back Number: " + current.variables.Phone_Number

But when having larger numbers of Variables, this basic idea will not work, so now looking for a way to only move the variable to short.description if it has value.

So I was trying something like this:

Boolean getBooleanValue(fieldName)

Returns false if the field value is false or undefined, otherwise returns true.
Parameters:
fieldName - specifies the name of the field.
Returns:
Boolean - the boolean value of the field.

if (getBooleanValue(current.variables.action) != null) {

      current.description = "Action: " + current.variables.action;

      if (getBooleanValue(current.variables.date_required) != null) {

              current.description = "Date Required: " + current.variables.date_required;

              if (getBooleanValue(current.variables.set_up_like) != null) {

                      current.description = "Setup User Like: " + current.variables.set_up_like.getDisplayValue();

                      if (getBooleanValue(current.variables.Phone_Number) != null) {

                              current.description = "Requestor's Call Back Number: " + current.variables.Phone_Number;

                                                                    }

                                                              }

                                                      }

                                              }

But this is not working. Can anyone please tell me what I am doing that is incorrect, or point me in right direction.

I am doing this from the Workflow Editor using the Core Utilities Run Script Box.

The main idea is so the end user will see what was entered from the Request in the Short Description, without showing all the Variables.

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mark,



Here you go. I assume you are using this code in business rule on RITM


1) First get all the variable names in an array belonging to that catalog item


2) Next query RITM table with the sys_id and iterate over this variable names array to determine what values they are having. No need to query if business rule is on RITM and we can directly fetch the values


have condition as per your need. i.e. print the value for the variable only when it is not empty and not 'No' and not 'false' etc



var variablesNames = [];



var item = new GlideappCatalogItem.get(current.cat_item); //sysid of the catalog item cat_item field on RITM refers to Catalog Item


var variables = item.getVariables();


while(variables.next())


{    


variablesNames.push(variables.name.toString()); // pushing into an array all the variable names


}



// now iterate over the variables to know if they have true or not empty



for(var i=0;i<variablesNames.length;i++){



if(current.variables[variablesNames[i]] != '' && current.variables[variablesNames[i]] != 'false' && ){ // this is the condition you can add


gs.print('Variable name is:'+variablesNames[i] + ' value is: ' + current.variables[variablesNames[i]]);


}



}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Kalaiarasan Pus
Giga Sage

You can try using the script in the below and recycle it to fit your need. Just need to add some if condition for additional checks for the required fields.


http://wiki.servicenow.com/index.php?title=Scripting_for_Email_Notifications#Summary_of_Requested_It...


Mark Lanning
Tera Guru

I was not using this from Business Rule, I was doing this in the Workflow


Would I still be able to do this from there?


Hi Mark,



If the workflow is on RITM table nothing will change in the code since current object refers to RITM record on which workflow is running.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

current object is accessible on workflow. So the code remains the same even for workflows or any other server side script.