UI Action, check mandatory fields.

Thomas Vogh
Tera Contributor

Hi

I have a UI Action in change with a script that checks mandatory fields. But it only checks one fields at the times.

How do i set the UI Action to check for all fields onClick.

function approval(){

  if(g_form.getValue('short_description') == ''){

          g_form.hideFieldMsg('short_description');

          g_form.setMandatory('short_description', true);

          g_form.showFieldMsg('short_description','short description are mandatory.','error');

          return false;  

    }

  if(g_form.getValue('description') == ''){

          g_form.hideFieldMsg('description');

          g_form.setMandatory('description', true);

          g_form.showFieldMsg('description','Description are mandatory.','error');

          return false;  

    }

    if(g_form.getValue('change_plan') == ''){

          g_form.hideFieldMsg('change_plan');

          g_form.setMandatory('change_plan', true);

          g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');

          return false;

    }

    if(g_form.getValue('backout_plan') == ''){

          g_form.hideFieldMsg('backout_plan');

          g_form.setMandatory('backout_plan', true);

          g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');

          return false;

    }

    if(g_form.getValue('test_plan') == ''){

          g_form.hideFieldMsg('test_plan');

          g_form.setMandatory('test_plan', true);

          g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');

          return false;  

    }

    gsftSubmit(null, g_form.getFormElement(), 'request.approval');

}

action.setRedirectURL(current);

current.approval='requested';

current.state=9;

current.update();

I am not a scripting expert, so keep it simple. 🙂

Thomas

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Try this



function approval(){


  var return_flag=true;


  if(g_form.getValue('short_description') == ''){


  g_form.hideFieldMsg('short_description');


  g_form.setMandatory('short_description', true);


  g_form.showFieldMsg('short_description','short description are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('description') == ''){


  g_form.hideFieldMsg('description');


  g_form.setMandatory('description', true);


  g_form.showFieldMsg('description','Description are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('change_plan') == ''){


  g_form.hideFieldMsg('change_plan');


  g_form.setMandatory('change_plan', true);


  g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('backout_plan') == ''){


  g_form.hideFieldMsg('backout_plan');


  g_form.setMandatory('backout_plan', true);


  g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('test_plan') == ''){


  g_form.hideFieldMsg('test_plan');


  g_form.setMandatory('test_plan', true);


  g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');


  return_flag=false;


  }


if(!return_flag)


  return return_flag;


  gsftSubmit(null, g_form.getFormElement(), 'request.approval');


}



action.setRedirectURL(current);


current.approval='requested';


current.state=9;


current.update();


View solution in original post

4 REPLIES 4

Chuck Tomasi
Tera Patron

Hi Thomas,



You've got a mix of client side and server side script in this UI action.



I recommend using a UI policy or client script to make your fields mandatory based on your conditions. The UI action will automatically enforce the mandatory fields.


UI Actions - ServiceNow Wiki


Creating a UI Policy - ServiceNow Wiki  


sourabhd87
Tera Contributor

Hi Tomas,



try something like:


  1. function approval(){
  2. var count = 0;
  3.   if(g_form.getValue('short_description') == ''){  
  4.           g_form.hideFieldMsg('short_description');  
  5.           g_form.setMandatory('short_description', true);  
  6.           g_form.showFieldMsg('short_description','short description are mandatory.','error');    
  7.           count = 1;    
  8.     }  
  9.   if(g_form.getValue('description') == ''){  
  10.           g_form.hideFieldMsg('description');  
  11.           g_form.setMandatory('description', true);  
  12.           g_form.showFieldMsg('description','Description are mandatory.','error');    
  13.           count = 1;    
  14.     }  
  15.     if(g_form.getValue('change_plan') == ''){  
  16.           g_form.hideFieldMsg('change_plan');  
  17.           g_form.setMandatory('change_plan', true);  
  18.           g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');    
  19.           count = 1;  
  20.     }  
  21.     if(g_form.getValue('backout_plan') == ''){  
  22.           g_form.hideFieldMsg('backout_plan');  
  23.           g_form.setMandatory('backout_plan', true);  
  24.           g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');    
  25.           count = 1;
  26.     }  
  27.     if(g_form.getValue('test_plan') == ''){  
  28.           g_form.hideFieldMsg('test_plan');  
  29.           g_form.setMandatory('test_plan', true);  
  30.           g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');    
  31.           count = 1;    
  32.     }  
  33. if(count == 1)
  34. {
  35.     return false;
  36. }
  37.     gsftSubmit(null, g_form.getFormElement(), 'request.approval');    
  38. }  
  39.  
  40. action.setRedirectURL(current);  
  41. current.approval='requested';  
  42. current.state=9;  
  43. current.update();

Abhinay Erra
Giga Sage

Try this



function approval(){


  var return_flag=true;


  if(g_form.getValue('short_description') == ''){


  g_form.hideFieldMsg('short_description');


  g_form.setMandatory('short_description', true);


  g_form.showFieldMsg('short_description','short description are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('description') == ''){


  g_form.hideFieldMsg('description');


  g_form.setMandatory('description', true);


  g_form.showFieldMsg('description','Description are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('change_plan') == ''){


  g_form.hideFieldMsg('change_plan');


  g_form.setMandatory('change_plan', true);


  g_form.showFieldMsg('change_plan','Change plan are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('backout_plan') == ''){


  g_form.hideFieldMsg('backout_plan');


  g_form.setMandatory('backout_plan', true);


  g_form.showFieldMsg('backout_plan','Backout plan are mandatory.','error');


  return_flag=false;


  }


  if(g_form.getValue('test_plan') == ''){


  g_form.hideFieldMsg('test_plan');


  g_form.setMandatory('test_plan', true);


  g_form.showFieldMsg('test_plan','Test plan are mandatory.','error');


  return_flag=false;


  }


if(!return_flag)


  return return_flag;


  gsftSubmit(null, g_form.getFormElement(), 'request.approval');


}



action.setRedirectURL(current);


current.approval='requested';


current.state=9;


current.update();


Thanks That works.