Case.get API returning Case Clients As Part of Related Contacts
The Case.get API returns case clients as part of related contacts. The case clients are not supposed to be returned as part of the related contacts. The condition here: https://github.com/civicrm/civicrm-core/blob/master/CRM/Case/BAO/Case.php#L1160 is meant to exclude case clients but when the clients are more than one for a case, the parameter here is treated as a String rather than CommaSeparatedIntegers, so the SQL end up having conditions like
NOT IN ('2,3') rather than
NOT IN (2,3).
Create a new case using the add new case form and select two or more clients.
Fetch the Case details with the Case.get API, you will notice that one of the case clients appears twice in the contact field for the returned results. i.e the client is returned as a client and also as a related contact. The logic for excluding the related contacts doesn't work in the
NOT INcondition because it is treated as a string rather than a comma separated integers.
- When the Case details is fetched with the Case.get API, none of the case clients should be returned twice. i.e the case client contact should not be returned as part of the related contacts here: https://github.com/civicrm/civicrm-core/blob/master/CRM/Case/BAO/Case.php#L1130
The proposed solution is to treat the parameter as Comma separated integer values here: https://github.com/civicrm/civicrm-core/blob/master/CRM/Case/BAO/Case.php#L1181
$clientIdType = !empty($caseInfo['client_id']) ? 'CommaSeparatedIntegers' : 'String'; $params = [ 1 => [$caseID, 'Integer'], 2 => [implode(',', $caseInfo['client_id']), $clientIdType], ];
$caseInfo['client_id'] could be empty sometimes and that is the reason for the check above. A good example is the civirules extension here: https://lab.civicrm.org/extensions/civirules/-/blob/master/civirules.php#L312 && https://lab.civicrm.org/extensions/civirules/-/blob/master/CRM/CivirulesPostTrigger/CaseCustomDataChanged.php#L56, if a new case is created, at the point of invoking the
hook_civicrm_custom the case clients are not yet created so the
$caseInfo['client_id'] will be empty, hence the reason for checking if the clients data is empty or not before setting the parameter type.