Setting Metadata Property in an Action Overwrites the Existing Value

Overview

In an action, when the api.user.setAppMetadata() or api.user.setUserMetadata() are used, the existing values inside the metadata get overwritten.

Applies To

  • Actions
  • setAppMetadata()
  • setUserMetadata()

Cause

This occurs when using an action to set a nested metadata property. Only the first-level object will be merged. See Updating an inner user metadata property for more details.

Solution

To update a nested property in a JSON object, such as adding a new value to an existing nested object, the entire nested object should be sent in the update request. If only the new value is sent, the existing nested properties will be overwritten.

For example, if there is an ‘office_contact’ under ‘contacts’ and the goal is to add a ‘home_contact’, then send the full ‘contacts’ object, including both ‘office_contact’ and ‘home_contact’.

{
  "metadata": {
    "profileID": 2024,
    "contacts": {
      "office_contact": "123-456-7890"
    }
  }
}

Request body to send:

{
  "metadata": {
    "contacts": {
      "office_contact": "123-456-7890",
      "home_contact": "987-654-3210"
    }
  }
}

After this update, the resulting metadata will include both contacts:
Sending the entire ‘contacts’ object ensures that the existing ‘office_contact’ is retained while adding the ‘home_contact’ property.

Updated ‘metadata’:

{
  "metadata": {
    "profileID": 2024,
    "contacts": {
      "office_contact": "123-456-7890",
      "home_contact": "987-654-3210"
    }
  }
}
1 Like