EP 10 : เซ็นและอนุมัติเอกสารออนไลน์ระบบ Microsoft Dynamics 365 for Finance and Operations ด้วย Power Apps

--

Ref. https://www.thompsoncoburn.com

ตัวอย่างในบล็อคนี้ จะเป็นการนำเสนอวิธีการสร้างแอพพลิเคชั่นเซ็นเอกสารออนไลน์ และนำลายเซ็นเข้าสู่ระบบ (Dynamics 365 for Finance and Operations) พร้อมแสดงบนเอกสาร อีกทั้งยังเป็นการ Approve workflow ไปในตัวอีกด้วย โดยใช้ Microsoft Power Apps

Power Apps

เริ่มต้นจากการสร้างตัวแอพพลิเคชั่น(ซึ่งรันบน mobile ได้) โดยเราจะสร้างแอพเพื่อเรียกดูข้อมูล Purchase order ของระบบที่ต้องทำการ Approve จากนั้นจะทำการ Sign and Approve แล้วส่งข้อมูลเข้าสู่ระบบ

ขั้นตอนคือ เข้าไปที่ https://make.powerapps.com/ และ Sign in
จากนั้นไปที่ Create -> Start from data -> New
เลือก Connection Dynamics 365, เลือก Table (หรือ Data Entity ของระบบ) เป็น “WorkflowWorkItems”

ระบบจะสร้าง template app จากข้อมูลให้ (Browse and Detail Screen)
สามารถปรับการแสดงผลข้อมูลเป็นฟิลที่เราต้องการได้ ที่ Tab Advanced -> Text

ทำการ filter ข้อมูล กดที่ BrowseGallery -> Items ใช้คำสั่ง

SortByColumns(Search(Filter(WorkflowWorkItems, MenuItemName = "PurchTable" && Status = "Pending"), TextSearchBox1.Text, "Subject", "Subject"), "Subject", If(SortDescending1, Descending, Ascending))

MenuItemName = “PurchTable” สำหรับกรองข้อมูลประเภท PO workflow
Status = “Pending” สำหรับกรองข้อมูลสถานะรอการ Approve
TextSearchBox1.Text, “Subject” สำหรับเมื่อพิมพ์ช่อง search items จะค้นหาไปที่ฟิล Subject

Detail screen จะเป็นหน้าจอที่แสดงขึ้นมาเมื่อเราเลือกรายการจากหน้า Browse
คลิ๊กที่ DetailForm1 -> Properties -> Edit fields
เลือกเพิ่มฟิล Workitem ID, Subject, User เพื่อแสดงผลและสำหรับส่งข้อมูล

ทำการเพิ่มพื้นที่สำหรับลายเซ็น คลิ๊กที่ Insert -> Text -> Pen input
ทำการเพิ่มปุ่มสำหรับ Approve คลิ๊กที่ Insert -> Button

ในส่วนของปุ่ม Approve จะใช้ตัว Power Automate มาช่วยในการ integration เข้าสู่ระบบ ERP
คลิ๊กที่ ปุ่ม Approve -> Action -> Power Automate -> Create a new flow
เลือก Flow ที่เป็น Power Apps Button

สร้าง Flow หลังจากกดปุ่ม Approve
ส่วนที่ 1 เป็นส่วนของการ Approve Purchase Order Workflow
Add action -> Dynamics Fin & Ops -> Execute action
Instance : URL ของ Microsoft Dynamic
Action : WorkflowWorkItems-complete
Outcome : Approve (fix ค่า)
Comment : เป็น text note อะไรก็ได้

ฟิลที่ต้องเลือกแบบ Dynamics content -> Ask in PowerApps
(ความหมายคือต้องการรับค่ามาจากฝั่ง PowerApps โดยการกดปุ่ม)
WorkflowWorkItemInstanceId, TargetUser, RunAsUser

ส่วนที่ 2 เป็นส่วนของการนำรูปลายเซ็น บันทึกเป็น Attach file ของ Purchase Order
Add action -> Dynamics Fin & Ops -> Create record
Instance : URL ของ Microsoft Dynamic
Entity name : PurchaseOrderHeaderDocumentAttachments
Type : Image
Company : company ในระบบ
File Type : jpg
Description : คำอธิบายของไฟล์ (อะไรก็ได้)
FileName : ชื่อไฟล์ (อะไรก็ได้)

ฟิลที่ต้องเลือกแบบ Dynamics content -> Ask in PowerApps
Purchase order : เลขที่เอกสาร
Archive file : ไฟล์ลายเซ็น ซึ่งถูกจัดเก็บอยู่ในรูปแบบของ binary

เพิ่มคำสั่งที่ Button สำหรับแปลง Pen input เป็น Image

Set(PenVar,JSON(PenInput1.Image,JSONFormat.IncludeBinaryData));
Set(PenInput64Bit,Mid(PenVar,24,Len(PenVar) - 24));

คำสั่งสำหรับส่งค่าตัวแปรไปยัง flow (datacard คือ field ที่เลือกก่อนหน้า)
** ในที่นี้ตัวอย่างข้อมูลไม่สามารถหาเลขที่ PO ได้ ผู้เขียนจึงใช้วิธีการตัด string จาก subject โดยใช้คำสั่ง Right

ApprovePOwithSignature.Run(DataCardValue2.Text, DataCardValue8.Text, PenInput64Bit, Right(DataCardValue9.Text, 10));

คำสั่งสำหรับกลับไปหน้า BrowseScreen หลังจาก Approve

Navigate(BrowseScreen1);

เริ่มทดสอบการทำงาน
สามารถ run ได้ทั้งบน mobile หรือ run ทดสอบได้เลยบน Studio นี้ โดยการกดปุ่ม Play (สามเหลี่ยม) ที่มุมขวาด้านบน

เลือก Purchase order, เซ็น และกดปุ่ม Approve
ตรวจสอบไฟล์แนบของ Purchase order ในระบบ จะได้รูปลายเซ็นที่ถูกบันทึกไว้
ตรวจสอบ Workflow history ของ Purhase order

Approve PO with Signature

เพิ่มรูปลายเซ็นในเอกสาร
ตัวอย่างการเขียน code ที่ Class “PurchPurchaseOrderDP” สำหรับ assign ค่าไปยัง Report Design

DocuRef docuRef;
DocuValue docuValue;
PurchTable purchTable; // to do assign
select docuRef
where docuRef.RefTableId == purchTable.tableId
&& docuRef.RefRecId == purchTable.RecId
&& docuRef.ActualCompanyId == curext()
&& docuRef.TypeId == "Image"
join docuValue
where docuValue.RecId == docuRef.ValueRecId
&& docuValue.FileName = "Signature"
if(docuValue.FileId)
purchPurchaseOrderHeader.ApprovedSignature = DocumentManagement::getAttachmentAsContainer(docuRef);

จบ…

--

--

Teerapong Mukdapipatkul (Bee) 😄

Microsoft Dynamics 365 Developer at QUICK ERP and Co-Founder at Analytic Business Solutions linkedin.com/in/teerapong-muk