> ## Documentation Index
> Fetch the complete documentation index at: https://developer.watson-orchestrate.ibm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# updateMessageStateUserDefined()

Updates the `user_defined` field inside a specific message's state. This provides a mechanism to store custom state on a message that will be reloaded if a thread is reloaded from history. The new data merges with existing data.

When a thread loads, this data can be found on the message at `message.message_state.content[<contentId>].user_defined`.

## Syntax

```javascript theme={null}
instance.updateMessageStateUserDefined(messageId, contentId, data);
```

## Parameters

**messageId** `string` *required*

The ID of the message to update.

**contentId** `string` *required*

The ID of the message content that must store the `user_defined` data.

**data** `unknown` *required*

The user-defined value to merge into the message state.

## Returns

**void**

Returns a `void` operator.

## Examples

The following example stores custom metadata on a message:

```javascript theme={null}
// Store custom metadata on a message
instance.updateMessageStateUserDefined(
    'msg-12345',
    'content-1',
    { customField: 'value', timestamp: Date.now() }
);

// Access the data when thread is reloaded
instance.on('pre:threadLoaded', (event, instance) => {
    event.messages.forEach((message) => {
        const userData = message.message_state?.content?.['content-1']?.user_defined;
        console.log('Custom data:', userData);
    });
});
```

> [card] [Do you need practical examples?](https://www.ibm.com/docs/SSAVQO/deploy/web_chat_agent/tutorials/tutorials_lp.html)
>
> Learn how to apply the features available for embedded chat into your implementation with guidance and examples.
