removeOption on Status Field - I want to remove the ability to choose it, not the ability to see it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:24 PM
I have a Status field that has 8 choices. I only want a user with a particular role to be able to choose the "Cancelled" choice (only when the status is "Review", so I created an onload Client Script to check if the user has that role and removeOption on all the other choices except "Cancelled".
The issue is that when the user goes to open up a record that has a status other than "Cancelled" the status field is blank.
I also created an ACL that makes the field writeable only if it's in Review status.
I tried an onChange Client script and it shows the status if it's not cancelled, but then when I go to change the status, it allows that user to select any choice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:50 PM - edited 07-24-2024 04:11 PM
Hello,
This gets complicated by involving ACLs now and later. Hiding rest of the choices but onLoad the existing state choices (other than Cancelled) are still shown. I tried in my PDI, its working as needed.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
var userCanBypass = g_user.hasRole('admin'); // role check
if (userCanBypass) { //if user has role
var stateField = g_form.getControl('state');
if (stateField) {
var options = stateField.options;
for (var i = options.length - 1; i >= 0; i--) {
if (options[i].value !== '8') { //assuming cancelled is 8
g_form.removeOption('state', options[i].value);
}
}
}
if (newValue != '8') {
g_form.setValue('state', oldValue);
g_form.showFieldMsg('state', 'You can only change the state to "Cancelled".', 'error');
}
}
}
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 04:15 PM
Add if (stateField && g_form.getValue('state','Review')){ in the line as shown below. Adjust as needed.
if (stateField) {
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 03:07 PM
This is so close, when it tries to go back to the oldValue, the onChange kicks off again and gives a double error message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 11:46 AM
You can add gs.hideFieldMsg() function just after the if block (newValue !=8)
So everytime the script runs next time, previous value is cleared.
Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.