Salesforce触发器-由子触发器触发的帐户字段更新为有效,但记录未更新

时间:2018-07-26 10:22:13

标签: salesforce apex-code apex

我在帐户上有一个名为First Order CC的字段,该字段需要从子订单上的第一条记录更新。

我会解释逻辑,然后编写代码。

帐户=主帐户 顺序=儿童

如果Order.order date!= null && Order.Order ID!= null Account.First购买日期!= null null,则进行另一个循环并验证

Account。首次购买日期(此值从外部数据库中提取)!= null && Account。首次购买日期== Order.order日期&& Account。首次订购ID = NULL

然后更新 Account.First订单ID = Order.Order ID Account.First_Order_CC__c = order.Order_CC__c

    public with sharing class OrderClass {


    public OrderClass() {

    }

    // Set First Order CC and Second Purchase Date on Distributor for Dashboard & Reports
    public void processOrderData (list<Order__c> orderList, String triggerAction) {

        try {

            if(triggerAction=='afterInsert' || triggerAction=='afterUpdate') {
                Map<Id,Account> DistMap = new Map<Id,Account>();
                Map<Id,Account> DistUpdMap = new Map<Id,Account>();
                List<Account> acctUpdList = new List<Account>();
                Set<Id> acctIds = new Set<Id>();

                for(Order__c ord : orderList) {
                    acctIds.add(ord.Distributor__c);
                }

                // Get Distributors to Update
                DistMap = new map<Id, Account>([select Id, Name, First_Order_CC__c, First_Order_ID__c, First_Purchase_Date__c,
                    Second_Order_ID__c, Second_Purchase_Date__c from Account where Id IN :acctIds]);
                System.debug('>>>>> DistMap: '+ DistMap);       
                if(DistMap != null) {
                    for(Order__c ord : orderList) {
                        Date ordate = ord.Order_Date__c;
                        if(DistMap.containsKey(ord.Distributor__c)) {
                            Account tmpAccount = DistMap.get(ord.Distributor__c);
                            boolean hasUpdate = false;

                            System.debug('>>>>> ORDER DETAILS : '+ ord);
                            if(ordate != null && ordate.year() != 2018  && ord.Order_ID__c != null && tmpAccount.First_Purchase_Date__c != null) {
                                System.debug('>>>>> INSIDE 1 <<<<<');
                                if(tmpAccount.First_Purchase_Date__c != null && tmpAccount.First_Purchase_Date__c == ord.Order_Date__c && tmpAccount.First_Order_ID__c == null) {
                                    System.debug('>>>>> INSIDE 2 <<<<<');
                                    tmpAccount.First_Order_ID__c = ord.Order_ID__c;
                                    tmpAccount.First_Order_CC__c = ord.Order_CC__c;
                                    hasUpdate = true;
                                }

                                if(tmpAccount.Second_Purchase_Date__c == null || tmpAccount.Second_Purchase_Date__c >= ordate) {
                                    System.debug('>>>>> INSIDE 3 <<<<<');
                                    if(tmpAccount.First_Order_ID__c == null || tmpAccount.First_Order_ID__c != ord.Order_ID__c) {
                                        System.debug('>>>>> INSIDE 4 <<<<<');
                                        tmpAccount.Second_Order_ID__c = ord.Order_ID__c;
                                        tmpAccount.Second_Purchase_Date__c = ordate;
                                        hasUpdate = true;
                                    }
                                }
                            }

                            if(hasUpdate) {
                                System.debug('>>>>> INSIDE 5 <<<<<');
                                DistUpdMap.put(tmpAccount.Id, tmpAccount);
                                DistMap.put(tmpAccount.Id, tmpAccount);
                            }
                        }                   
                    } // order loop 

                    // DML Statements
                    acctUpdList.addAll(DistUpdMap.values());
                    system.debug('>>>>> acctUpdList: '+acctUpdList);
                    if(!acctUpdList.isEmpty()) {
                        update acctUpdList; 
                    }
                } // is Dist Map
            } // After Insert or After Update
        } 
        catch(Exception ex) {
            system.debug('>>>>> Error updating Account in OrderClass:  Message: '+ ex.getMessage());
        }
    }
}

尽管我有90%的代码覆盖率,但是记录没有得到更新。任何帮助都可以申请

0 个答案:

没有答案