# Deals in the CRM system

Deals in the CRM are displayed as cards with information about the client, deal name, description and amount.

<div data-with-frame="true"><figure><img src="/files/PVGuoIpi8uJYJLvuG6qv" alt="" width="563"><figcaption></figcaption></figure></div>

You can add other fields, if needed (see fig. 8-12).

## "Create a deal" button

<div data-with-frame="true"><figure><img src="/files/hBTsWq5Iok8sV8q9L0Aa" alt="" width="563"><figcaption></figcaption></figure></div>

The **Create a deal** window lets you quickly set up and save a new deal.

<div data-with-frame="true"><figure><img src="/files/ZBl8iloQQRmACAppVZI6" alt="" width="563"><figcaption></figcaption></figure></div>

Fill out the form to create a client. If the client exists, MaviBot will prompt you to select them from the database.

<div data-with-frame="true"><figure><img src="/files/m13BEfLXElfYkifJodED" alt="" width="563"><figcaption></figcaption></figure></div>

1. If the client isn’t found, connect a WhatsApp or email bot to the project.
2. Message the client through the connected channel — this will automatically create their profile.
3. Select the funnel stage where the deal should be placed.

<div data-with-frame="true"><figure><img src="/files/3tZ66284eklm3fgrBzQB" alt="" width="563"><figcaption></figcaption></figure></div>

Deal variables can be also added in the modal form:

<div data-with-frame="true"><figure><img src="/files/HHMcSlCYlJkILBBBDJtz" alt="" width="563"><figcaption></figcaption></figure></div>

Next, click **"Create"** to add the deal to the system.

## The "Add quickly" button

This button can also help to manually add a new client to the CRM.

<div data-with-frame="true"><figure><img src="/files/2XAG6exFMg0Tjibp4JCS" alt="" width="308"><figcaption></figcaption></figure></div>

Click the button to open a window where you can enter the client's details.

<div data-with-frame="true"><figure><img src="/files/6lgoppkTax7f1UI5xYi1" alt="" width="308"><figcaption></figcaption></figure></div>

Fig. 26 Quick deal creation window (Note: All data is fictional; any resemblance is purely coincidental.)

Thus, the deal will be created in the **"Interested"** stage.

{% hint style="success" %}
Done!&#x20;

Now you know how to create deals.
{% endhint %}

## How to create a deal from the chatbot

Chatbot flow example for creating a deal via function

Process overview:

1. Create a Start block

<div data-with-frame="true"><figure><img src="/files/2Z7JfIhMlXqo6aZ9SY2k" alt=""><figcaption></figcaption></figure></div>

2. Create two button blocks.

<div data-with-frame="true"><figure><img src="/files/wmBjuGjcXyQZvx3lcKgx" alt=""><figcaption></figcaption></figure></div>

3. Go to the connection settings, enable the "User enters data" option, and specify the name of the service the user will input.

<div data-with-frame="true"><figure><img src="/files/uTwOH9JHdT1zCXsS70IE" alt=""><figcaption></figcaption></figure></div>

4. Then, add the question "What is your name?" in a white block. Create a new block below it and enable **"User enters data"** in its arrow settings.

<div data-with-frame="true"><figure><img src="/files/lKK7lAXSZo55Oe81GgG9" alt=""><figcaption></figcaption></figure></div>

The "User enters data" setting with the variable `name` is enabled in every connection leading to the "Enter your phone number" block.

5. Now, create two blocks — "Chat" and "Fallback" — from the "Enter your phone number" block.

<div data-with-frame="true"><figure><img src="/files/CD9cqIWL7E2j3E5g7Vx0" alt=""><figcaption></figcaption></figure></div>

1. Select the **"RegEx math"** type in the connection settings.
2. Enable the "User enters data" setting.
3. Enter the variable name: `phone`.

<div data-with-frame="true"><figure><img src="/files/cY2EkOyNPHWZbXpC7MiA" alt=""><figcaption></figcaption></figure></div>

Now, in the Fallback block, write the message: **"This is incorrect! Try again."**

This configuration will work as follows: if your client enters an incorrect phone number, they will receive the message from the Fallback block. The Fallback message will continue to be sent until the client enters a correct phone number.

6. Then, ask the client about their budget and immediately create a new block below it.

<div data-with-frame="true"><figure><img src="/files/CUaUQq0Ywtxu8LmuPYzo" alt=""><figcaption></figcaption></figure></div>

7. Enable the **"User enters data"** setting in the connection and enter the variable name: `budget`.

<div data-with-frame="true"><figure><img src="/files/gU9olp99Q99jchFQP5jh" alt=""><figcaption></figcaption></figure></div>

