Select a Primary in a Many-to-Many Relationship

I’m a noob, so apologies if this has already been asked/answered. I haven’t been able to find anything, but I might not have the right terminology.

I am trying to build a new module to record Visits by Contacts. There is a many-to-many relationship between these two modules. In many instances, a Visit may consist of a single Contact, but occasionally a Visit could consist of a group. In these cases, we would like to specify the primary Contact for the group, for the purposes of generating internal paperwork.

After the user has linked the Visit record to one or more Contact records, they will then select one of the linked contacts to be the primary. I’d like the UI to display a dropdown of the linked Contacts; a Contact has to exist in the xx_visit_contacts_c table before it can be specified as the primary. This selected Contact ID will then be stored in a field on the Visit table.

Is there anyway to achieve this? Many thanks for your assistance.

Yes @cpetrovay . It is possible. We did it several times for a lots of customers.
Basically you will need to:

  1. add the field primary_contact into the Visit module with an empty dropdown list;
  2. create a custom controller for the module Visit and override the methods action_editview and action_detailview in order to load the dropdown options containing the related Contacts for the primary Contact;
  3. add the Primary Contact into DetailView and EditView