Fields Mandatory

Naveen87
Tera Guru

Hello Developers,

 

I wanted to make few fields mandatory while closing the stask.

I used below script & it is working fine.

But I also want to add few things like, If user is in a different view where these fields are not visible on form then these should not be mandatory.

 

Do we have any line of code which check, If these fields are on the form?

If yes, then fill them(mandatory)

If No, ignore (non-mandatory)

Naveen87_0-1724663053421.png

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var state = g_form.getValue('state');
    var solution = g_form.getValue('u_solution');
    var assign = g_form.getValue('assigned_to');

    if ((state == 3 || state == 10) && (solution == '')) {
        g_form.showFieldMsg('u_solution', 'Please fill solution before closing the Task', 'error');
        //	alert('Please fill solution field before closing the Task');
        return false;
    }
    if ((state == 3 || state == 10) && assign == '') {
        g_form.showFieldMsg('assigned_to', 'Please fill Assigned to before closing the Task', 'error');
        return false;
    }

}

 

Thank you

 

1 ACCEPTED SOLUTION

You can use the below like to check if the field is present on the form(current view)

 

 g_form.hasField('<your field name>') //returns true fi field is present, false if it is not

-Anurag

View solution in original post

14 REPLIES 14

Dr Atul G- LNG
Tera Patron
Tera Patron

This is not expected buddy, if field are not on Form then how user will know that it is mandatory 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

That's why I want to make a check, If fields is visible on form or not.

If yes, Mandatory

If no, Non-Mandatory

Anurag Tripathi
Mega Patron
Mega Patron

Hi Naveen,

You can use something like below to check the view and make the fields Mandatory based on them

  var view = getView();
   if (view == '<view name>'){
 //make fields mandatory
   }
-Anurag

Hi Anurag,

Not specifically to 1 view,

This will be better for future too, I want to check if fields is available on form, If yes then Mandatory else not.

 

Please suggest