2

I'm trying to update a contact and, especially, the email address. As far as I can tell using the API explorer, this is the correct JavaScript code for that:

            crmApi('Contact', 'create', {
          id: myContact.id,
          first_name: myContact.first_name,
          last_name: myContact.last_name,
          middle_name: myContact.middle_name,
          "api.Email": myContact.email
        })

It correctly updates the names, but not the email. Does anyone see what I have wrong?

2
  • have you tried to put "email" as the key?
    – Xavier
    Jun 10, 2018 at 21:28
  • "api.Email": myContact.email should be "api.Email.create": myContact.email
    – user4278
    Jun 11, 2018 at 0:44

2 Answers 2

4

There are many ways you can update email for contact

  1. Using Email api with create action

    CRM.api3('Email', 'create', {"email": "[email protected]",  "id": 201}).done(function(result) { // do something});
    
  2. Using Email api with replace action

  CRM.api3('Email', 'replace', {
      "values": {"0":{"email":"[email protected]","location_typ_id":"Home"}},
      "contact_id": 203
    }).done(function(result) {
      // do something
    });
  1. Using Contact api with Email chain api
CRM.api3('Contact', 'create', {
  "id": 203,
  "api.Email.replace": {"values":{"0":{"email":"[email protected]","location_typ_id":"Home"}}}
}).done(function(result) {
  // do something
});

HTH

Pradeep

3
  • 3. gave me what I needed. Adding the values object solved my problem. Many thanks! Jun 12, 2018 at 15:21
  • I could use some similar advise with Address chaining. This snippet (below) does what I need for email and phone, but not street_address. (No error, but nothing changes.) Is there different syntax I should use for address? crmApi('Contact', 'create', { id: myContact.id, last_name: myContact.last_name, "api.Email.replace": {"values":{"0":{"email":myContact.email}}}, "api.Phone.replace": {"values":{"0":{"phone":myContact.phone}}}, "api.Address.replace": {"values":{"0":{"street_address":myContact.street_address}}}, etc. Jun 16, 2018 at 17:23
  • There is a limitation why people use 'replace' method to update email is it deletes all the emails before it creates one. So you have to make sure you pass all the emails in array that a contact has. Jun 16, 2018 at 17:37
0

Email addresses are in their own separate entity in the API named "Email". You would update the email address with that using the contact ID to connect the person to the email address.

Example from the API Explorer (from Civi 4.6):

CRM.api3('Email', 'create', {
  "contact_id": 1111,
  "email": "[email protected]"
}).done(function(result) {
  // do something
});
2
  • 1
    Is that the best practice when I'm trying to update a contact's info with a single Save button? Shouldn't I try to use the chain API pattern? Jun 10, 2018 at 18:25
  • Glad you got it sorted! Jun 12, 2018 at 17:46

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.