restcivicrm

Set CivCRM Email to Blank by REST API


I am trying to set the email address to blank in CiviCRM. I serialise an array as below and post via REST (code is groovy):

def rest = [
                json: 1,
                api_key: apiKey,
                key: siteKey,
                debug: 1,
                version: 3,
                entity: 'Contact',
                action: 'create',
                contact_type: contact_type,
                overwriteblank:true,
                id: record.crID,
                rest["email[1][email]"] = modified.value as String
                rest["email[1][location_type_id]"] = 1
        ]

I have tried the 'overwriteblank' option - but the email still does not seem to be set to blank.

Thanks


Solution

  • An email address is a separate API entity from the contact (since a contact can have an infinite number of email addresses). All you need to do is get the email entries where the contact_id = whatever and then delete them.

    $result = civicrm_api3('Email', 'get', array('contact_id' => $YOUR_CONTACT_ID));
    

    then after checking for errors and iterating over $result['values']:

    $deleted = civicrm_api3('Email', 'delete', array('id' => $YOURRESULTROW['id']));
    

    With the SQL method, you're not deleting the email so much as setting that person's email to be an empty string.