buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public without sharing class ServiceSummaryHandler {
    public static String recordtypeId = '';
    public static void CreateCase(List<ServiceSummary__c> newList, Map<Id, ServiceSummary__c> newMap, List<ServiceSummary__c> oldList, Map<Id, ServiceSummary__c> oldMap) {
        recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Case' and DeveloperName = 'Draft'].id;
        List<Case> upsertCaseList = new List<Case>();
        for (ServiceSummary__c newServiceSummary : newList) {
            ServiceSummary__c oldServiceSummary = oldMap == null ? null : oldMap.get(newServiceSummary.Id);
            if (oldServiceSummary != null) {
                if (newServiceSummary.IndividualCase__c != oldServiceSummary.IndividualCase__c && newServiceSummary.IndividualCase__c) {
                    upsertCaseList.add(CaseDate(newServiceSummary));
                }
            } else  if (newServiceSummary.IndividualCase__c) {
                upsertCaseList.add(CaseDate(newServiceSummary));
            }
            
        }
 
        if (upsertCaseList.size() > 0 ) {
            upsert upsertCaseList;
        }
 
    }
 
 
    public static Case CaseDate(ServiceSummary__c serviceSummary) {
 
        Case newCase = new Case();
        newCase.Subject = serviceSummary.ServiceSubject__c; //主题 
        newCase.Description = serviceSummary.ServiceDescription__c; //描述
        newCase.AccountId = serviceSummary.ServiceAccount__c; //服务客户
        newCase.Reason = serviceSummary.ServiceReason__c; //个案原因
        newCase.Origin = serviceSummary.ServiceOrigin__c; //个案来源
        newCase.Priority = serviceSummary.ServicePriority__c; //优先级
        newCase.CreatedDate = Datetime.now(); //开始日期/时间
        newCase.CaseServiceSummary__c = serviceSummary.Id;
        newCase.Status = 'New';
        newCase.ApprovalStatus__c = '草案中';
        newCase.recordtypeId = recordtypeId;
 
        return newCase;
    }
 
 
}