- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2015 07:59 AM
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.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2015 02:52 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2015 03:05 PM
Two steps. First, create a UI Policy with the condition approval field is Requested, then set the 5 fields mandatory.
Second. If the user click on "Request Approvals" UI Action, you'll need to run client & server code in this UI Action to check for these same mandatory fields. Check this out: http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2015 06:16 AM
I created a new button called "Test button" and set the form up as follows:
Name: Test button
Action name: request.approval
Client: true
Form button: true
Onclick: RequireFields();
Condition:
Script:
//Client-side 'onclick' function.
function RequireFields(){
if(current.backout_plan == ''){ //if the current backout plan is empty
current.approval = "Not Yet Requested"; //set the approval back to Not Yet Requested
gs.addInfoMessage('Backout Plan is required to submit approvals.'); //give the user an error
current.update(); //update the record
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);
}
When I click the Test button I have no action that takes place that I can see. I should get a message saying it didn't work (Backout Plan is required to submit approvals) or that it did work (it worked). Thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2015 08:52 AM
Hi Andy,
On the client side function, "current" is out of scope. Current is Server side only.
Try this:
/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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2015 09:59 AM
Jordan - I tried your code and get zippo... nothing happens on the form at all. So I create a Change Request, hit my test button with your code under it and nothing happens on the form... no messages, no change in status. I tried your code by itself and also tried to add the server side code behind it as well.
Seems like this should be so easy....
//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);
}