Form display condition - by group that user is assigned?

Is it possible to filter a form/put a display condition on a form based on the group the user is assigned? I know how to do it with a user property, but not clear in the documentation how to do it with groups/

technically yes. but, the trick is that by default a user will belong to the group you assign them, and maybe a few more depending on if that's a mobile worker group or a location in your org structure.

For this reason, you will want to see what your expected mobile workers will get in their meta group definitions when they load the form. so some use of the debugging tools is needed. then, you can then decide to do one of two things:

1- put those groups in a multiple choice question then use the result in a display condition.
2- code which one you should look for, and return a true/false value to use in a display condition. for that one, you can use a little trick - you Join() the names of the groups that the user has access to which is somewhere under this path if I remember correctly (instance('commcaresession')/session/user/data/) but you join it with a space character. this will give you a space separated list of the group names the user has. THEN, you can use the Contains() function to check for that specific group and return that true false.


To add to what @Mazz wrote, this expression should show the groups each user is assigned:

join(" ", instance('groups')/groups/group/name)

You may want to use "group data" to see if the user is in a group that should view a particular form. If so, the expression would probably look about like this, depending on what group data you've configured, for this example, I assume there's a key called "can_view_form" whose value is "yes"

count(instance('groups')/groups/group/group_data/data[@key="can_view_form" and . = "yes"])) > 0

That expression should be able to go in the form display condition box.

1 Like