OnChange Client Script to Hide Field Choices

richelle_pivec
Mega Guru

I have written this on-change client script, but the problem I seem to be having is that I want a choice from the same field that is changing to hide as the different choices are made on the field. If the Change State is Planning or Pending Mgr Approval I want it to hide the Approval Change State. If the Change State is Closed Complete (which as a value of 4 and not a name value like the others), Closed Canceled or Approved, I want Approved to be available.

I keep getting errors (like this: onChange script error: InternalError: too much recursion function) when I change the value. (Sometimes it will work to hide Approved when moving from Planning to Pending Mgr Approval, but moving to the others gives an error.

Any help on how to hide values on change of the field you want the values to hide on?

thanks, Richelle

Here's the script

Name: Hide Approved

Type: onChange

Table name: change_request

Field name: Change state (u_change_state)

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }

if (newValue =='Planning' || newValue =='Pending Mgr Approval') {

      g_form.removeOption('u_change_state', 'Approved');

}
else {
if (newValue =='Pending CAB Approval' || newValue == 'Closed Canceled' || newValue == 4, 'Closed Complete') {
if(!g_form.getOption('u_change_state', 'Approved')){
              g_form.addOption('u_change_state', 'Approved');
}  
}
  }
}

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

You are close Richelle. newValue is going to be the new value you just changed u_change_state to.



There's no getOption() method in g_form and it looks like you're last || on line 9 is a little "off".



Is u_change_state holding numeric values or string values? (Not the label, but the value of those choice list - that's what newValue will hold.)


View solution in original post

2 REPLIES 2

Chuck Tomasi
Tera Patron

You are close Richelle. newValue is going to be the new value you just changed u_change_state to.



There's no getOption() method in g_form and it looks like you're last || on line 9 is a little "off".



Is u_change_state holding numeric values or string values? (Not the label, but the value of those choice list - that's what newValue will hold.)


It is holding string values except for Closed Complete...for some reason that is holding "4" as it's value.



Anyway, I think this will work for me. I determined that the only time I need "Approved" to display is when it is in "Pending CAB Approval." Once a Manager approves it through the proper Approval Request, it switches it to Approved. And only those that go to "Pending CAB Approval" (because they need that) will need to be able to go back to the "Approved" Change State. We are trying to eliminate the risk of team members (or managers) accidentally approving requests through the Change State field and not through the Approval record...



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
    }


if (newValue =='Planning' || newValue =='Pending Mgr Approval' || newValue =='New'|| newValue == 'Closed Canceled' || newValue == 4, 'Closed Complete') {


      g_form.removeOption('u_change_state', 'Approved');


}
else {
if (newValue =='Pending CAB Approval' ) {
//if(!g_form.getOption('u_change_state', 'Approved')){
              g_form.addOption('u_change_state', 'Approved');
//}  
}
}
}


I think the last step will be to do an "On Load" client script that looks at that field when it first loads (in Planning) and hides Approved then. (This should make it work for all changes going forward. Those currently out there could technically defeat the client scripts if they are in Pending Mgr Approval or Closed Complete or Closed Canceled, but that number would be limited and would phase out as new changes replace existing ones.)



Thanks for help with the solution!



Richelle