2

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": "cio@agloa.org"
  }
]

... to have first_name: "Mike" and email: "mike@agloa.org". So, I tried:

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

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": "cio@agloa.org"
  },
  {
    "id": 2,
    "first_name": "Mike",
    "email.id": 11,
    "email.email": "mike@agloa.org"
  }
]

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

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":"mike@agloa.org", "id":2}]}]}
})

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.