How to validate multiple fields OnSubmit & show alert messages using single servienow functionality?

Novin Ramteke
Tera Contributor

I have a scenario where if the data in Task time fields is greater than zero, and the user tries to submit the form, he should get an error message to fill in the associated Task details.

For example, when user fills Task 3 time and Task 4 time values greater than zero, and tries to submit the form, he should get an error message that it's associated Task 3 details and Task 4 details should be filled.

I am trying to achieve it with a single functionality and avoid writing multiple onSubmit client scripts. Please help me with a script logic or an approach to implement it.scenario.png

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Novin Ramteke ,

 

You can use below script to achieve the same:

function onSubmit() {
      var task1 = parseInt(g_form.getValue('task_1'));
      var task2 = parseInt(g_form.getValue('task_2'));
      var task3 = parseInt(g_form.getValue('task_3'));
      var task4 = parseInt(g_form.getValue('task_4'));
      var task5 = parseInt(g_form.getValue('task_5'));
     var fields = [];
      if(task1>0){
              if(g_form.getValue("task_details_1") == ''){
                       fields.push("task_details_1");
      }
-     if(task2>0){
              if(g_form.getValue("task_details_2") == ''){
                       fields.push("task_details_2");
      }
      if(task3>0){
              if(g_form.getValue("task_details_3") == ''){
                       fields.push("task_details_3");
      }
      if(task4>0){
              if(g_form.getValue("task_details_4") == ''){
                       fields.push("task_details_4");
      }
      if(task5>0){
              if(g_form.getValue("task_details_5") == ''){
                       fields.push("task_details_5");
      }
  if(fields.length != 0){
		g_form.addErrorMessage("Fill task details for these fields, since time is greater than 0 "+ fields);
		return false;
	}
	
}
Best Regards
Aman Kumar

View solution in original post

5 REPLIES 5

@Novin Ramteke 

Awesome, glad to help!
Apologies, I was not using script editor so might have missed a few.

 

Best Regards
Aman Kumar