How can I filter objects where the field I want to filter on is a multiple-choice field?

I have an object which stores the employee name and role.

<model name="employee" label="Employee">
    <field name="first_name" label="First Name" type="text:name" />
    <field name="role" label="Role" type="multiple-choice">
        <option key="admin">Administrator</option>
        <option key="driver">Driver</option>
    </field>
    <display>{first_name}</display>
</model>

Some users are drivers, some admin and some both. I created a procedure in the sharedJS folder to return all drivers based on the role.

function sharedLookupDrivers() {
    return DB.employee.where("role = ? ", "driver").orderBy("first_name").toArray();  
}

I am having issues returning all the drivers, since the role field is a multiple-choice and I cannot indicate in the filter that I need to return all object where 'driver' is selected. I have tried other methods of applying the filter but I cannot return all employees where 'driver' is selected.

Can you please advise how I can select objects where the role has 'driver' selected?"

When working with a multiple choice field in the database, you can you contain instead of = to query based on a value.

Example:

function sharedLookupDrivers() {
    return DB.employee.where("role contains ? and role contains ?", "driver", "admin").orderBy("first_name").toArray();  
}

This will find all user that are admin's and drivers.

1 Like