How to restrict a choice value in dependent variable based on roles?

Jagadeesvarraj
Tera Contributor

Hi,

 

I have two variables category and subcategory. Subcategory is a dependent to Category variable. How to restrict a particular choice in subcategory which is dependent to category variable based on user role?

 

I have tried using g_form.removeOption as well as g_form.clearOptions, but none of them working for this dependent variable scenario. If I removed the dependency from subcategory, the above methods are working fine. 

 

Can anyone provide a solution on restricting a particular choice in subcategory which is dependent to category variable based on user role.

 

Thanks.

 

12 REPLIES 12

Omkar Kadoli
Tera Contributor

Hi @Jagadeesvarraj  ,

 

I would suggest a new Client Script that runs onLoad().

 

function onLoad() {
//who to display it for - admin
var isAdmin = g_user.hasRole('admin'); // Here You can add your role

if (!isAdmin){
g_form.removeOption('subcategory', 'Add your field value');
}
}

 

OR

 

you can use onchange() client script where in you can see the value selected in field A and depending on that you can add/remove options in field B(choice List Variable)

 

Use these functions for the same

 

g_form.addOption('<Variable Name>','<value>','<display value>'); // To add options to the dropdown

g_form.clearOptions('<variable name>'); //To remove all values from the dropdown

g_form.removeOption('<Variable Name>', '<value>'); // To Remove 1 option from the dropdown

g_form.setValue('<Variable Name>','<value>'); // To select by default any one of the options.


Hope this helps.

 

Thanks,

Omkar

I tried this, but this will work only if Subcategory isn't dependent to Category. Also as per my requirement, subcategory as more than 300 choices. 

Hi @Jagadeesvarraj  ,

 

Once you change the category, subcategory field's option is recalculated. Now if you have any script written onchange of category then it executes before subcategory options are re-calculated.

 

Still you can do a workaround. You can delay your script by 1 second so that it executes after re-calculation of subcategroy option. Please try below script onChange of the Category field:

if( !g_user.hasRole("roleName")) //Role that is needed to see the option
{
setTimeout(function () {
      g_form.removeOption("subcategory" , "option_you_want_to_remove") //delay removal by 1 second
    }, 1000);
}

Note: You can handle conditions like which category should remove which subcategory in side if condition, as per your requirement. This is just to show how you can effictively remove subcategory options based on category selection

 

Please let me know if this do not works for you.

 

I Hope this helps.

 

Please mark this helpful if this helps and Accept the solution if this solves your issue.

 

Regards,

Kamlesh