Activate the **"User enters data"** checkbox on the connection and specify the phone variable client\_name.

8. You can either change the type of the last block to "Leads" or use the `create_order` function.

   Here is the option with the red "Leads" block.

<div data-with-frame="true"><figure><img src="/files/wRjnx0Ketlrjtgrdnq4V" alt=""><figcaption></figcaption></figure></div>

Example.

<div data-with-frame="true"><figure><img src="/files/CVDRrhXzEuZRlqnZrMLg" alt=""><figcaption></figcaption></figure></div>

The function create\_order(name, budget, description, client\_name, phone, email, state\_id) is used to create a deal from the chatbot in the CRM.

<details>

<summary>Parameters</summary>

The current active deal in the funnel, along with variables, will be available only in the deal management and CRM functions.

<table><thead><tr><th width="303.3046875">Parameters</th><th>Meaning</th></tr></thead><tbody><tr><td><strong>name</strong></td><td><p>Deal name.</p><p>Optional parameter, if not passed, it will apply to the current active deal of the client</p></td></tr><tr><td><strong>budget</strong></td><td>Deal value (number).  An optional parameter, if not passed, will be applied to the client's current active deal. If you pass a non-number in the <strong>budget</strong> parameter, the function returns the response: <strong>'budget must be a number'</strong></td></tr><tr><td><strong>description</strong> </td><td>Deal description. Optional parameter;</td></tr><tr><td><strong>client_name</strong></td><td>(optional parameter) string, name for a new client</td></tr><tr><td><strong>phone</strong></td><td>(optional parameter) string, phone number for a new client</td></tr><tr><td><strong>email</strong></td><td>(optional parameter) string, email for a new client</td></tr><tr><td><strong>state_id</strong> </td><td>the number allows you to specify the state in which the deal will be after creation (an optional parameter)</td></tr></tbody></table>

{% hint style="info" %}
To create a client, the phone or email variable must be specified. If phone is specified and there is no client with such a phone in the project, a new one will be created.

If the email variable is specified without the phone variable, and there is no client in the project with this address, a new one will be created.
{% endhint %}

</details>

In the flow, we have collected the following data in stages:

* Name - deal name (in the example, I need a website & I need a landing);
* Budget — deal cost, which was announced in blocks after the client selected the type of website;
* Phone — the client's phone number
* Client\_name — the client's name

If you need to create a deal in a specific transaction state, pass the state\_id parameter.

{% hint style="info" %}
Important!&#x20;

If you do not pass any parameters, then pass none instead of the parameter (as in Fig. 36, where none is specified instead of the email parameter).
{% endhint %}

Where to find the state id (state\_id parameter)

<div data-with-frame="true"><figure><img src="/files/xgy8bduUKjUhW9VJUeBp" alt="" width="316"><figcaption></figcaption></figure></div>

Stage ID:

<div data-with-frame="true"><figure><img src="/files/ZLX4cxjePmMY4WCOxv6s" alt="" width="375"><figcaption></figcaption></figure></div>

## Other settings

The search bar is a search for a deal by the client's name.

<div data-with-frame="true"><figure><img src="/files/cb07GeuPzGh6b3emONYV" alt="" width="375"><figcaption></figcaption></figure></div>

Filter: allows you to filter deals with various parameters. You can also save the created filter if you use it frequently.

<div data-with-frame="true"><figure><img src="/files/EODzcPVOBka4oa53OMpY" alt=""><figcaption></figcaption></figure></div>

## How to trigger a bot when a deal moves to a new funnel stage

To trigger a bot upon a deal's stage change in the CRM funnel:

1. Go to the project settings.
2. Open the **"Callbacks"** section.

<div data-with-frame="true"><figure><img src="/files/r5aqXAnmCOgB8qeR4e4Q" alt=""><figcaption></figcaption></figure></div>

Click on **"CRM stage transition callback settings"**:

<div data-with-frame="true"><figure><img src="/files/n8Owu8UMdfXR4J4YRI6Y" alt=""><figcaption></figcaption></figure></div>

After that, you’ll see a modal window with all the funnels in the project. You can choose either the entire funnel or a specific stage.

<div data-with-frame="true"><figure><img src="/files/rH98g1TLTvc1UN3ec6qb" alt="" width="563"><figcaption></figcaption></figure></div>

Then save the settings.&#x20;

Now, when the deal state changes—either automatically or manually—a notification is generated from anywhere on the site, which can be used to trigger a bot response.

{% hint style="info" %}
The notification looks like this: **crm\_state\_changed Funnel name:Stage name**
{% endhint %}

The callback\_query\_id variable will also appear in the client’s card, storing the identifier of the deal for which the notification was sent.

