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\"."
    }
}

Hello everyone!

Same problem here and i am on 8.9.1. I have tried all of the above and experimented with new creation vs updating existing. Assumed that maybe it would auto set primary on the only/new email record (in the relationship) . AI was helpless, Docs are not covering this, and all my testing provided no positive results. So just wanted to confirm that there is still no way to do this via API on 8.9.1 or newer (8.9.3 at this time)?
Thank you!

Hello Erick,

I’ve just applied this commit manually to my 8.9.3 and then it worked:

It has been part of the 7.15.1 release:

We 8.x folks probably still have to wait until the 8.10 release to get the 7.15 features + the merged bugfixes.

1 Like

Hi Bastian,

Thank you for your informative answer!
I am a little on the edge of applying the commit manually vs waiting for the official upgrade. So I do hope we see v.8.10.x very soon. I couldn’t tell if it was planned on 2027 or sooner from the link you provided.

One question, may make my decision simple, is : Do you think there is any harm in having API create contact record, then link to account, then add email record, then create link to contact and leave the email not being set to Primary ? I will have my users update the email to primary in the GUI when they do get to see/work on that record, but i am wondering if anything else might break.

What are your thoughts?

Probably nothing will break, doing it in this way.
The only issue is the data needs to be manually corrected.

If it’s just a couple of records and you’d upgrade the CRM and the upgrade will bring the fix and you’d change your API workflow after the upgrade and that all soon, totally fine.
In that case, you’ve got a temporary workaround, which stays temporary.
If busy day to day takes over, workaround sits there for longer, data is not being corrected manually .. that’s the first system decay :wink:

What you could do as well: Simply copy & paste the few new lines of code into your file on the server. During the next upgrade, that file should be overridden anyway.
Not the cleanest approach - but technically the easiest.

As for the 8.10:

Hopefully today or tomorrow

OMG I didn’t realize it is so close. I am definitely waiting for 8.10.0 probably 8.10.1 or .2 even. I was more concerned about spending time on n8n flows and then finding out the upgrade went slightly different route than making few lines of code change. Thank you again for good advise and clear answer.

1 Like