Details
-
Type: Bug
-
Status: Done/Fixed
-
Priority: Minor
-
Resolution: Fixed/Completed
-
Affects Version/s: 1.1, 1.2, 1.3
-
Fix Version/s: 1.3
-
Component/s: None
-
Labels:None
Description
I tried to add a contact to a group via the API, first by obtaining the group object with crm_get_groups() and then adding contacts with crm_add_grou_contacts().
The search result is an array (duh), and I tried to pass it directly into the crm_add_group_contacts function. This resulted in an error (duh again), but not the right one.
THE ISSUE:
1) The function actually tries to execute, passing in $group->id (which doesn't exist because I passed in an array, not an object). You want to catch this beforehand with validation instead of letting it get to MySQL, which barfs with a "foreign key constraint" error.
2) Speaking of barfing, the error message is in the form of a formatted HTML page (installation is inside of drupal, but i'm using the API that "should" be separate) I get a page formatted with my drupal theme, when I should get an error object or some other API-friendly result.
CODE SNIPPET:
if ($group = crm_get_groups(array('name'=>'Vendors'))) {
$members = array($crm_org,$crm_user);
crm_add_group_contacts($group,$members);
}
MY "FIX"
if ($group = current(crm_get_groups(array('name'=>'Vendors')))) {
$members = array($crm_org,$crm_user);
crm_add_group_contacts($group,$members);
}
--Allie Micka
We need to add checks to all api functions that expect "objects" and/or arrays and validate that before we do any work. For objects we need to use is_a, for arrays: is_array. Once we make the change we also need to expand all the unit tests to make sure the functionality works, and that current functionality is not broken.
K: potentially good project for manish
lobo