make variables non-mandatory on tasks

randrews
Tera Guru

we will quite often add required variables to catalog items as our catalog matures..

if an item was requested prior to the variable being added   when the task is generated we have a problem that the task can't be saved because the required variable is blank < we feed all variables to the task and mark them as read only so that they aren't changed by techs>.

I have modified a combined a BR and client script we had to hide empty variables so that it marks them as not mandatory and it DOES strip the red bar from them.. however it does NOT allow you to save the field if a variable is blank (you get a popup that "the following mandatory fields are not filled in" ... any idea on why this is not working or a better method of doing this?

_________________________________________________
CLIENT SCRIPT (run on task table with inherited and global checked)

function onLoad() {

//Mark all empty variables not mandatory using the scratchpad object passed from 'Hide Empty Variables' business rule
if(g_scratchpad.emptyVars != ''){
var emptyVars = g_scratchpad.emptyVars.split(',');
for(i = 0; i < emptyVars.length; i++){
eval("g_form.setMandatory('variables." + emptyVars + "', false)");
}
}
//Mark all notempty variables not mandatory using the scratchpad object passed from 'mark variables not mandatory' business rule
if(g_scratchpad.notemptyVars != ''){
var notemptyVars = g_scratchpad.notemptyVars.split(',');
for(i = 0; i < notemptyVars.length; i++){
eval("g_form.setMandatory('variables." + notemptyVars
+ "', false)");
}
}
}
___________________________________________________
BR set on the task table when set to display; condition set to !RP.isPopup()

//Initialize the scratchpad variable
g_scratchpad.notemptyVars = '';

//Check to see if a variable pool exists
var notcount = 0;

for(vars in current.variable_pool){
notcount++;
break;
}

//If a variable pool exists then collect Notempty variable names
if(notcount > 0){
var notemptyVars = new Array();
var nottable = current.getTableName();
//Query for the Notempty variables for this record
//Catalog item and task variables pull from 'sc_item_option_mtom' nottable
if(nottable == 'sc_req_item' || nottable == 'sc_task'){
var notitemVars = new GlideRecord('sc_item_option_mtom');
if(nottable == 'sc_req_item'){
notitemVars.addQuery('request_item', current.sys_id);
}
if(nottable == 'sc_task'){
notitemVars.addQuery('request_item', current.request_item.sys_id);
}
notitemVars.addNotNullQuery('sc_item_option.value');
//Exclude Label and Container variables
notitemVars.addQuery('sc_item_option.item_option_new.type', '!=', 11);
notitemVars.addQuery('sc_item_option.item_option_new.type', '!=', 19);
notitemVars.addQuery('sc_item_option.item_option_new.type', '!=', 20);
notitemVars.query();
while(notitemVars.next()){
//Add variable names to the NotemptyVars array
notemptyVars.push(notitemVars.sc_item_option.item_option_new.name.toString());
}
}
else{
//All other variables pulled from 'question_answer' nottable
var notproducerVars = new GlideRecord('question_answer');
notproducerVars.addQuery('nottable_sys_id', current.sys_id);
notproducerVars.addNotNullQuery('value');
//Exclude Label and Container variables
notproducerVars.addQuery('question.type', '!=', 11);
notproducerVars.addQuery('question.type', '!=', 19);
notproducerVars.addQuery('question.type', '!=', 20);
notproducerVars.query();
while(notproducerVars.next()){
//Add variable names to the NotemptyVars array
notemptyVars.push(notproducerVars.question.name.toString());
}
}

//Store the result in the scratchpad
g_scratchpad.notemptyVars = notemptyVars.join();
}

2 REPLIES 2

Jim Coyne
Kilo Patron

Saving a record without populating all mandatory variables

You might be able to take something from it and modify to your needs?


zachkenyon
ServiceNow Employee
ServiceNow Employee

One other idea would be to make the variables mandatory on the Service Catalog using Catalog UI policies, rather than checking the "mandatory" flag on the variable. That would make the variables mandatory when the catalog item is requested, but they would not be flagged mandatory on tasks.