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

Elijah Aromola
Mega Sage

You need to nest the g_form.setMandatory() call inside your if statement. Currently it is outside of it so it will always be mandatory no matter if your conditions are true/false.

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('score_range') == '' &&
            g_form.getValue('is_the_enhancement_feasible') == 'Yes' ||
            g_form.getValue('is_the_enhancement_feasible') == ' '); {
            g_form.setMandatory('score_range', true);

            alert('Please complete feasibility and fill request sizing before closing the task.');
            return false;
        }
    }
}

 

ronyates
Tera Guru

Than you for the responce!

This still sets the field to mandatory if I select no for is_the_enhancement_feasible. 

SanjivMeher
Mega Patron
Mega Patron

I have made few correction to you script. For ex g_form.getValue('score_range') should be g_form.getValue('variables.score_range'). Can you try below script? If variables.score_range doesnt work, another option is using Display Business Rule to get values of this variables in scratchpad and then use in the onSubmit script.

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');{
g_form.setMandatory('variables.score_range', true);
alert('Please complete feasibility and fill request sizing before closing the task.');
return false;
}
}
}

 

 


Please mark this response as correct or helpful if it assisted you with your question.

Thank you.

I made the changes, but still when I select no for the is_the_enhancement_feasible, the field scope.range is still made mandatory.