How to filter out options in a multiple choice list based on user role?

HI, I have what i thought was a fairly simple problem but cant work out how to do it.  I have a STATUS field which has multiple choice input, for example:   CREATED, SPECIFIED, CODED, TESTED, APPROVED.

I want to limit the ability to set the field to APPROVED to only those users who have a role APPROVER.  For anyone else, I'd like to either hide that option on the radio button list, or gray it out, or even pop up a message saying they cant select that option because they are not an approver.

Any idea how I can achieve this?

 

 

I believe this is what you are looking for, I got this code from this community somewhere...

/////////////////////////////////////////////////////////////////////////////////////
//Remove restricted entries from a dropdown. In this case, high-level Account Roles.
$(document).on('mousedown keyup',
'#view_522_field_62_chzn' + //Roles field for Add account
', #view_523_field_62_chzn' //Roles field for Edit account
//', #view_XXX_field_YYY_chzn' //Add more here as needed. Don't forget the + at end of line!
, function () {
//$("ul.chzn-results li:contains('Supervisor')").remove();
$("ul.chzn-results li:contains('Admin')").remove();
//$("ul.chzn-results li:contains('SuperAdmin')").remove();
}
);

 

 

Hi Dean,
this is almost what I want for something slightly different but simpler I think.

On a specific view I want to restrict the choices available regardless of role. So I assume the snippet would be similar but without these lines?

#view_522_field_62_chzn’ + //Roles field for Add account
‘, #view_523_field_62_chzn’ //Roles field for Edit account

So I tried this and nothing happened (this is JS not CSS isn’t it…?). Any tips on this?

//Remove Absence Types from a dropdown (Add Absence).
$(document).on(‘mousedown keyup’,
//’, #view_160_field_311_chzn’ //Add more here as needed. Don’t forget the + at end of line!
, function () {
//$(“ul.chzn-results li:contains(‘Unpaid Leave’)”).remove();
$(“ul.chzn-results li:contains(‘Unpaid Sick’)”).remove();
//$(“ul.chzn-results li:contains(‘Paid Sick’)”).remove();
}
);

Thanks a lot

Hi

I have watched this with interest and have an alternative suggestion - which is to let the database do the hard work for you. Set the field up as a connection instead of a Multiple Choice and have field(s) in the Looked Up object which you can then use to filter the dropdown on forms as you need.

Hope this helps - wherever possible I think it’s best to avoid JS - and use the database for what it’s good at.

Julian

1 Like

Thanks Julian, I’ll have a look at that suggestion and see how I get on. I agree though about getting the system do do the job instead of using JS etc to create workarounds.

Cheers

Hi @JulianKirkness,

Very clever! Once I understood the idea it was easy and works well (assuming you meant to create a new object with one record for each of the choice options).

Although it doesn’t use any code it does feel a bit like you are creating a workaround that is slightly outside the intended scope of the objects, connections etc, but I like it anyway!

Thanks

Hi @Hugo

You’re welcome - and in my view this is far from being a ‘work around’ - simply using the database for what it does best.

Julian

Hi @JulianKirkness,

fair enough, I’ll accept your view entirely - I am still learning and don’t think I would have thought of that in a long time!

Thanks again

Sorry Hugo I just saw this and the solution I gave you is static in that it removes the option for everyone using that form. The form is then protected by their role and only certain users would have access or see certain forms. For what I think you are trying to do, in JS you would need to pull the role data from the user profile and use a test to check if they were in a certain role to access/see and select a certain option. Although possible the solution that is proposed by Julian could be a much easier and cleaner way to go if it solves your issue.

Thanks @DeanBozzan62116, it wouldn’t need to be role specific, but I went with Julian’s solution anyway which works perfectly.

Cheers though :grinning:

1 Like

To remove multiple options regardless of role using css for a specific form, just change the # in child(X). Also replace view_xx and field_xx for it to work. The first item in your current results is 1, your second is 2 etc…

Hope it helps.

#view_XX-field_XX > option:nth-child(X){
display: none; /* worked in firefox*/
visibility: hidden; /* worked in chrome*/
}