---
title: "Create custom values"
slug: "create-custom-values"
abstract: "Create a custom value manually or use the Code Assistant to generate JavaScript from natural language prompts."
---
# Create custom values

> [!important]
> Feature availability is part of a phased rollout. Your organization may not see this functionality yet. It will become available when your environment is included in the rollout.

Custom values let you create your own JavaScript-based logic for [component personalization](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize-and-test/component-personalization.html "Component personalization").

Unlike profile data or conditions, custom values can combine profile attributes, visitor behavior, and custom logic into a single value that you can use as an input in decision tables for audience targeting.

You can create custom values manually or generate the JavaScript with the Code Assistant.

## Create a custom value using JavaScript

If you are familiar with JavaScript, you can create a custom value manually by writing your own logic in the code editor.

To create a custom value manually:

1. You can start in either of these ways:

- From a decision table: [Create a new decision table](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize-and-test/component-personalization/personalize-a-component.html#define-decision-table-inputs "Define decision table inputs"), select **Custom value**, and then click **Create new**.
- From the navigation menu: Click **Optimization**. In the left-hand pane, click **Custom values**.

Then:

2. On the **Custom Values** page, click **Create**.
3. Enter a name for the custom value.
4. Click **Create**.
5. In the code editor, enter or edit the JavaScript for the logic you want to use.

    For example, the following custom code returns the `lastPage` value from the visitor’s most recent session:

    ```js
    (function() {
      if (!profile || !Array.isArray(profile.sessions) || profile.sessions.length === 0) {
        return null;
      }

      const latestSession = profile.sessions[0];
      if (!latestSession.stats || typeof latestSession.stats.lastPage !== 'string') {
        return null;
      }

      return latestSession.stats.lastPage;
    })()
    ```

6. On the **Overview** tab in the right pane:
    - Enter a short description for the custom value.
    - Select or create tags to help organize and identify it.
    - Select the **Return type**, such as *String*, *Number*, *Boolean*, *Date*, or *Array*.
7. Click **Save**.

## Create a custom value with the Code Assistant

If you are not familiar with JavaScript, you can use the Code Assistant to generate a custom value from a natural language prompt.

To create custom data with the Code Assistant:

1. [Create a new decision table](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize-and-test/component-personalization/personalize-a-component.html#define-decision-table-inputs "Define decision table inputs") and select **Custom value**.
2. Click **Create new**.
3. On the **Custom Values** page, click **Create**.
4. Enter a name for the custom value.
5. Click **Create**.
6. In the **Code Assistant** pane, do one of the following:
    - Enter a prompt describing the logic you want to create.

        When writing prompts, be specific about visitor attributes, time frames, conditions, and the expected return value.

        Example prompts:

        - *Return true if the visitor’s birthday is within the next seven days.*
        - *Categorize visitors as high, medium, or low intent based on session count and page views.*
        - *Return true if the visitor viewed more than five product pages.*
    - Select one of the suggested prompts.

    The Code Assistant generates JavaScript based on the current prompt and any code already in the editor.

    > [!note]
    > AI-generated responses can vary. Running the same prompt multiple times may produce different results.

7. To preview the generated code, click **View**.
8. In the dialog:
    - Click **Replace code** to insert the generated code into the editor.
    - Click **Copy code** to copy the generated code to your clipboard.

    > [!important]
    > AI-generated code may not always be accurate. Always review and test the generated code before using it.

9. Optionally, after inserting the code:
    - Make manual changes in the code editor.
    - Click **Undo** :mdiArrowULeftTop: to revert changes.
10. Click **Save**.

## Next steps

After saving the custom value:

- [Test the custom value](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize-and-test/component-personalization/decision-tables/decision-table-inputs/manage-custom-values.html#test-a-custom-value "Test a custom value") using a guest profile to verify that it returns the expected values.
- [Publish the custom value](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize-and-test/component-personalization/decision-tables/decision-table-inputs/manage-custom-values.html#publish-a-custom-value "Manage custom values") to make it available for use as an input to the decision t able in component personalization.