2

I am struggling with adding tags trough the CiviCRM API to a newly created contact in an extension (trough API Chaining). The installation is running on Wordpress 4.4.1 with CiviCRM 4.7. The following PHP snippet creates the contact with Tag "Non-Profit" which is not the expected behavior.

$contactParams['api.EntityTag.create'] = array(
        'entity_table' => 'Contacts', 'tag_id' => array("Interessent",     "Contributor")
);

$api->Contact->Create($contactParams)
  • 1
    Could you please update your question with your CiviCRM version number? If nothing else, I changed the behavior of the EntityTag API in issues.civicrm.org/jira/browse/CRM-17350, so this may be version-dependent. Also, are you using a non-administrator account? If so, you'll want to be using 4.7+ or merge in CRM-17350's patch. – Jon G - Megaphone Tech Jan 30 '16 at 22:12
  • I am using 4.7. This is running as an extensions which hooks into civicrm_emailProcessor. I am not sure if the administrator account is relevant at all. The questions is, do I use the TagName or the TagID or Name_ID like the GroupContact API asks? I have tried all combinations with no success. The interesting thing is that the give example adds one unrelated tag named NonProfit with ID 1. – Alain Schaefer Jan 31 '16 at 0:05
3

A few advices :

  • when you chain, you shouldn't have to define the entity_table. If you want to define it anyway, the entity_table should be the database table - i.e. civicrm_contact in your case.
  • Label and id of the tag can be used in tag_id but i really encourage you to use the id. The label can change or be language dependent while the id could not change.

But to answer your question, in your case, the real problem is the array part. The EntityTag api is to create one tag assignation. I don't think there is a syntax to assign multiple tag.

It could be nice to be able to add several api.EntityTag.create but because the key is the api call definition, it's impossible right now (or i don't know how).

So while this will work fine :

$result = civicrm_api3('Contact', 'create', array(
  'contact_type' => "Individual",
  'first_name' => "xxx",
  'last_name' => "yyy",
  'api.EntityTag.create' => array('tag_id' => 5),
));

If you have multiple tags, i suggest you do :

$result = civicrm_api3('Contact', 'create', array(
  'contact_type' => "Individual",
  'first_name' => "xxx",
  'last_name' => "yyy",
));
$contact_id = $result['id'];
foreach ($tags as $tag_id) {
  civicrm_api3('EntityTag', 'create', array(
    'entity_table' => 'civicrm_contact',
    'entity_id' => $contact_id,
    'tag_id' => $tag_id,
  );
}

EDIT

For multiple tags, you could try (not tested but work for website for example - https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Contact/ChainTwoWebsites.php)

$result = civicrm_api3('Contact', 'create', array(
  'contact_type' => "Individual",
  'first_name' => "xxx",
  'last_name' => "yyy",
  'api.EntityTag.create' => array('tag_id' => 5),
  'api.EntityTag.create.2' => array('tag_id' => 8),
));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.