Custom Actions ใน Frappe CRM

Custom Actions ช่วยให้คุณสามารถเพิ่มปุ่มการทำงานบนส่วน Header ของหน้า Lead หรือ Deal ได้ตามต้องการ ไม่ว่าจะเป็นปุ่มเดี่ยว จัดกลุ่มปุ่มไว้ในเมนูดรอปดาวน์ หรือสร้างปุ่มดรอปดาวน์ที่มีป้ายกำกับชื่อ ซึ่งแต่ละปุ่มสามารถกำหนดโค้ดคำสั่งให้ทำงานได้ทันทีเมื่อคลิก หากต้องการเชื่อมโยงการทำงานระหว่างระบบขายและระบบธุรกิจทั้งหมด สามารถศึกษาเพิ่มเติมได้ที่ บริการระบบ Nextio ERPNext

Creating Custom Actions

Custom Actions จะทำงานอยู่ภายใน CRM Form Script เริ่มต้นโดยเปิดเมนู CRM Form Script ใน Desk เลือก DocType ที่ต้องการ (Lead หรือ Deal) และเขียน Class ขึ้นมา จากนั้นกำหนดค่า this.actions ภายในฟังก์ชัน onLoad():

class CRMDeal {
    onLoad() {
        this.actions = [
            {
                label: "Open in Desk",
                onClick: (close) => {
                    let URL = `https://frappecrm.frappe.cloud/app/crm-deal/${this.doc.name}`
                    window.open(URL, '_blank')
                }
            }
        ]
    }
}

คำสั่งนี้จะเพิ่มปุ่มที่ชื่อ "Open in Desk" ซึ่งจะเปิดดีลปัจจุบันในแท็บใหม่

![](/files/Screenshot 2026-05-26 at 5.51.52 PM.png) ปุ่ม "Open In Desk" บริเวณมุมบนขวาของ Header

Grouped Actions (เมนูสามจุด)

ใช้ตัวเลือก group เพื่อรวมเมนูย่อยต่างๆ ไว้ใต้ไอคอนดรอปดาวน์รูปสามจุด (more-horizontal) และตั้งค่า hideLabel: true เพื่อซ่อนหัวข้อกลุ่มไม่ให้แสดงในเมนูดรอปดาวน์

class CRMDeal {
    onLoad() {
        this.actions = [
            {
                group: "Add",
                hideLabel: true,
                items: [
                    { label: "Create Quotation", onClick: (close) => {} },
                    { label: "Create Sales Order", onClick: (close) => {} },
                ]
            },
            {
                group: "Delete",
                items: [
                    { label: "Delete Quotation", onClick: (close) => {} },
                    { label: "Delete Sales Order", onClick: (close) => {} },
                ]
            }
        ]
    }
}

![](/files/Screenshot 2026-05-26 at 5.50.39 PM.png)

Grouped Actions with Label (ปุ่มดรอปดาวน์แบบมีป้ายกำกับ)

เพิ่ม buttonLabel เพื่อเปลี่ยนไอคอนสามจุดให้กลายเป็นปุ่มดรอปดาวน์ที่มีข้อความกำกับ

class CRMDeal {
    onLoad() {
        this.actions = [
            {
                buttonLabel: "Create",
                group: "Add",
                hideLabel: true,
                items: [
                    { label: "Create Quotation", onClick: (close) => {} },
                    { label: "Create Sales Order", onClick: (close) => {} },
                ]
            }
        ]
    }
}

![](/files/Screenshot 2026-05-26 at 5.53.36 PM.png)

Mixing Single Buttons and Groups

คุณสามารถผสานทั้งปุ่มเดี่ยวและกลุ่มเมนูดรอปดาวน์ไว้ใน Array เดียวกันได้:

class CRMDeal {
    onLoad() {
        this.actions = [
            {
                label: "Refresh Score",
                icon: "refresh-cw",
                onClick: (close) => { /* ... */ }
            },
            {
                group: "More",
                hideLabel: true,
                items: [
                    { label: "Export PDF", onClick: (close) => {} },
                    { label: "Duplicate", onClick: (close) => {} },
                ]
            }
        ]
    }
}

![](/files/Screenshot 2026-05-26 at 5.54.11 PM.png)

What You Have Access To

ภายในสคริปต์ของคุณ ตัวช่วย (Helpers) เหล่านี้สามารถเรียกใช้งานได้ทันทีโดยไม่ต้อง Import เพิ่มเติม:

  • this.doc — เอกสารปัจจุบัน สามารถอ่านและเขียนค่าฟิลด์ได้โดยตรง เช่น this.doc.status = "Lost", this.doc.deal_name
  • call(method, params) — เรียกใช้ Python Method ฝั่ง Server โดยจะส่งค่ากลับมาเป็น Promise
  • createDialog(options) — แสดงหน้าต่างยืนยัน (Confirmation Dialog) โดยกำหนด title, message และ actions
  • formDialog(options) — แสดง Dialog พร้อมฟิลด์ฟอร์มเพื่อรับข้อมูลจากผู้ใช้งาน
  • toast.success(msg), toast.error(msg), toast.info(msg) — แสดงการแจ้งเตือน
  • router.push(location), router.replace(location) — นำทางไปยังหน้าอื่น
  • throwError(msg) — แสดงข้อความแจ้งเตือนสีแดงและหยุดการทำงานของโค้ดทันที

