自動完成個案里程碑
建立 Apex 觸發以在符合唯一條件的個案將里程碑自動標記為「已完成」。在您的觸發中,定義必須滿足哪些事件與相關個案條件,里程碑才會標示為「已完成」。您可以實作類似的觸發,以自動完成工作指示里程碑。
必要版本
| 檢視支援的版本。 |
| 需要的使用者權限 | |
|---|---|
| 定義 Apex 觸發與類別: | Author Apex |
小秘訣 在 Lightning Experience 中,支援代表可以按一下「里程碑」元件中的「標記為已完成」連結,將里程碑標記為已完成。這並不會自動完成,但是很簡單。如需詳細資訊,請參閱設定里程碑追蹤器。
下列觸發會在其所在的個案符合唯一條件時,將特定類型的里程碑標示為「已完成」。我們也提供里程碑公用程式 Apex 類別及伴隨的單元測試。先定義里程碑公用程式類別,再使用任何觸發。
- 里程碑公用程式 Apex 類別
- Apex 類別會減少觸發的大小,並更容易重複使用與維護 Apex 程式碼。定義您組織中的此 Apex 類別:
- 進入「設定」,在「快速尋找」方塊中輸入「Apex 類別」,然後按一下「Apex 類別」。
- 按一下「新增」。
- 複製類別文字並貼到文字方塊中。
- 按一下「儲存」。
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } } } - Apex 類別單元測試
- 您可以在開發人員主控台中設定 Apex 單元測試,以掃描程式碼是否有任何問題。
/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTest private class MilestoneTest { static testMethod void TestCompleteMilestoneCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Id contactId = cont.Id; Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; if (entlId != null){ Case c = new Case(Subject = 'Test Case with Entitlement ', EntitlementId = entlId, ContactId = contactId); cases.add(c); } if (cases.isEmpty()==false){ insert cases; List<Id> caseIds = new List<Id>(); for (Case cL : cases){ caseIds.add(cL.Id); } milestoneUtils.completeMilestone(caseIds, 'First Response', System.now()); } } static testMethod void testCompleteMilestoneViaCase(){ List<Account> acts = new List<Account>(); Account myAcc = new Account(Name='TestAct', phone='1001231234'); acts.add(myAcc); Account busAcc = new Account(Name = 'TestForMS', phone='4567890999'); acts.add(busAcc); insert acts; Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id); insert(cont); Id contactId = cont.Id; Entitlement entl = new Entitlement(Name='TestEntitlement', AccountId=busAcc.Id); insert entl; String entlId; if (entl != null) entlId = entl.Id; List<Case> cases = new List<Case>{}; for(Integer i = 0; i < 1; i++){ Case c = new Case(Subject = 'Test Case ' + i); cases.add(c); if (entlId != null){ c = new Case(Subject = 'Test Case with Entitlement ' + i, EntitlementId = entlId); cases.add(c); } } } } - 範例觸發 1
- 您可以建立名為「解決時間」的里程碑,此里程碑需要個案在特定的時間長度內結束。這是強制符合 SLA 中個案解決方案條款的好方法。此範例個案觸發會在個案結束時,將每個「解決時間」里程碑標示為「已完成」。如此一來,支援代表便無須在結束個案後手動將里程碑標記為完成。
定義您組織中的此觸發:
- 進入「設定」,在「快速尋找」方塊中輸入「個案觸發」,然後按一下「個案觸發」。
- 按一下「新增」。
- 複製觸發文字並貼到文字欄位中。
- 按一下「儲存」。
trigger CompleteResolutionTimeMilestone on Case (after update) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); List<Id> updateCases = new List<Id>(); for (Case c : Trigger.new){ if (((c.isClosed == true)||(c.Status == 'Closed'))&&((c.SlaStartDate <= completionDate)&&(c.SlaExitDate == null))) updateCases.add(c.Id); } if (updateCases.isEmpty() == false) milestoneUtils.completeMilestone(updateCases, 'Resolution Time', completionDate); } } - 範例觸發 2
- 您可以建立名為「第一個回應」的里程碑,其需要支援代表在個案建立後的 X 分鐘或小時內與個案連絡人連絡。這是一個確定支援小組會儘快與個案連絡人通訊的好方法。此範例電子郵件觸發會在電子郵件傳送給個案連絡人時,將「第一個回應」里程碑標示為「已完成」。如此一來,支援代表便無須在以電子郵件傳送個案連絡人之後,手動將「第一個回應」里程碑標記為「已完成」。
備註 此觸發會參照里程碑公用程式測試類別,因此請務必先定義測試類別。定義您組織中的此觸發:
- 進入「設定」,在「快速尋找」方塊中輸入「電子郵件觸發」,然後按一下「電子郵件觸發」。
- 按一下「新增」。
- 複製觸發文字並貼到文字欄位中。
- 按一下「儲存」。
trigger CompleteFirstResponseEmail on EmailMessage (after insert) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); Map<Id, String> emIds = new Map<Id, String>(); for (EmailMessage em : Trigger.new){ if(em.Incoming == false) emIds.put(em.ParentId, em.ToAddress); } if (emIds.isEmpty() == false){ Set <Id> emCaseIds = new Set<Id>(); emCaseIds = emIds.keySet(); List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email, c.OwnerId, c.Status, c.EntitlementId, c.SlaStartDate, c.SlaExitDate From Case c where c.Id IN :emCaseIds]; if (caseList.isEmpty()==false){ List<Id> updateCases = new List<Id>(); for (Case caseObj:caseList) { if ((emIds.get(caseObj.Id)==caseObj.Contact.Email)&& (caseObj.Status == 'In Progress')&& (caseObj.EntitlementId != null)&& (caseObj.SlaStartDate <= completionDate)&& (caseObj.SlaStartDate != null)&& (caseObj.SlaExitDate == null)) updateCases.add(caseObj.Id); } if(updateCases.isEmpty() == false) milestoneUtils.completeMilestone(updateCases, 'First Response', completionDate); } } } } - 範例觸發 3
- 當上一個觸發處理電子郵件訊息時,此範例個案註解觸發會在個案上有了公用註解時,將「第一個回應」里程碑標示為「已完成」。如果公用個案註解是您支援字詞中第一個有效的回應,您便可以使用此註解。
備註 此觸發會參照里程碑公用程式測試類別,因此請務必先定義測試類別。定義您組織中的此觸發:
- 進入「設定」,在「快速尋找」方塊中輸入「個案註解觸發」,然後按一下「個案註解觸發」。
- 按一下「新增」。
- 複製觸發文字並貼到文字欄位中。
- 按一下「儲存」。
trigger CompleteFirstResponseCaseComment on CaseComment (after insert) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); List<Id> caseIds = new List<Id>(); for (CaseComment cc : Trigger.new){ if(cc.IsPublished == true) caseIds.add(cc.ParentId); } if (caseIds.isEmpty() == false){ List<Case> caseList = [Select c.Id, c.ContactId, c.Contact.Email, c.OwnerId, c.Status, c.EntitlementId, c.SlaStartDate, c.SlaExitDate From Case c Where c.Id IN :caseIds]; if (caseList.isEmpty() == false){ List<Id> updateCases = new List<Id>(); for (Case caseObj:caseList) { if ((caseObj.Status == 'In Progress')&& (caseObj.EntitlementId != null)&& (caseObj.SlaStartDate <= completionDate)&& (caseObj.SlaStartDate != null)&& (caseObj.SlaExitDate == null)) updateCases.add(caseObj.Id); } if(updateCases.isEmpty() == false) milestoneUtils.completeMilestone(updateCases, 'First Response', completionDate); } } } }
此文章是否解決您的問題?
請讓我們知道,以便我們改進!

