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.

Setting variable field to mandatory on submit.

ronyates
Tera Guru

I am trying to set a variable field (variables.score_range) to mandatory if any of these three conditions are true. The script set the field to mandatory no matter the condition. Any help would be apricated. I don’t know what I am missing.  

Conditions:

  1. If score.range is null
  2. If the is_the_enhancement_feasible = yes
  3. Or is_the_enhancement_feasible = null

function onSubmit() {
var task = g_form.getReference("request_item");
var cat_item = task.cat_item;
var state = g_form.getValue("state");
var actName = g_form.getActionName();
if(cat_item == 'SysID' && (state == '3' || actName == 'close_sc_task'))
{
//Set feasibility analysis fields to mandatory on close.

g_form.setMandatory('variables.score_range', true);
if(g_form.getValue('score_range') == '' &&
g_form.getValue('variables.is_the_enhancement_feasible') == 'Yes' ||
g_form.getValue('variables.is_the_enhancement_feasible') == ' ');{
alert('Please complete feasibility and fill request sizing before closing the task.');
return false;


}
}
}

1 ACCEPTED SOLUTION

ronyates
Tera Guru

I found the secret sauce. Now it is working 

function onSubmit() {
    var task = g_form.getReference("request_item");
	var cat_item = task.cat_item;
	var state = g_form.getValue("state");
	var actName = g_form.getActionName();
		if(cat_item == 'SysID' && (state == '3' || actName == 'close_sc_task'))
{
	var feasibility = g_form.getValue('variables.is_the_enhancement_feasible');
	var project_checkbox = g_form.getBooleanValue("variables.project_checkbox");

//Set feasibility analysis fields to mandatory on close.
        if(feasibility != 2 && project_checkbox == false)
		{ 
            g_form.setMandatory('variables.score_range', true);
            alert('Please complete feasibility and fill request sizing before closing the task.');
            return false;
        }
    }
}

 

View solution in original post

7 REPLIES 7

Tai Vu
Kilo Patron
Kilo Patron

Hi @ronyates 

There's a semicolon (;) at the line If Statement in your script. Let's use SanjivMeher script and remove it out, it should do the trick.

 

function onSubmit() {
    var task = g_form.getReference("request_item");
    var cat_item = task.cat_item;
    var state = g_form.getValue("state");
    var actName = g_form.getActionName();
    if (cat_item == 'SysID' && (state == '3' || actName == 'close_sc_task')) {
        //Set feasibility analysis fields to mandatory on close.
        if (g_form.getValue('variables.score_range') == '' && g_form.getValue('variables.is_the_enhancement_feasible') != 'No') { //remove semicolon at this line
            g_form.setMandatory('variables.score_range', true);
            alert('Please complete feasibility and fill request sizing before closing the task.');
            return false;
        }
    }
}

 

 

Cheers,

Tai Vu

ronyates
Tera Guru

This is still not working. When I select No for is_the_enhancement_feasible, the field should not be made mandatory but it still is.

I have tried several different varaitions of the if condition and still get the same results. 

I added this:

 

function onSubmit() {
var task = g_form.getReference("request_item");
var cat_item = task.cat_item;
var state = g_form.getValue("state");
var actName = g_form.getActionName();
if (cat_item == 'SysID' && (state == '3' || actName == 'close_sc_task')) {
 
//Set feasibility analysis fields to mandatory on close.
  var feasibility = g_form.getValue('variables.is_the_enhancement_feasible');
        if (feasibility != 'No')
        {
            g_form.setMandatory('variables.score_range', true);
            alert('Please complete feasibility and fill request sizing before closing the task.');
            return false;
        }
    }
}

ronyates
Tera Guru

I found the secret sauce. Now it is working 

function onSubmit() {
    var task = g_form.getReference("request_item");
	var cat_item = task.cat_item;
	var state = g_form.getValue("state");
	var actName = g_form.getActionName();
		if(cat_item == 'SysID' && (state == '3' || actName == 'close_sc_task'))
{
	var feasibility = g_form.getValue('variables.is_the_enhancement_feasible');
	var project_checkbox = g_form.getBooleanValue("variables.project_checkbox");

//Set feasibility analysis fields to mandatory on close.
        if(feasibility != 2 && project_checkbox == false)
		{ 
            g_form.setMandatory('variables.score_range', true);
            alert('Please complete feasibility and fill request sizing before closing the task.');
            return false;
        }
    }
}