---
title: "Create a condition with the Code Assistant"
slug: "create-a-condition-with-the-code-assistant"
abstract: "How to quickly create a custom condition using the Code Assistant."
---
# Create a condition with the Code Assistant

> [!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.

The condition Code Assistant can:·

- **Generate new conditions** - Creates custom JavaScript conditions based on natural language descriptions.
- **Modify existing conditions** - Updates, refines, or fixes existing condition code.
- **Explain conditions** - Provides clear explanations of what a condition does and how it works.
- **Suggest conditions** - Recommends relevant condition ideas based on your personalization goals.

You can use the Code Assistant to quickly create a custom condition in Personalize. Whether you’re a developer who wants to save time or a marketer without JavaScript experience, the Code Assistant helps you generate code by simply describing what you need in natural language.

To create a custom condition with the Code Assistant:

1. In the Page builder **Personalize** mode, in the left-hand panel, click **Create variant**.
2. Enter a name for the page variant, then click **Next**.
3. In the **Create your audience** dialog, in the **Conditions** right-hand panel, click **Create**.
4. Enter a name for the new condition.
5. In the code editor, click **Code Assistant**.
6. In the **Code Assistant** left-hand pane, do one of the following:
    - Enter a prompt describing the logic you want to create. Be specific about the criteria, such as guest attributes (gender, city, country), time frames, and the expected return value.

        Here are some prompt examples:

        - *Write a condition that returns true if the guest’s birthday is within the next seven days*
        - *Return true if the customer has no date of birth value so we can prompt them to finish their profile setup*
        - *Return true if the most recent purchase was a web based purchase*
        - *Create a condition that returns true if the time a guest spent in their most recent session is over 1 minute*
        - *Return true for customers with an order of more than X in total in the past Y months (x and Y are configurable)*
        - *Look at every point of sale value and return true if any of them are X (make X a free text input)*
    - Select one of the suggested prompts.

    The Code Assistant generates the code. This might take a few minutes.

    > [!note]
    > The Code Assistant generates code based on your current prompt and any code that's present in the code editor. It does not consider your prompt history or previous codes in the editor.
    >
    > AI responses can vary. Running the same prompt more than once might produce different results.

    After the code is generated, it is labeled with a version number, such as *Code Output 1*.

7. To preview the generated code, click **View**.

    ![The AI-generated code is labeled with a version number in the left-hand pane.](https://doc.sitecore.com/api/markdown-image/sai/en/users/sitecoreai/images/AI_Assistant_Conditions_generated_code.png)

    > [!tip]
    > If you've entered multiple prompts, use the View buttons to compare different code outputs.

8. In the **Code Output** dialog:
    - To insert the generated code into the editor and replace any existing code, click **Replace code**.
    - To copy the generated code to your clipboard, click **Copy code**.

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

9. Optionally, after inserting the code, you can do the following in the code editor:
    - Make manual changes to the code if needed.
    - Click **Undo** :mdiUndo: to revert changes.
10. Click **Save**.