การเขียน Custom Script เพื่อสร้าง Lead อัตโนมัติใน Frappe CRM

ใน Frappe CRM คุณสามารถตั้งค่าการสร้าง Lead อัตโนมัติได้โดยใช้ฟีเจอร์ Server Script ซึ่ง DocType Server Script จะช่วยให้คุณเขียนโค้ด Python ฝั่งเซิร์ฟเวอร์ได้โดยตรง โดยไม่จำเป็นต้องสร้างแอปพลิเคชันแบบ Custom หรือ Deploy ระบบใหม่ หากคุณกำลังใช้งานหรือปรับแต่ง ระบบ Nextio ERPNext การใช้ Server Script จะช่วยเพิ่มประสิทธิภาพการทำงานได้อย่างมาก

Server Script มีทั้งหมด 4 ประเภท แต่สำหรับคู่มือนี้เราจะมุ่งเน้นที่:

  1. DocType Event
  2. API
  3. Scheduler Event

1. Create Lead When an Email is Received (DocType Event)

ตัวอย่างเช่น หากคุณต้องการสร้าง Lead ใหม่ทุกครั้งที่มีอีเมลขาเข้าส่งเข้ามาในระบบ

Step 1: Go to Server Script

ไปที่:
Desk → Search Server Script → Add Server Script

Step 2: Fill in the following:

Field Value
Script Type DocType Event
Doctype Communication
Event After Insert
Script (ดูโค้ดด้านล่าง)

Sample Script:

if doc.communication_type == 'Communication' and doc.sent_or_received == 'Received':
    existing_lead = frappe.db.exists("CRM Lead", {"email": doc.sender})
    if not existing_lead:
        frappe.get_doc({
            "doctype": "CRM Lead",
            "first_name": doc.sender_full_name,
            "email": doc.sender,
            "source": "Email",
            "status": "New",
        }).insert(ignore_permissions=True)

สคริปต์นี้จะสร้าง Lead ใหม่อัตโนมัติสำหรับอีเมลขาเข้าแต่ละฉบับ พร้อมป้องกันการสร้างข้อมูลที่ซ้ำกัน

2. Create Lead via Custom API (API Script)

ในบางกรณี คุณอาจต้องการให้ระบบภายนอกสามารถสร้าง Lead ได้ผ่านการเรียกใช้งาน API Endpoint

Step 1: Go to Server Script

ไปที่:
Desk → Search Server Script → Add Server Script

Step 2: Fill in the following:

Field Value
Script Type API
API Method create_lead_api
Script (ดูโค้ดด้านล่าง)

Sample Script:

data = frappe.local.form_dict

if not data.get("email") or not data.get("name"):
    frappe.throw("Missing required fields: 'email' and 'name'")

existing_lead = frappe.db.exists("CRM Lead", {"email": data["email"]})
if existing_lead:
    frappe.response["message"] = f"Lead already exists: {existing_lead}"
else:
    lead = frappe.get_doc({
        "doctype": "CRM Lead",
        "first_name": data["name"],
        "email": data["email"],
        "status": "New",
        "source": "API"
    })
    lead.insert(ignore_permissions=True)
    frappe.response["message"] = f"Lead created: {lead.name}"

How to Use:

ส่งคำขอ POST request ไปที่:

https://<your-site>/api/method/create_lead_api

พร้อมข้อมูล Body:

{
  "email": "someone@example.com",
  "name": "Someone"
}

3. Create Lead via Scheduled Job (Scheduler Event)

คุณอาจต้องการสร้าง Lead ตามรอบเวลาที่กำหนดโดยใช้ตรรกะบางอย่าง เช่น ดึงข้อมูล Lead จากตารางอื่น หรือจากผู้ใช้ที่ไม่มีการใช้งาน (Inactive Users)

Step 1: Go to Server Script

ไปที่:
Desk → Search Server Script → Add Server Script

Step 2: Fill in the following:

Field Value
Script Type Scheduler Event
Cron Expression 0 * * * * (Every hour)
Script (ดูโค้ดด้านล่าง)

Sample Script:

# Example: Create a test lead every hour for demo
frappe.get_doc({
    "doctype": "CRM Lead",
    "first_name": "Auto Generated Lead",
    "email": f"demo_{frappe.utils.now_datetime().strftime('%H%M%S')}@example.com",
    "source": "Scheduled Script",
    "status": "New"
}).insert(ignore_permissions=True)

คุณสามารถปรับเปลี่ยนตรรกะนี้ด้วย Query, Filter หรือกฎเกณฑ์ที่กำหนดเองอื่นๆ ในการสร้าง Lead ได้ตามต้องการ

Permission Query (Skipped)

ประเภท Permission Query นั้นใช้สำหรับการควบคุมสิทธิ์การมองเห็นระเบียนข้อมูลแบบไดนามิก ซึ่งไม่ได้ใช้สำหรับการสร้าง Lead จึงขอข้ามไปในคู่มือนี้

Tips

  • ควรทดสอบสคริปต์ของคุณบน Test Site เสมอก่อนนำไปใช้งานจริงบน Production
  • ใช้งาน ignore_permissions=True อย่างระมัดระวัง
  • ใช้ frappe.throw() เพื่อตรวจสอบความถูกต้องของข้อมูลนำเข้า (Input Validation) ใน API Scripts

Learn More

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