Removing a User Role from Front-End Forms

I am trying to create a form that allows a user to remove someone else’s “Supervisor” role. I can successfully get a form to show all the user roles:

And if I change the user status, that works. But if I want them to remove the user role in question, it doesn’t work. Nothing actually happens to the user roles in the back end.

The form does add a user role successfully. And I can remove user roles other than the Supervisor role. (The form is connected to the Supervisor role.)

Is this not possible?

Hi @Deanna - Hopefully the below will help. You might find the additional resources of interest
One is from my friend and college @StephenChapman, the other is Knacks September webinar where me, @Andy and @DaveParrish discussed user roles and accounts.

Thanks, but it’s not helpful, given this is an app that has been running for years, with thousands of records. This is just one new feature.

It really feels like a bug to me, as I can delete roles other than the one in question.

(To clarify - I already knew I could do it via a table of accounts, but the connection is at the supervisor level and I only want to make the form available if there’s no associated appointments. And I don’t want a grid, I want the specific update form. I get that I could refactor the entire thing to NOT use role connections and only use Account connections, but that isn’t going to happen.)

Sorry it wasn’t of help, I obviously didn’t know the background :man_shrugging:
I’d recommend raising your concerns to Knack support to see if they can offer any further advice.

One is from my friend and college @StephenChapman

@Carl You have to stop confusing us Australians. :joy: @Callum.Boase gets full credit for that helpful resource!

@Deanna unfortunately there is no native way of doing so, as frustrating as that is.

There is a JavaScript method you can use to do so:

Requirements:

  • The profile key of the role you want to remove (profile_zz). This is identical to the user table’s object key (e.g. object_zz, but just with the keyword profile.
  • A form (view_xx) to update an Account table, which contains an editable user role field (field_yy) with the label removed. It is important that this remains editable, otherwise it will not update the user role field.
    Your form should look like below:
  • Ideally, you only give users access to this form if the selected account actually contains a Supervisor role, which can be controlled with page rules.

Code
Paste the below code into your app’s JavaScript editor, and replace view_xx, field_yy, and profile_zz with your values.

$(document).on('knack-view-render.view_xx',function(event, view, record){
  
  /* Replace these values with your user roles field, and the role you want to remove */
  const userRolesField = "field_yy";
  const profile = 'profile_zz';

  /* Get the elements on the form */
  const userRolesSelect = $(`#${view.key}-${userRolesField}`)
  const userRolesList = $(`#${view.key}_${userRolesField}_chzn`);
  
  /*Hide the input list and remove the profile from the selections.
  On submit of the form, the specified profile will be removed from the user role field */
  userRolesList.hide();
  userRolesSelect.find(`option[value='${profile}']`).remove();

});

When the form is loaded, it will automatically remove the user role from the field, and hide the field so that it cannot be further edited. On submit, the user role will be removed from the record.

I hope that helps!

1 Like

Oops, yes, it was @Callum.Boase video, sorry @StephenChapman and thanks for the correction :+1:

Sorry I may have over engineered this solution, and missed your key point.
I believe if you’re editing the Supervisor record itself, you won’t be able to remove that role within the same form. This would need to be done from the overall Accounts record, which is also where my solution is based.