> ## 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

<ParamField path="messageId" type="string" required>
  The ID of the message to update.
</ParamField>

<ParamField path="contentId" type="string" required>
  The ID of the message content that must store the `user_defined` data.
</ParamField>

<ParamField path="data" type="unknown" required>
  The user-defined value to merge into the message state.
</ParamField>

## Returns

<ResponseField name="void">
  Returns a `void` operator.
</ResponseField>

## 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 title="Do you need practical examples?" icon="text-align-start" href="https://www.ibm.com/docs/SSAVQO/deploy/web_chat_agent/tutorials/tutorials_lp.html" arrow="true" cta="Learn with walk-throughs" horizontal>
  Learn how to apply the features available for embedded chat into your implementation with guidance and examples.
</Card>
