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

Hi Andy,


Sorry for the typo.   Update this line


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


to


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


//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.setMandatory("backout_plan", true);


  g_form.showFieldMessage("backout_plan", "Backout Plan is required to submit approvals.", "error");


  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);


}


Hmm, still get nothing happening on the form when I hit my test button.   Here is a screenshot of what I have...


Capture2.PNG


I found another syntax error, but I just created this button in my developer instance.



capture.jpg



Corrected:


//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.setMandatory("backout_plan", true);


      g_form.showFieldMsg("backout_plan", "Backout Plan is required to submit approvals.", "error");


      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);


}


You rock, Jordan.   Couple last things.   How can I move the error message to the top of the form and simply highlight the field (instead of making it mandatory)?   The problem with making it mandatory is that I now can't simply save the Change Request without that information (keep in mind that I'm simply trying to prevent people requesting approvals, not saving).



Thank you so much.