- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 05:47 AM
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:
I have also created the following client script:
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 09:24 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 06:12 AM
I just recreated your code in my demo instance and NOT declaring the emptyVars in the business rule is the issue. Once I did that, problem solved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 07:37 AM
HI Michael,
Thanks for this, I've added this and it seems to have partially worked, however I have some label variables that are still displaying, Line 9 in the business rule should exclude labels - any suggestions?
Also it does not seem to have worked for check boxes - I'm guessing because these always have a value i.e. either false or true. How would it be best to amend either the business rule of the client script so when a check box is false it does not display?
Thanks
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 07:49 AM
The variable "type" field is actually a string and not a number so you query isn't setup correctly, should have quotes around the number like the following:
producerVars.addQuery("question.type", "!=", "11");
For checkboxes, yes that is hard though you can handle it in your while loop:
while (producerVars.next()) {
if (producerVars.question.type == "7" && producerVars.value == "false") {
// False checkbox, skipping
} else {
emptyVars.push(producerVars.question.name.toString());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 08:00 AM
Actually scratch the above while loop code. Realized I completely messed it up. So solution is to remove the addNullQuery from your code and check for NULL inside the while loop:
//producerVars.addNullQuery("value");
producerVars.query();
while (producerVars.next()) {
if (gs.nil(producerVars.value) || (producerVars.question.type == "7" && producerVars.value == "false")) {
emptyVars.push(producerVars.question.name.toString());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 08:44 AM
HI Michael,
Thanks for the above. That seemed to work for the check boxes. However the label variables are still showing, even if I have "" round the numbers.
Also I have been trying to further tidy this up as we have the variables with names 'short_description' & 'description' but these auto map to the fields with the same name on the form so have no need to show these on within the variable editor. I've amended the business rule as follows, but it is not working?