REST API v8: Setting primary email for account

Hi all,
I’m trying set account’s primary email via REST API but with no success.

Successfully created email address

POST {{suitecrm.url}}/V8/module
{
	"data": {
			"type": "EmailAddresses",
			"attributes": {
					"email_address": "some.address@mail.com"
			}
	}
}

Successfully created relationship between account and email address

POST {{suitecrm.url}}/V8/module/Accounts/{{account.id}}/relationships
{
  "data": {
    "type": "EmailAddresses",
    "id": "{{email.address.id}}"
    
  }
}

Tried to use email1 field for updating account’s primary email, but with no success. The below request has no effect (account still has old primary email).

PATCH {{suitecrm.url}}/V8/module
{
  "data": {
    "type": "Accounts",
    "id": "{{account.id}}",
    "attributes": {
      "email1": "some.address@mail.com",
    }
  }
}

SuiteCRM ver 8.4.2.

I would appreciate for any suggestions.

Best Regards,
Illia

Why don’t you update email address directly in the database using API.

Maybe it will work.

You must set the email address bean’s attribute primary_address to 1.

1 Like

Hi chris001,

I see the different response format (from the one in the referred topic) for
/V8/module/Accounts/{{account.id}}/relationships/email_addresses
request.
And I do not see primary_address field though the corresponding column is present in email_addr_bean_rel table. I suspect the response data depends on the SuiteCRM (minor) version.

