Business Rules for mandatory fields??

aharper00
Kilo Explorer

Seems that I'm missing something easy here.   On my Change Request form... I'm trying to enforce some mandatory fields in order to request approvals and think I am over-complicating my solution.   At the end of the day, I'm looking for recommendations on how to make the Planned Start/End Dates, Change/Test/Backout Plans all mandatory only when requesting approvals by clicking "Request Approvals" or changing the approval field to "Requested."   These fields are not mandatory to simply save or submit the form.

I initially tried to setup a UI Policy with various conditions (Approval is Requested and Planned Start Date is not empty or Planned End date is not empty or ...).   I also set the color of the fields to something to highlight them so that people could visibly see where to go to make changes easily.   The problem I ran in to was that my highlighting and such was always active... it's like the conditions for the UI Policy didn't matter.   No matter what I used I could never get the UI Policy to work as intended so I finally gave up and thought to try this with a Business Rule.

The Business Rule checks for various conditions like Approval is Requested, Change Type is Comprehensive, etc.   I then have an advanced script that tosses an error at the top of my Change Form if the condition isn't met.   The Backout Plan, for example, has the Advanced Script condition of "current.backout_plan.nil()" and then simply runs the following script... gs.addErrorMessage("Backout Plan needs populated to request approvals."); .   I have 5 of these Business Rules setup... one for each of the required fields that I want filled out if someone requests approval.

The Business Rules seem to work as expected but has a side effect of leaving the form in the state that the user was initially moving it to... Requested.   Because the requirements aren't met I want to leave the approval field in the "Not Yet Requested" state.   If I script the current.approval back to "Not Yet Requested" I only get one error at a time at the top of my form even though all 5 fields might be missing.   I hate to tell my end user about one field at a time if I don't have to.

This is an example of what the form looks like if I were to submit a test Change Request with all 5 fields empty.  

Capture.PNG

Pointers?   I'm ok if I keep the Business Rules in place and simply highlight the Approval field telling the user to change the state back to Not Yet Requested if that's easiest, too.

Thanks!

Andy

1 ACCEPTED SOLUTION

Thanks for being patient.   I think this what you're after.


//Client-side 'onclick' function.


function RequireFields(){


  var bPlan = g_form.getValue("backout_plan");


  var tPlan = g_form.getValue("test_plan");


  // declare your variables or your additional fields



  if (bPlan == "" || tPlan == "") { //check for empty fields.   Use OR condition with your additional fields


  if (bPlan == "") {   //back out plan is empty


  g_form.hideFieldMsg("backout_plan");


  g_form.flash("change_request.backout_plan", "#FF0000", -4);


  g_form.showFieldMsg('backout_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Backout Plan is required to submit for Approval.");


  }


  if (tPlan == "" ) { //test plan is empty


  g_form.hideFieldMsg("test_plan");


  g_form.flash("change_request.test_plan", "#FF0000", -4);


  g_form.showFieldMsg('test_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Test Plan is required to submit for Approval.");


}


  //add your code here for each additional field



return false;   //Abort submission



}


//Call the UI Action and skip the 'onclick' function


...



I did have a comment about your server side function.


current.approval = "Requested" should be current.approval = "requested";


For coding, always set the Value since it may be different than the label e.g. requested/Requested




//Server-side function


function runBusRuleCode(){


  current.approval = "Requested";


  current.update();


  gs.addInfoMessage('It worked'); //tell me it worked, I'll remove this later


  action.setRedirectURL(current);


}


View solution in original post

16 REPLIES 16

Several ways to do this but I chose two.


The label will flash with g_form.flash();


Also, the message is displayed at the top and I kept the message on the field.



You could also set a field style on the backout field if the stage is not approved, which is not included as part of this script.



Updated:


//Client-side 'onclick' function.


function RequireFields(){


  if (g_form.getValue("backout_plan") == "" ) { //if the current backout plan is empty


  g_form.hideFieldMsg("backout_plan");


  g_form.flash("change_request.backout_plan", "#FF0000", -4);


  g_form.showFieldMsg('backout_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Backout Plan is required to submit approvals.");



  return false;   //Abort submission


}


//Call the UI Action and skip the 'onclick' function


gsftSubmit(null, g_form.getFormElement(), 'request.approval'); //MUST call the 'Action name' set in this UI Action


}






//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


  runBusRuleCode();






//Server-side function


function runBusRuleCode(){


  current.approval = "Requested";


  current.update();


  gs.addInfoMessage('It worked'); //tell me it worked, I'll remove this later


  action.setRedirectURL(current);


}


^^   THIS DOES IT!   Thanks!   So last question... how can I add additional fields to this, now?   I'd like to add the test_plan, change_plan, backout_plan, start_date and end_date to all be checked and throw errors at the same time.



Thanks for your help!!   I'm glad that forums like this exist to help like this.



Note - while I am getting the intended result I'm never getting the "it worked" msg thrown to the top of the form (not sure that it matters, though).


Hi Andy,


You'll need to update the Client-side 'onclick' function for each field that is being validated. Here's a head start:


//Client-side 'onclick' function.


function RequireFields(){


  if (g_form.getValue("backout_plan") == "" ) { //if the current backout plan is empty


  g_form.hideFieldMsg("backout_plan");


  g_form.flash("change_request.backout_plan", "#FF0000", -4);


  g_form.showFieldMsg('backout_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Backout Plan is required to submit for Approval.");


  return false;   //Abort submission


  }   if (g_form.getValue("test_plan") == "" ) {


  g_form.hideFieldMsg("test_plan");


  g_form.flash("change_request.test_plan", "#FF0000", -4);


  g_form.showFieldMsg('test_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Test Plan is required to submit for Approval.");


  return false;   //Abort submission



//paste code here and update with your fields






}   // keep your code within the function


The problem I'm having with the code here is that it's looking at one thing at a time.   So if it looks for the backout plan first and doesn't see it then I get an error and it stops processing (I don't know that the other fields are also empty).   If I have a backout plan but don't have a test plan I'll get the error for the test plan.   My hope is that I can have it check all fields and provide errors for what all is missing in one sweep.



And btw, I really appreciate your help!


Thanks for being patient.   I think this what you're after.


//Client-side 'onclick' function.


function RequireFields(){


  var bPlan = g_form.getValue("backout_plan");


  var tPlan = g_form.getValue("test_plan");


  // declare your variables or your additional fields



  if (bPlan == "" || tPlan == "") { //check for empty fields.   Use OR condition with your additional fields


  if (bPlan == "") {   //back out plan is empty


  g_form.hideFieldMsg("backout_plan");


  g_form.flash("change_request.backout_plan", "#FF0000", -4);


  g_form.showFieldMsg('backout_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Backout Plan is required to submit for Approval.");


  }


  if (tPlan == "" ) { //test plan is empty


  g_form.hideFieldMsg("test_plan");


  g_form.flash("change_request.test_plan", "#FF0000", -4);


  g_form.showFieldMsg('test_plan','Required to submit approvals.','info');


  g_form.addInfoMessage("Test Plan is required to submit for Approval.");


}


  //add your code here for each additional field



return false;   //Abort submission



}


//Call the UI Action and skip the 'onclick' function


...



I did have a comment about your server side function.


current.approval = "Requested" should be current.approval = "requested";


For coding, always set the Value since it may be different than the label e.g. requested/Requested




//Server-side function


function runBusRuleCode(){


  current.approval = "Requested";


  current.update();


  gs.addInfoMessage('It worked'); //tell me it worked, I'll remove this later


  action.setRedirectURL(current);


}