g_form.setMandatory is not working in the code

Chaisai
Kilo Expert

Hello There,

 

 

On incident form i have a requirement like once state is changed from "Resolved" to "Assigned to"

have to ask for confirmation "Do you really want to re-open incident" and after clicking ok , comments should be mandatory and should show field message like comments are mandatory. I am able to achieve this with onchange client script but on field message is coming up but mandatory is not working wierdly, please find the code below. Any help is much appreciated.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading && oldValue!=6) //if oldvalue is not resolved  {
return;
}
g_form.clearValue('comments');

if(newValue==-3) //if new value is assigned 
{
var answr = confirm("Are you sure you want to reopen this incident?"); 
if(answr==true){ //working till here and going in loop
g_form.setDisplay('comments',true);
g_form.setMandatory('comments', true); //only mandatory is not working and even the fieldmessage also if this line commented field message is visible
g_form.showFieldMsg('comments','Please enter a comment when reopening an Incident', 'error');

}
else
{
return;
}
}
}

Note : on change field is selected as state 

13 REPLIES 13

can you try this code with basic stuff

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    /* commented out fow now
    if (isLoading && oldValue!=6) //if oldvalue is not resolved  {
    return;
    }*/

    //This is the default
    if (isLoading || newValue === '') {
        return;
    }

    //g_form.clearValue('comments'); //commented out for now

    if (newValue == -3) //if new value is assigned 
    {
        var answr = confirm("Are you sure you want to reopen this incident?");
        if (answr == true) { //working till here and going in loop
            //g_form.setDisplay('comments', true);//commented out for now
            g_form.setVisible('comments', true); //try visible
            g_form.setMandatory('comments', true); //only mandatory is not working and even the fieldmessage also if this line commented field message is visible
            g_form.showFieldMsg('comments', 'Please enter a comment when reopening an Incident', 'error');

        } else {
			g_form.setDisplay('state', oldValue); //set the state to old value;
            return false;
        }
    }
}
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Chaisai
Kilo Expert

Already have tried the above code and it is going recursive and still no luck 

Hi Chaisai,

function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
if (isLoading && oldValue!=6) //if oldvalue is not resolved
{ return;
}

if(newValue==-3) //if new value is assigned
{
var usrResponse = confirm('Are you sure you want to reopen this incident?');
if(usrResponse == true)
{
g_form.setDisplay('comments',true);
g_form.clearValue('comments');
//g_form.setValue('comments',''); //or try to use this instead of clearValue
g_form.setMandatory('comments', true); //if i am placing mandatory nothing is working
g_form.showFieldMsg('comments','Please enter a comment when reopening an Incident', 'error');

}
else
{
return false;
}
}
}

Hi Sanket,

 

This seems to be a bug and as mentioned in other threads no workaround other than using a UI policy. Any other thoughts ?