Examples

Create Quotation (เฉพาะดีลที่เป็น Won)

Action นี้จะตรวจสอบว่าสถานะดีลเป็น Won หรือไม่ จากนั้นเรียกคำสั่งฝั่ง Server เพื่อสร้างใบเสนอราคา (Quotation) หากสำเร็จจะนำทางไปยังหน้า Quotation ที่สร้างขึ้นใหม่

class CRMDeal {
  onLoad() {
    this.actions = [
      {
        label: "Create Quotation",
        icon: "file-text",
        onClick: (close) => {
          if (this.doc.status !== "Won") {
            toast.info("This action is only available for Won deals");
            return;
          }

          call("crm.api.quotation.create_from_deal", {
            deal: this.doc.name,
            customer: this.doc.organization,
          }).then((data) => {
            if (data?.name) {
              router.push({ name: "Quotation", params: { name: data.name } });
              toast.success("Quotation created successfully");
            } else {
              toast.error("Could not create quotation");
            }
          });
        },
      },
    ];
  }
}

Mark as Lost (พร้อมการยืนยัน)

แสดงกล่องข้อความถามยืนยันจากผู้ใช้ก่อน จากนั้นอัปเดตสถานะของดีลโดยตรง

class CRMDeal {
  onLoad() {
    this.actions = [
      {
        label: "Mark as Lost",
        icon: "x-circle",
        onClick: (close) => {
          createDialog({
            title: "Mark Deal Lost",
            message: "Are you sure you want to mark this deal as Lost?",
            actions: [
              {
                label: "Mark Lost",
                theme: "red",
                variant: "solid",
                onClick: async ({ close: dialogClose }) => {
                  this.doc.status = "Lost";
                  toast.success("Deal marked as Lost");
                  dialogClose();
                },
              },
              {
                label: "Cancel",
                variant: "outline",
                onClick: ({ close: dialogClose }) => dialogClose(),
              },
            ],
          });
        },
      },
    ];
  }
}

Schedule a Follow-up (พร้อมฟอร์ม Dialog)

เปิด Dialog พร้อมฟิลด์วันที่และข้อความโน้ต เมื่อกดยืนยันจะเรียก Server เพื่อบันทึกข้อมูลการติดตามงาน

class CRMDeal {
  onLoad() {
    this.actions = [
      {
        label: "Schedule Follow-up",
        icon: "calendar",
        onClick: (close) => {
          formDialog({
            title: "Schedule Follow-up",
            fields: [
              {
                fieldname: "date",
                fieldtype: "Date",
                label: "Follow-up Date",
                reqd: 1,
              },
              { fieldname: "notes", fieldtype: "Small Text", label: "Notes" },
            ],
            submitLabel: "Schedule",
            cancelLabel: "Cancel",
            onSubmit: async (data) => {
              await call("crm.api.deal.schedule_followup", {
                deal: this.doc.name,
                date: data.date,
                notes: data.notes,
              });
              toast.success("Follow-up scheduled for " + data.date);
            },
          });
        },
      },
    ];
  }
}

Delete Deal (พร้อมการยืนยันและเปลี่ยนเส้นทางหน้า)

แสดงกล่องข้อความยืนยันก่อนลบ จากนั้นเปลี่ยนเส้นทางกลับไปยังหน้ารายการดีลทั้งหมด

class CRMDeal {
  onLoad() {
    this.actions = [
      {
        label: "Delete Deal",
        icon: "trash-2",
        onClick: (close) => {
          createDialog({
            title: "Delete Deal",
            message: `Are you sure you want to delete "${this.doc.deal_name}"? This cannot be undone.`,
            actions: [
              {
                label: "Delete",
                theme: "red",
                variant: "solid",
                onClick: async ({ close: dialogClose }) => {
                  await call("frappe.client.delete", {
                    doctype: "CRM Deal",
                    name: this.doc.name,
                  });
                  dialogClose();
                  router.push("/deals");
                  toast.success("Deal deleted");
                },
              },
              {
                label: "Cancel",
                variant: "outline",
                onClick: ({ close: dialogClose }) => dialogClose(),
              },
            ],
          });
        },
      },
    ];
  }
}

Showing and Hiding Actions Based on Field Values

ในบางกรณีที่คุณต้องการให้บาง Action แสดงเฉพาะในเงื่อนไขหรือสถานการณ์ที่กำหนด ให้ทำการกำหนดค่า this.actions ใหม่อีกครั้งเมื่อฟิลด์ที่เกี่ยวข้องมีการเปลี่ยนแปลง:

class CRMDeal {
  onLoad() {
    this._updateActions();
  }

  // Re-run whenever status changes
  status() {
    this._updateActions();
  }

  _updateActions() {
    const isLost = this.doc.status === "Lost";

    this.actions = [
      {
        label: "Create Quotation",
        onClick: (close) => {
          /* ... */
        },
      },
      // Only show "Mark as Lost" if the deal isn't already lost
      ...(!isLost
        ? [
            {
              label: "Mark as Lost",
              icon: "x-circle",
              onClick: (close) => {
                /* ... */
              },
            },
          ]
        : []),
    ];
  }
}

แปลจากเอกสารของ Frappe: https://docs.frappe.io/crm/custom-actions