Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hideing variables in the Incident Variable Editor

Sam Ogden
Tera Guru

Hi All,

I have created a record producer to be used on a customer portal to create incidents.   This has many variables, but the answer to 'what is the incident related to?' (inc_rel_to) decides which variables appear to the user to be answered.

I have add the Incident Variable Editor to the incident form, but I only want this to show the variables that have been answered - currently it displays all the variables on the record producer regardless of if these were visible when the ticket was submitted.

I have created the display business rule on the incident table:

find_real_file.png

I have also created the following client script:

find_real_file.png

However all the variables are displaying when I load an incident in the normal servicenow UI that has been created by my record producer.

Any help as to where I am going wrong will be greatly appreciated.

Thanks

Sam

1 ACCEPTED SOLUTION

Sam, ok reworked the script.   I found if I hid the container start and end fields, the entire variable formatter disappeared so I commented those lines out and added specific code NOT to hide them.   I added code to hide the "mapped" fields as well.   Here is my entire business rule script:


(function executeRule(current, previous /*null when async*/) {


      var hideTypes = [];


      hideTypes.push("11"); //Label


   


      g_scratchpad.emptyVars = "";


      var emptyVars = [];


   


      var producerVars = new GlideRecord("question_answer");


      producerVars.addQuery("table_sys_id", current.sys_id);


      producerVars.query();


      while (producerVars.next()) {


              // First check to see if container and if so continue since hiding will cause issues


              if (producerVars.question.type == "19" || producerVars.question.type == "20") {


                      continue;


              }


           


              // Second check to see if variable type is one we always want to hide


              if (hideTypes.indexOf(producerVars.question.type) > -1) {


                      emptyVars.push(producerVars.question.name.toString());


                      continue;


              }


           


              // Third check if variable is mapped to field, if so skip it


              if (producerVars.question.map_to_field == true) {


                      emptyVars.push(producerVars.question.name.toString());


                      continue;


              }


           


              // Fourth skip any variable with a Null value


              if (gs.nil(producerVars.value)) {


                      emptyVars.push(producerVars.question.name.toString());


                      continue;


              }


           


              // Fifth check if variable is a checkbox and hide if default value of false


              if (producerVars.question.type == "7" && producerVars.value == "false") {


                      emptyVars.push(producerVars.question.name.toString());


                      continue;


              }


      }


      g_scratchpad.emptyVars = emptyVars.join();


})(current, previous);


View solution in original post

23 REPLIES 23

screenshot of the alert, i did notice that this show a comma first not sure why?

 

Just been trying to use the script debugger - all new to me so still trying to get to grips with it.

It looks like position 0 in the array 'emptyVars' is being set as undefined which is why I think it is not working:

 

But not sure why this is being set as undefined?

Sam the blank value you mention appears to be a dash '-'.  That could be causing an error and the script fails to run.  I updated the script to check for that and abort pushing it to the emptyVars array.  Maybe this will fix the issue:

(function executeRule(current, previous /*null when async*/) {
       var hideTypes = [];
       //hideTypes.push("11"); //Label
     
       g_scratchpad.emptyVars = "";
       var emptyVars = [];
     
       var producerVars = new GlideRecord("question_answer");
       producerVars.addQuery("table_sys_id", current.sys_id);
       producerVars.query();
       while (producerVars.next()) {
               // First check to see if container and if so continue since hiding will cause issues
               if (producerVars.question.type == "11" || producerVars.question.type == "19" || producerVars.question.type == "20") {
                    continue;
               }
			   
			   var variableName = producerVars.question.name.toString();
			   
			   // Second check to make sure name has a value, if not continue
               if (gs.nil(variableName) || variableName == "-") {
                    continue;
               }
             
               // Third check to see if variable type is one we always want to hide
               if (hideTypes.indexOf(producerVars.question.type) > -1) {
				    emptyVars.push(variableName);
				    continue;
               }
             
               // Fourth check if variable is mapped to field, if so skip it
               if (producerVars.question.map_to_field == true) {
				    emptyVars.push(variableName);
				    continue;
               }
             
               // Fifth skip any variable with a Null value
               if (gs.nil(producerVars.value)) {
				    emptyVars.push(variableName);
				    continue;
               }
             
               // Sixth check if variable is a checkbox and hide if default value of false
               if (producerVars.question.type == "7" && producerVars.value == "false") {
					emptyVars.push(variableName);
					continue;
               }
       }
       g_scratchpad.emptyVars = emptyVars.join();
})(current, previous);

souren0071
Tera Expert

Hey Sam,



We use g_scratchpad variable in display business rule to store data from server side, so that we can use the data in client side later on. I hope you have used display business rule. As you are adding the variables in an array and retriveing the variables by spliting it. Some modification in the code:



g_scratchpad.emptyVars = [];



//same code as you have written



g_scratchpad.emptyVars = emptyVars;




in client script:


var emptyVars = g_scratchpad.emptyVars.toString().split(",");



Please let me know if your issue has been resolved.



Regards,


Souren