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?

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

There are many ways you can update email for contact

  1. Using Email api with create action

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

  CRM.api3('Email', 'replace', {
      "values": {"0":{"email":"asasa@asas.com","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":"assdfsfdsasa@asas.com","location_typ_id":"Home"}}}
}).done(function(result) {
  // do something
});

HTH

Pradeep

  • 3. gave me what I needed. Adding the values object solved my problem. Many thanks! – Mike Steigerwald Jun 12 '18 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. – Mike Steigerwald Jun 16 '18 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. – Pradeep Nayak Jun 16 '18 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": "example@example.org"
}).done(function(result) {
  // do something
});
  • 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? – Mike Steigerwald Jun 10 '18 at 18:25
  • Glad you got it sorted! – Allen Hutchison Jun 12 '18 at 17:46

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.