3

I need to add an email id to a CiviCRM contact through the API. Existing email ids of the contact should be kept unchanged and the new email added as type "Work" (the contact doesn't have any "Work" emails yet, if that should make it easier).

I tried to find a suitable API call through the API Explorer (civicrm/api/explorer) but the autocomplete didn't offer an Email field anywhere.

I also found an old forum post using some civicrm_location_update($params). But this left me confused and I can't find anything like it in the API Explorer.

4

Here is an example for creating a new email on an existing contact. Note that the default "Work" is location_type_id 2:

  $params = array(
    'contact_id' => 99,
    'location_type_id' => 2,
    'email' => 'myemail@work.com'

  try{
    $result = civicrm_api3('Email', 'create', $params);
  }
  catch (CiviCRM_API3_Exception $e) {
    // Handle error here.
    $errorMessage = $e->getMessage();
    $errorCode = $e->getErrorCode();
    $errorData = $e->getExtraParams();
    return array(
      'error' => $errorMessage,
      'error_code' => $errorCode,
      'error_data' => $errorData,
    );
  }
  • 4
    It's not necessary to pass option ids, as names will work just as well and are more readable, so you could write instead 'location_type_id' => 'Work' – Coleman Aug 18 '15 at 1:25

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.