# Tạo phiếu ghi/lead/deal kèm thông tin trường động

**Bài toán:**  Nhu cầu tích hợp CareSoft vào website bán hàng Online. Trong đó khi phát sinh đơn hàng, thì tạo phiếu ghi. và gán phân loại đơn hàng là "mới" kèm giá trị đơn hàng là tổng tiền của đơn hàng trên CRM&#x20;

Trên cấu hình trường động phiếu ghi, đã cấu hình 2 trường dữ liệu gồm:\
\- Trạng thái Đơn Hàng-  Chọn 1 trong các giá trị: Mới tạo, Đã Xuất Kho, Đã Giao Hàng, Hủy đơn\
\- Giá trị đơn hàng: - Kiểu số&#x20;

### Chuẩn bị

1. Gọi API lấy trường động dữ liệu phiếu ghi, xem tại [Trường động (Custom fields)](/thong-tin-chung/truong-dong-custom-fields.md) có giá trị như sau  (Dữ liệu mô phỏng)   \
   **GET**: `{{domain}}/api/v1/tickets/custom_fields`

{% code title="Dữ liệu mô phỏng giá trị trường động" %}

```json
{
    "code": "ok",
    "custom_fields": [
        {
            "custom_field_id": 6416,
            "code":"TRANGTHAIDONHANG",
            "custom_field_lable": "Trạng thái đơn hàng",
            "type": "Single drop-down list",
            "values": [
                {
                    "id": 90923,
                    "label": "Mới",
                    "code":"12ABC",
                    "parent_value_id": -1
                },
                {
                    "id": 90924,
                    "label": "Đã xuất kho",
                    "code":"XUATKHO",
                    "parent_value_id": -1
                },
                {
                    "id": 90925,
                    "label": "Đã giao hàng",
                    "code":"DAGIAO",
                    "parent_value_id": -1
                }
            ]
        },
        {
            "custom_field_id": 6422,
            "code":"GIATRI",
            "custom_field_lable": "Giá trị đơn hàng",
            "type": "Numeric"
        }
    ]
} 
```

{% endcode %}

2. Tạo 1 bảng mapping thông tin  custom\_field\_id  với các trường thông tin tương ứng trên CRM

### Thực hiện

1. Tạo phiếu ghi khi có đơn hàng mới. Tình huống giả định: Anh Nam mua điện thoại có giá trị 5.000.000 đ\
   \
   Tạo object phiếu ghi để thực hiện tạo mới bằng cách gọi tới api \
   **POST**: `{{domain}}/api/v1/tickets`<br>

   <pre class="language-json" data-title="Click vào từng giá trị để xem thông tin" data-overflow="wrap"><code class="lang-json">{
       "ticket": {
           "phone": "09****0148",
           "username": "Anh Nam",
           "service_id": 12,
           "<a data-footnote-ref href="#user-content-fn-1">ticket_subject</a>": "Đơn hàng #122211",
           "ticket_comment": "Khách đặt 3 iphone",
           "is_public": 0,
           "custom_fields": [
               {
                   "id": "<a data-footnote-ref href="#user-content-fn-2">6416</a>",
                   "value": "<a data-footnote-ref href="#user-content-fn-3">90923</a>" 
               },
               {
                   "id": "<a data-footnote-ref href="#user-content-fn-4">6422</a>",
                   "value": "<a data-footnote-ref href="#user-content-fn-5">5000000</a>"
               }
           ]
       }
   }
   </code></pre>

\
Hoặc theo Code trường động

```json
{
    "ticket": {
        "phone": "09****0148",
        "username": "Anh Nam",
        "service_id": 12,
        "ticket_subject": "Đơn hàng #122211",
        "ticket_comment": "Khách đặt 3 iphone",
        "is_public": 0,
        "custom_fields": [
            {
                "code": "TRANGTHAIDONHANG",
                "value_code": "DAXUATKHO" 
            },
            {
                "code": "GIATRI",
                "value": "5000000"
            }
        ]
    }
}
```

Mô phỏng Postman tạo phiếu ghi

<figure><img src="/files/0Z9T2WtG65mHswSoKlpW" alt=""><figcaption><p>Lưu lại ticketId để thực hiện cập nhật phiếu ghi ở tiến trình tiếp theo </p></figcaption></figure>

Kết quả hiển thị trên giao diện CareSoft.

<figure><img src="/files/EMPaHhkHjsbvOESNLjng" alt=""><figcaption></figcaption></figure>

Để cập nhật thêm trạng thái xem thêm ở [Phiếu ghi](/danh-muc/restful-api-cua-caresoft/phieu-ghi.md)

> Đối với các tác vụ tạo lead/deal thực hiện thay đổi lại endpoint, object  và làm tương tự.&#x20;

[^1]: Chủ đề phiếu ghi sử dụngt

[^2]: ID của trường "Trạng thái đơn hàng"

[^3]: ID của giá trị phân loại **"Mới"** của trường **"Trạng thái đơn hàng"**

[^4]: ID của trường **Giá trị đơn hàng**

[^5]: Số tiền giá trị đơn hàng từ CRM đẩy trực tiếp sang


---

# 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.caresoft.vn/danh-muc/restful-api-cua-caresoft/tao-phieu-ghi-lead-deal-kem-thong-tin-truong-dong.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.
