intercom

Intercom: How can i create a custom attribute on a contact?


I am using the Ruby API and the following code gets an error:

intercom_query = intercom.contacts.search('query': {'field': 'external_id', 'operator': '=', 'value': 1})
contact = intercom_query.first
contact.custom_attributes = {"kamil_attribute" => "here-iam-update-me"}
intercom.contacts.save(contact)

"(Custom attribute 'kamil_attribute_console' does not exist)"


Solution

  • Think will need to try creating the attribute first

    intercom.data_attributes.create({ name: "kamil_attribute", model: "contact", data_type: "string" })
    

    If the attribute doesn't currently exist, the API docs mention that need to create it first

    Creating new Custom Data Attributes

    You can only write to custom data attributes that already exist on the workspace. If you need to create new attributes to write to, you should Create Data Attributes through the Data Attributes API.

    Note that there is a permission needed on the Access Token in order to create attributes so you may need to add that if you don't currently have it and then regenerate the token and then update the token value in any code being used enter image description here

    intercom-ruby readme on data attributes

    # Create a new custom data attribute
    intercom.data_attributes.create({ name: "test_attribute", model: "contact", data_type: "string" })
    
    # List all data attributes
    attributes = intercom.data_attributes.all
    attributes.each { |attribute| p attribute.name }
    
    # Update an attribute
    attribute = intercom.data_attributes.all.first
    attribute.label = "New label"
    intercom.data_attributes.save(attribute)
    
    # Archive an attribute
    attribute.archived = true
    intercom.data_attributes.save(attribute)
    

    It will throw an error if the attribute exists though so you could check if the attributes exist before creation or perhaps if there are errors to create the attribute when you catch the exception and then retry the update.