Apex 트리거를 사용하여 사례 중대 사건 자동 완성
고유 기준에 일치하는 사례에 대한 중대 사건을 완료 상태로 자동으로 표시하는 Apex 트리거를 만들 수 있습니다. 트리거 시 중대 사건이 완료 상태로 표시되려면 만족해야 하는 이벤트 및 관련 사례 기준을 정의합니다. 작업 주문 중대 사건을 자동으로 완료하도록 유사 트리거를 구현할 수 있습니다.
필수 Edition
| 지원되는 Edition을 확인하세요. |
| 필요한 사용자 권한 | |
|---|---|
| Apex 트리거 및 클래스 정의: | 작성자 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); } } } }
이 기사를 통해 문제를 해결했습니까?
개선을 위한 의견을 보내주세요.

