Custom Actions ใน Frappe Helpdesk

Custom actions ช่วยให้คุณเรียกใช้งานคำสั่งเฉพาะด้านได้โดยตรงจากหน้า Ticket การดำเนินการเหล่านี้สามารถทำได้ตั้งแต่การเชื่อมต่อกับแอปภายนอก ไปจนถึงการอัปเดตข้อมูลตั๋วงาน หรือการสร้างเอกสารที่เกี่ยวข้องใน ระบบ Nextio ERPNext

Creating Custom Actions

Custom actions จะถูกสร้างและกำหนดค่าโดยใช้ Form Scripts ภายใน Desk:

  1. Navigate to Desk: สลับไปที่ Desk แล้วไปที่เมนู HD Form Script
  2. Select Doctype: เลือก Doctype (เช่น HD Ticket) ที่คุณต้องการเพิ่ม custom action จากนั้นระบบจะมี boilerplate code เริ่มต้นมาให้
  3. Apply To: เลือก "Form"
  4. Define Your Action: วิธีการกำหนดรูปแบบ action ประเภทต่างๆ มีดังนี้: Single Button: โค้ดนี้จะสร้างปุ่มเดี่ยวที่ชื่อว่า "Open in Desk" สำหรับเปิด Ticket ปัจจุบันในแท็บเบราว์เซอร์ใหม่

	```
	const actions = [{
	    "label": "Open in Desk",
	    "onClick": () => {
	        let URL = `https://frappehelpdesk.frappe.cloud/app/hd-ticket/${doc.name}`
	        window.open(URL, '_blank')
	    },
	    "variant": 'solid' | 'subtle' | 'outline' | 'ghost',
	    "theme" : 'gray' | 'blue' | 'green' | 'red'
	}]
	```
	You can add variant and theme to your buttons.
	Default variant is "solid".
	Default color is "gray".
	Both of these properties are optional.
	You can also add your own tailwind classes here using the "class" key
	`e.g: class: 'rounded-lg bg-surface-pink-1' `

กลุ่มของปุ่มจะแสดงรายการปุ่มย่อยภายใต้คีย์ options (สำหรับสคริปต์รุ่นเก่าที่ใช้คีย์ items ก็ยังคงรองรับการทำงานได้ตามปกติ)

Grouped Actions: โค้ดนี้จะสร้างกลุ่มของปุ่มที่ซ่อนอยู่ภายใต้เมนูสามจุดที่ชื่อว่า "Add" เมื่อคลิกที่ "Open Weekly Analysis" หรือ "Open Article Analysis" จะเรียกใช้ฟังก์ชัน onClick ตามที่กำหนดไว้

	```
    const actions = [
      {
        group: 'Reports',
        hideLabel: true,
        options: [
          {
            label: 'Open Weekly Analysis',
            onClick: () => {},
          },
          {
            label: 'Open Articles Analytics',
            onClick: () => {},
          },
        ],
      },
      {
        group: 'Ticket',
        options: [
          {
            label: 'Create a new ticket',
            onClick: () => {},
          },
          // suggest some action other than open in fc
          {
            label: 'Open Customer Details',
            onClick: () => {},
          },
        ],
      },
    ]
	```

Grouped Actions with label: โค้ดนี้จะสร้างกลุ่มของปุ่มที่ซ่อนอยู่ภายใต้ปุ่ม dropdown ที่มีป้ายกำกับว่า "Reports" เมื่อคลิกเลือก "Open Weekly Analysis" หรือ "Open Article Analysis" ก็จะทำงานตามฟังก์ชัน onClick ที่ระบุไว้

	```
    const actions = [{
          "buttonLabel": "Reports",
          "group": "Add",
          "hideLabel": true,
          "options": [
              {
                label: 'Open Weekly Analysis',
                onClick: () => {},
              },
              {
                label: 'Open Articles Analytics',
                onClick: () => {},
              },
          ]
      }]
	```

Combined Actions: คุณสามารถผสมผสานทั้งปุ่มเดี่ยวและกลุ่มของปุ่มเข้าด้วยกันในลิสต์เดียว แล้วส่งค่าผ่านพร็อพเพอร์ตี้ "actions"

Using Context and Actions

พารามิเตอร์ doc จะให้บริบท (context) ของหน้า Ticket ปัจจุบัน คุณสามารถนำบริบทนี้ไปใช้งานร่วมกับฟังก์ชันต่างๆ เพื่อสร้าง custom actions ตามต้องการ:

  • doc: เข้าถึงค่าของฟิลด์ต่างๆ ใน Ticket (เช่น doc.raised_by, doc.customer)
  • $dialog: เปิดหน้าต่างกล่องข้อความยืนยันพร้อมปุ่ม action (คุณสามารถส่งคีย์ title, message, html และ actions ขณะสร้าง dialog) พร็อพเพอร์ตี้ onClick ของแต่ละ dialog action จะได้รับ { close } ดังนั้นให้เรียกใช้ close() เพื่อปิด dialog ทั้งนี้รูปแบบการเขียนเดิมแบบ onClick(close) ยังคงใช้งานได้
  • updateField: อัปเดตค่าของฟิลด์ที่กำหนด (เช่น updateField('status', 'closed', callback))
  • router: นำทางไปยังหน้าอื่นๆ โดยใช้ Vue Router
  • call: เรียกใช้งาน API จากฝั่ง client-side โดยคุณสามารถเพิ่ม server-side endpoint ได้ใน Server Script
  • toast: แสดงการแจ้งเตือนแบบ toast ด้วยคำสั่ง toast.success(), toast.error() หรือ toast.info()
  • createToast: แสดงข้อความแจ้งเตือนความสำเร็จหรือข้อผิดพลาด ซึ่งเป็น wrapper รุ่นเก่าของ toast แต่ยังคงรองรับการใช้งานอยู่

การกำหนดค่าฟิลด์ใน HD Form Script Doctype:

  • Enabled: เปิดใช้งานสคริปต์ หากไม่ได้เลือกตัวเลือกอื่นเพิ่มเติม สคริปต์จะมีผลกับหน้า Agent's Ticket View
  • Apply to customer portal: สคริปต์จะมีผลกับหน้า Ticket View ของลูกค้าบน Customer Portal
  • Apply on new page: สคริปต์จะมีผลในหน้าสร้าง New Ticket
  • Apply to knowledge base: สคริปต์จะทำงานบนหน้า Knowledge Base ของ Customer Portal สำหรับผู้อ่านทุกคน สามารถใช้เพิ่มปุ่มและลิงก์บนแถบ Header ได้ ดูรายละเอียดเพิ่มเติมที่ Knowledge Base Settings

ตารางฟังก์ชันที่รองรับในหน้าต่างๆ:

Functions Ticket Agent Ticket Customer New Ticket
call ✓ ✓ ✓
updateField ✓ ✓ ✗
router ✓ ✓ ✓
$dialog ✓ ✓ ✓
toast ✓ ✓ ✗
createToast ✓ ✓ ✗
applyFilters ✗ ✗ ✓

Example: Custom Actions for Ticket Management

ตัวอย่างต่อไปนี้แสดงการใช้งาน custom actions หลากหลายรูปแบบที่คุณสามารถสร้างเพื่อจัดการงานต่างๆ:

function setupForm({
  doc,
  updateField,
  call,
  router,
  $dialog,
  createToast,
}) {
  const actions = [
    {
      group: "Actions",
      hideLabel: true,
      options: [
        {
          label: "Open FC Info",
          onClick: () => {
            // Call API to get custom information
            call("show_custom_info", {
              ticket: doc.name,
              // Include relevant fields for fetching data
            }).then((data) => {
              if (data.name) {
                $dialog({
                  title: data?.title,
                  message: data?.message,
                  html: data?.html, // this field can be in HTML format
                  actions: [
                    {
                      label: "Open in FC",
                      variant: "solid",
                      onClick() {
                        let URL = `http://helpdesk:8010/app/hd-ticket/${doc?.name}`;
                        window.open(URL, "_blank");
                      },
                    },
                    //   add more actions
                  ],
                });
              }
            });
          },
        },
        {
          label: "Close this ticket",
          onClick: () => {
            $dialog({
              title: "Close this ticket?",
              message: "Are you sure you want to mark this close this ticket?",
              actions: [
                {
                  label: "Close",
                  theme: "red",
                  variant: "solid",
                  onClick({ close }) {
                    updateField("status", "Closed", () => {
                      createToast({
                        title: "Ticket has been closed",
                        icon: "alert-circle",
                      });
                      close();
                    });
                  },
                },
                {
                  label: "Cancel",
                  variant: "outline",
                  onClick({ close }) {
                    close();
                  },
                },
              ],
            });
          },
        },
      ],
    },
  ];
  if (doc.status !== "Closed") {
    actions.push({
      label: "Close the ticket",
      onClick: () => {
        $dialog({
          title: `Close ticket: ${doc.name}`,
          message: "Are you sure you want to close this ticket?",
          actions: [
            {
              label: "Close",
              theme: "red",
              variant: "solid",
              onClick({ close }) {
                updateField("status", "Closed", () => {});
                close();
              },
            },
            {
              label: "Cancel",
              variant: "outline",
              onClick({ close }) {
                close();
              },
            },
          ],
        });
      },
    });
  }

  return {
    actions,
  };
}

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