buli
2023-07-14 36d15f189de2e83ce2576715dac30c3c260388dd
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
trigger SummaryAccruedAmountTrigger on Maintenance_Contract__c(after insert) {
   if (Trigger.isInsert) {
        List<String> ids = new List<String>();
        List<String> assIds = new List<String>();
        List<String> proIds = new List<String>();
        Decimal sum = 0;
        List<Maintenance_Contract__c> mcs = Trigger.new;
        for (Maintenance_Contract__c mc : mcs) {
            ids.add(mc.Id);
        }
        if (ids.size()>0) {
            List<Maintenance_Contract_Asset__c> mcas = [select Asset__c from Maintenance_Contract_Asset__c where Maintenance_Contract__c = :ids[0]];
            System.debug(LoggingLevel.INFO, '*** mcas: ' + mcas);
            for (Maintenance_Contract_Asset__c mca : mcas) {
                assIds.add(mca.Asset__c);
            }
        }
        if (assIds.size()>0) {
            List<Asset> assets = [select Product2Id from Asset where Id in :assIds];
            System.debug(LoggingLevel.INFO, '*** assets: ' + assets);
            for (Asset a : assets) {
                proIds.add(a.Product2Id);
            }
        }
        if (proIds.size()>0) {
            List<Product2> pros = [select Intra_Trade_Gurantee_RMB__c from Product2 where id in :proIds];
            for (Product2 p : pros) {
                sum = sum+p.Intra_Trade_Gurantee_RMB__c;
                System.debug(LoggingLevel.INFO, '*** sum: ' + sum);
            } 
        }
 
        List<Maintenance_Contract__c> updateContract = new List<Maintenance_Contract__c>();
        for (Maintenance_Contract__c mc : Trigger.new) {
            mc.FM__c = Math.ceil(sum*1.13/3); 
            updateContract.add(mc);
        }
        update updateContract;
    }
}