3

Using civicrm api4, what's the best way to chain save a contact so that the email updates, without creating a new one?

For example, I want to update this existing contact:

entity: Contact, action: get, version: 4, count: 1
(1) [
  {
    "id": 2,
    "first_name": "Michael",
    "email.email": "[email protected]"
  }
]

... to have first_name: "Mike" and email: "[email protected]". So, I tried:

crmApi4('Contact', 'save', {
  records: [{"id":2, "first_name":"Mike"}],
  chain: {"name_me_0":["Email", "save", {"records":[{"contact_id":"$id", "email":"[email protected]"}]}]}
})

That correctly updates the first name, but creates a new email:

entity: Contact, action: get, version: 4, count: 2
(2) [
  {
    "id": 2,
    "first_name": "Mike",
    "email.id": 2,
    "email.email": "[email protected]"
  },
  {
    "id": 2,
    "first_name": "Mike",
    "email.id": 11,
    "email.email": "[email protected]"
  }
]

How can I get it to simply update the existing email?

1 Answer 1

2

I got what I needed by including the email id ("id":2) in the records chain property:

crmApi4('Contact', 'save', {
  records: [{"id":2, "first_name":"Mike"}],
  chain: {"name_me_0":["Email", "save", {"records":[{"contact_id":"$id", "email":"[email protected]", "id":2}]}]}
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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