Multiple-selection entity reference field for Case fails to submit all references

Created on 27 June 2025, 3 months ago

Here is a description of the bug and resolution based on the provided information.

### 1.0 Problem/Motivation

When a webform includes a CiviCRM "Existing Contact" field configured to allow multiple selections, the system fails to process any but the first selected contact. Subsequent processing steps that rely on the list of selected contacts, such as creating cases or populating other contact reference fields, only receive the first contact from the list. This results in incomplete or incorrect data being saved to CiviCRM, as actions are not applied to all the contacts selected by the user.

### 2.0 Detailed steps to reproduce

1. Create or edit a webform with CiviCRM integration enabled.
2. On the CiviCRM processing tab, enable several contacts.
3. For one of the contacts, configure the "Existing Contact" field with Default Unsupervised rule. Then in Case section, select that Contact for one of the fields (which should be a contact reference field). Then in Build tab, enable the "Multiple" selection option in its settings.

In this case, we've Entity select as a type with civicrm_6_contact_1_contact_existing as a key.
Entity reference settings: Type: CiviCRM Contact; Reference method: View - filter by an entity reference view, the use the existing view which returns entity references.

Normally when creating civicrm_6_contact_1_contact_existing, it only allows to select basic and advanced types, but not Entity selection. So to workaround this issue, previous civicrm_6_contact_1_contact_existing was removed, and replaced with the new one which allows Entity selection then the same machine name was used.

4. Optionally, add a CiviCRM Case to the form and configure it to use the contacts from the multiple-selection field as clients.
5. Submit the webform, selecting several contacts in the multiple-selection field.
6. Inspect the created CiviCRM submission data (e.g., the created Case).
7. **Expected Result:** All selected contacts should be processed and linked correctly (e.g., all should be clients of the case).
8. **Actual Result:** Only the first contact from the multiple-selection list is processed and linked. The other selected contacts are ignored.

### 3.0 Proposed resolution

The solution in the attached patch file involves modifying the submission processing logic to correctly handle an array of values from a multiple-selection "Existing Contact" field.

The primary function responsible for fetching existing contact IDs is updated to detect when a field returns multiple values. It now iterates through and verifies each selected contact ID and stores the entire array of verified IDs. This makes the complete list of selected contacts available for subsequent processing steps.

Additionally, other parts of the processing logic, specifically for handling Cases and custom fields of the "Contact Reference" type, are enhanced. These sections now check if the value for a contact is an array. If it is, they correctly use the entire array of contact IDs, ensuring that all selected contacts are properly associated with the case or saved in the corresponding custom fields. This ensures data integrity and that the webform submission behaves as the user expects.

4.0 Submit PR on https://github.com/colemanw/webform_civicrm

🐛 Bug report
Status

Active

Version

6.2

Component

CiviCRM Data Handling

Created by

🇬🇧United Kingdom kenorb

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024