<div data-with-frame="true"><figure><img src="/files/lIL2TtV1WVOUF6CWXl0E" alt="" width="563"><figcaption></figcaption></figure></div>

To trigger your bot in response to this event, enter the callback text into the trigger field of a connection or a trigger-based block (such as a Start or Trigger block).

<div data-with-frame="true"><figure><img src="/files/ZYahsUeQuAHHp2egqeUx" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
If you want to keep the client in the main chatbot flow, use the "Chat" block. This block cannot be transitioned to, so after reacting to the callback, the client stays in the main funnel while still receiving the message.

If you need to continue the funnel after a state change, use the "Start" block. The client will be redirected to the "Start" block in response to the callback, allowing the funnel to proceed from there.
{% endhint %}

### How to move a deal

To move a deal to any funnel stage, simply click and drag it with the left mouse button to the desired stage.

<div data-with-frame="true"><figure><img src="/files/B9Fi027iqF1NwRw8fi6v" alt="" width="563"><figcaption></figcaption></figure></div>

When a deal is moved to **"Failed"** or **"Archived,"** a modal window appears prompting you to specify the reason. This action creates a corresponding deal variable: `failed_reason` or `archived_reason`.

The reason provided is saved to the respective variable. If the deal is later restored, the variable `restored_from_failed` or `restored_from_archived` is added with the value `"true"`.

## How to prevent moving a deal to the specified stage

If you need to restrict the stages to which deals can be moved manually, configure the restriction in the state settings.

Open the funnel and go to the stage settings.

<div data-with-frame="true"><figure><img src="/files/xgy8bduUKjUhW9VJUeBp" alt="" width="316"><figcaption></figcaption></figure></div>

Use the stage settings to restrict deal movement to specific stages. Select the desired stages in the checklist (multiple selections allowed) and save the changes.

<div data-with-frame="true"><figure><img src="/files/GCyW5oC9pTEuXEb7ielO" alt="" width="563"><figcaption></figcaption></figure></div>

After saving, the restricted stages will be visually highlighted in MaviBot CRM. They will appear darker, and moving deals to them will be disabled. If stage movement restrictions are applied, those stages will also be hidden in the client chat interface.

## Reaction to deal deletion in CRM

When a deal is deleted in the CRM, the bot receives a callback named **`crm_lead_removed`** to notify the client.

<div data-with-frame="true"><figure><img src="/files/EJX9Ng1iZjkRZpw4tO52" alt="" width="465"><figcaption></figcaption></figure></div>

## How to switch deals

When actions are performed on deals directly within the CRM page, the following callbacks are triggered:

* `change_responsible_order` — the deal's assigned manager has changed.
* `crm_state_changed` — the deal was moved to a different funnel stage.
* `crm_system_state_changed` — the deal was moved to **Lost**, **Won**, or **Archived**.
* `crm_lead_removed` — the deal was deleted.

Additionally, the client variables will include `callback_order_id`, which contains the ID of the specific deal affected by the CRM action.

When processing one of these callbacks, the corresponding deal becomes temporarily active within the processing block, allowing you to access its variables directly.

Once the flow moves to the next block, MaviBot's standard logic resumes — any subsequent access to deal variables will return values from the client's most recent active deal.

### How to switch a deal to an active stage in the bot

The `switch_order(order_id)` function allows you to make a specific deal active in order to access its variables.

* `order_id` — an optional parameter for the specific deal ID. If omitted, the client's most recent deal becomes active.
* To activate a particular deal, pass its ID to the `switch_order(order_id)` function.
* If you call this function within a calculator block and provide a target deal ID, you will be able to access that deal's variables in all following blocks.

<div data-with-frame="true"><figure><img src="/files/HNb7RPHAwCFndPrNzwLa" alt="" width="563"><figcaption></figcaption></figure></div>

If the `switch_order` function is called with the ID of a deleted or non-existent deal, no switch will occur. In this case, the function will return the ID of the deal currently active for the client (or `None` if the client has no active deal).

You can therefore verify whether the switch was successful by comparing the `order_id` passed to the function with the value it returns:

`switch_result = switch_order(order_id)`

`result = if(switch_result == order_id, value_if_true, value_if_false)`

<div data-with-frame="true"><figure><img src="/files/d49iOt7YE8yoP4Hol5hx" alt="" width="563"><figcaption></figcaption></figure></div>

If no argument is passed to the `switch_order()` function, it will activate the client's most recent deal. This allows you to revert or reset the active deal — for example, after performing operations with another deal, or when you do not need to keep a deal temporarily activated by a CRM callback.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mavibot.ai/crm/setting/deals.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
