高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class MaintenanceEstimateCopy {
    WebService static String copy(String id) {
        Maintenance_Contract_Estimate__c oldMce = [select Discount_Percentage__c, Discount_Price__c, Maintenance_Contract__c
                                                   from Maintenance_Contract_Estimate__c where Id = :id];
        List<Maintenance_Contract_Asset_Estimate__c> oldMcae = [select Asset__c, Asset_Estimate_RMB__c from Maintenance_Contract_Asset_Estimate__c
                                                                where Maintenance_Contract_Estimate__c = :id];
        //try {
            Maintenance_Contract_Estimate__c newMce = new Maintenance_Contract_Estimate__c(
                Name = '*',
                Discount_Percentage__c = oldMce.Discount_Percentage__c,
                Discount_Price__c = oldMce.Discount_Price__c,
                Maintenance_Contract__c = oldMce.Maintenance_Contract__c
            );
            insert newMce;
            
            List<Maintenance_Contract_Asset_Estimate__c> newMcae = new List<Maintenance_Contract_Asset_Estimate__c>();
            for (Maintenance_Contract_Asset_Estimate__c mcae : oldMcae) {
                newMcae.add(new Maintenance_Contract_Asset_Estimate__c(
                    Asset__c = mcae.Asset__c,
                    Asset_Estimate_RMB__c = mcae.Asset_Estimate_RMB__c,
                    Maintenance_Contract_Estimate__c = newMce.Id
                    // コピーで作成した時を「継続」にする
//                    New_Or_Old__c = '继承'
                ));
            }
            if (newMcae.size() > 0) insert newMcae;
            
            return newMce.Id;
        //} catch (Exception e) {
        //    return 'NG';
        //}
    }
}