{
    "meta": {
        "total-records": 4,
        "total-pages": 1,
        "records-on-this-page": 4
    },
    "data": [
        {
            "type": "EmailAddress",
            "id": "884d8827-d503-5f2b-81f6-662f97a30e98",
            "attributes": {
                "email_address": "crm-acc-78934@gmail.com",
                "email_address_caps": "CRM-ACC-78934@GMAIL.COM",
                "invalid_email": "0",
                "opt_out": "0",
                "confirm_opt_in": "not-opt-in",
                "confirm_opt_in_date": "",
                "confirm_opt_in_sent_date": "",
                "confirm_opt_in_fail_date": "",
                "confirm_opt_in_token": "",
                "date_created": "2024-04-29T12:52:00+00:00",
                "date_modified": "2024-04-29T12:52:00+00:00",
                "deleted": "0"
            },
            "links": {
                "self": "V8/module/EmailAddress/884d8827-d503-5f2b-81f6-662f97a30e98"
            }
        },
        {
            "type": "EmailAddress",
            "id": "4c76c3b4-f77a-7c8f-3c19-662f84d853d3",
            "attributes": {
                "email_address": "crm-acc-2507-02@gmail.com",
                "email_address_caps": "CRM-ACC-2507-02@GMAIL.COM",
                "invalid_email": "0",
                "opt_out": "0",
                "confirm_opt_in": "not-opt-in",
                "confirm_opt_in_date": "",
                "confirm_opt_in_sent_date": "",
                "confirm_opt_in_fail_date": "",
                "confirm_opt_in_token": "",
                "date_created": "2024-04-29T11:27:00+00:00",
                "date_modified": "2024-04-29T11:27:00+00:00",
                "deleted": "0"
            },
            "links": {
                "self": "V8/module/EmailAddress/4c76c3b4-f77a-7c8f-3c19-662f84d853d3"
            }
        },
	...
	...

As for now, I’m not sure whether there is a way of updating primary email via API.
I see the direct database update option only.

Hi rsp,

The primary_address flag is not a part of EmailAddress object. It is an attribute of EmailAddress/Account relationship.

Hi @ilsid
You’re right, primary address is on the relationship.
Could you try your same API v8 calls on the online demo, which is current version Suite 8.6, and see if it returns the information you expect, primary_address.

Hi @chris001 ,

I do not see a way of creating OAuth2 client (for getting API access token) on the demo env. The Admin menu is not available for the demo user.

Is there a way you could run the bitnami docker container of 8.6 on your local computer and then try your same API v8 calls on there.

@chris001 , I tried version 8.6.0 (image suitecrm:8.6.0-debian-12-r3). I see the same response result for
/V8/module/Accounts/{{account.id}}/relationships/email_addresses
There is no primary_address field in the response

{
    "meta": {
        "total-records": 2,
        "total-pages": 1,
        "records-on-this-page": 2
    },
    "data": [
        {
            "type": "EmailAddress",
            "id": "4be17a98-5243-d8f4-bf31-6638bc90aab2",
            "attributes": {
                "email_address": "crm-acc-10@yahoo.com",
                "email_address_caps": "CRM-ACC-10@YAHOO.COM",
                "invalid_email": "0",
                "opt_out": "0",
                "confirm_opt_in": "not-opt-in",
                "confirm_opt_in_date": "",
                "confirm_opt_in_sent_date": "",
                "confirm_opt_in_fail_date": "",
                "confirm_opt_in_token": "",
                "date_created": "2024-05-06T11:19:00+00:00",
                "date_modified": "2024-05-06T11:19:00+00:00",
                "deleted": "0"
            },
            "links": {
                "self": "V8/module/EmailAddress/4be17a98-5243-d8f4-bf31-6638bc90aab2"
            }
        },
        {
            "type": "EmailAddress",
            "id": "5d8c9e69-ebba-573b-7349-6638bc25f413",
            "attributes": {
                "email_address": "crm-acc-10-02@yahoo.com",
                "email_address_caps": "CRM-ACC-10-02@YAHOO.COM",
                "invalid_email": "0",
                "opt_out": "0",
                "confirm_opt_in": "not-opt-in",
                "confirm_opt_in_date": "",
                "confirm_opt_in_sent_date": "",
                "confirm_opt_in_fail_date": "",
                "confirm_opt_in_token": "",
                "date_created": "2024-05-06T11:20:00+00:00",
                "date_modified": "2024-05-06T11:20:00+00:00",
                "deleted": "0"
            },
            "links": {
                "self": "V8/module/EmailAddress/5d8c9e69-ebba-573b-7349-6638bc25f413"
            }
        }
    ]
}

The standard approach of creating a new email address record and relating it to the Account with the primary flag set to 1 doesn’t work as expected in SuiteCRM versions 7.8 and above, by general consensus.

Workaround:

Create a new email address record, and then update the existing related primary email address on the Account to mark it as non-primary, followed by setting the new email address as primary. Here’s the steps involved:

  1. Create a new Email Address record with the desired email address. You’ve done this.
  2. Retrieve the existing related email address(es) of the Account.
  3. Update the existing email address(es) by setting the primary flag to 0.
  4. Relate the newly created email address to the Account. You’ve done this.
  5. Update the newly related email address by setting the primary flag to 1.

@chris001,

So, steps 3 and 5 assume a direct database access, correct?
Maybe I’m missing something, but I didn’t find a way of updating the primary_address field in email_addr_bean_rel table via API

Steps 1-5 (including 3 and 5) are all REST API v8.
If there truly is no field for primary_email, then that would be a bug.
However, I’m sure primary_email is accessible somewhere in the REST API v8.

primary_email is not a EmailAddress field. It is relationship’s field ( email_addr_bean_rel table). I didn’t find a way of updating (and getting) it via API’s.
That’s why I created this topic :slight_smile:

While the “primary_address” field is stored in “email_addr_bean_rel”, the REST API v8 doesn’t expose a way to modify it directly.

This is a two step process: How to set primary_address for an Account record:

  1. Create a new email address record if it doesn’t already exist for the desired email. You’re doing this successfully already.
POST {{suitecrm.url}}/V8/module
{
	"data": {
			"type": "EmailAddresses",
			"attributes": {
					"email_address": "some.address@mail.com"
			}
	}
}
  1. Create a relationship between the Account and the email address using the v8 API. You were doing this, however you failed to include the field primary_address. Be sure to include the field primary_address, and set it to 1.
POST {{suitecrm.url}}/V8/module/Accounts/{{account.id}}/relationships
{
  "data": {
    "type": "EmailAddresses",
    "id": "{{email.address.id}}",
    "primary_address": "1"
  }
}

I tried this before creating this topic.
I’m getting an error.
Looks like a bug. As a (temporary) workaround, I will implement this via direct database update.

{
    "errors": {
        "status": 400,
        "title": null,
        "detail": "The option \"primary_address\" does not exist. Defined options are: \"id\", \"type\"."
    }
}