Announcement

Collapse
No announcement yet.

Update multiple contact emails (emailAddressData) via API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Update multiple contact emails (emailAddressData) via API

    I successfully update contact fields (e.g., fistName, emailAddressIsOptedOut, emailAddressIsInvalid) via API.

    The same code does not update the object field "emailAddressData". The API returns the 200 code (Ok). But the value of the field "emailAddressData" remains unchanged.

    Code:
    emailAddressData = [
      {
        "emailAddress": "name@domain1.com",
        "lower": "name@domain1.com",
        "primary": true,
        "optOut": false,
        "invalid": false
      },
      {
        "emailAddress": "name@domain2.com",
        "lower": "name@domain2.com",
        "primary": false,
        "optOut": true,
        "invalid": true
      }
    ]​
    Last edited by mfliorko; 12-17-2023, 10:12 AM.

  • #2
    Maybe the result in the form of a table is why!

    Comment


    • #3
      I’m not sure that I understand what you mean. Can you explain?

      I send contact data to the server via PUT (update) API call in the same format as it comes from the server via GET call. And the PUT call returns the success (200) response. Other fields are updated in the same PUT call. So, the call and payload data look good.

      Comment


      • #4
        It looks like the same functionality works differently for (A) web users and (B) API users.

        I created two similar users with the same role.
        The web user can modify "Opted Out" and "Invalid" attributes of contact's emails.
        The API user cannot modify these attributes - API calls return Ok (200) code and silently ignore requested changes for "Opted Out" and "Invalid" attributes of contact's emails.

        Vadym, tanya - can you check and confirm it?

        Comment


        • #5
          Both web and api users use the same api endpoints.

          The attribute emailAddressData should work. If you want to add to an existing list, first you need to fetch the record (e.g. a Contact). Add the new number to emailAddressData and then send PUT request with the payload: {"emailAddressData": [...]}.

          If you open the browser console (F12) > Network tab, you can see what API requests the front-end is sending when your are editing email/phone fields.​

          Comment


          • #6
            Hi rabii , thank you for your contribution.

            In previous research I compared my code (in Google Apps Script) with one I got via (F12) > Network. And the only difference I found: Web UI calls used tokens and other authentication headers.

            first you need to fetch the record (e.g. a Contact)
            Unfortunately, it's not fully true. Look at the code below grabbed from (F12) > Network. It sends to the server just one field "emailAddressData", not the whole Contact object.

            Code:
            fetch("https://espo.domain.com/api/v1/Contact/6...d", {
              "headers": {
                "accept": "*/*",
                "accept-language": "en-US,en;q=0.9,uk-UA;q=0.8,uk;q=0.7",
                "authorization": "Basic d...Y=",
                "espo-authorization": "d...Y=",
                "espo-authorization-by-token": "true",
            ​    "cache-control": "no-cache",
                "content-type": "application/json",
                "pragma": "no-cache",
                "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"macOS\"",
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-origin"
              },
              "referrer": "https://espo.domain.com/",
              "referrerPolicy": "strict-origin-when-cross-origin",
              "body": "{\"emailAddressData\":[{\"emailAddress\":\"name@domain1.com\",\"primary\":true,\"optOut\":false,\"invalid\":false,\"lower\":\"name@domain1.com\"},{\"emailAddress\":\"name@domain2.com\",\"primary\":false,\"optOut\":true,\"invalid\":true,\"lower\":\"name@domain2.com\"}]}",
              "method": "PUT",
              "mode": "cors",
              "credentials": "include"
            });​
            This data is successfully stored, e.g.,
            Code:
            emailAddressData[1].optOut === true
            emailAddressData[1].invalid === true
            When I replace the following lines in the code above:
            Code:
            "authorization": "Basic d...Y=",
            "espo-authorization": "d...Y=",
            "espo-authorization-by-token": "true",
            ​​
            by
            Code:
            "X-Api-Key": "7a...c0",
            the attributes "optOut" and "invalid" do not change their values on the server. Other fields (e.g., "firstName) in the same API calls are changed successfully.

            And I granted the full permissions to the API user.
            Last edited by mfliorko; 12-22-2023, 02:18 AM.

            Comment


            • #7
              Can you share your code here so we could look at and assist to resolve this issue.

              Comment


              • #8
                It looks like the reason why email address attributes were not updated:
                Code:
                emailAddressData[1].optOut === true
                emailAddressData[1].invalid === true​
                was because this email was registered as address of EspoCRM user.

                Other email addresses are updated as intended.

                Comment

                Working...
                X