0

我想在联系人上建立一个触发器,即接受复选框。在 Account 对象中,它需要 2 个处于活动状态的联系人。当用户将第三个活动联系人分配给同一帐户时,他/她将收到错误消息。怎么可能?我完成了所有代码,但这些 2 个联系人的限制待定。谁能尝试指导我如何做到这一点?

trigger ContactTrigger on Contact(after insert, after update, after delete, after undelete) {
    switch on Trigger.operationType {
        when AFTER_INSERT {
            Set<Id> accountIds = new Set<Id>();
            for (Contact con : Trigger.new) {
                
                if (String.isNotBlank(con.AccountId)) {
                    //write automation logic here
                    accountIds.add(con.AccountId);
                }
            }
            // get aggregate result for all accounts
            List<AggregateResult> results = [
                SELECT AccountId, COUNT(Id) totalContacts
                FROM Contact
                WHERE Active__c = TRUE AND AccountId IN :accountIds
                GROUP BY AccountId
            ];
            integer var = results.size();
            // build final list of accounts to update
            List<Account> accountsToUpdate = new List<Account>();
            for (AggregateResult result : results) {
                // get account id and number of active contacts
                String accId = String.valueOf(result.get('AccountId'));
                Integer totalContacts = Integer.valueOf(result.get('totalContacts'));
                // make sure you use Id feild in your account to update it
                Account acc = new Account(Id = accId, Active_Contacts__c = totalContacts);
                             
                accountsToUpdate.add(acc);
            }
            // update the final list of account
            update accountsToUpdate;
        }
        when AFTER_UPDATE {
            Set<Id> accountIds = new Set<Id>();
            for (Contact con : Trigger.new) {
                // capture the account id only if active checkbox value is flipped
                if (String.isNotBlank(con.AccountId) && Trigger.oldMap.get(con.Id).Active__c != con.Active__c) {
                    // write automation logic here
                    accountIds.add(con.AccountId);
                } else if (Trigger.oldMap.get(con.Id).AccountId != con.AccountId) {
                    accountIds.add(con.AccountId);
                    accountIds.add(Trigger.oldMap.get(con.Id).AccountId);
                }
            }
            // get aggregate result for all accounts
            List<AggregateResult> results = [
                SELECT AccountId, COUNT(Id) totalContacts
                FROM Contact
                WHERE Active__c = TRUE AND AccountId IN :accountIds
                GROUP BY AccountId
            ];
            // build final list of accounts to update
            List<Account> accountsToUpdate = new List<Account>();
            for (AggregateResult result : results) {
                // get account id and number of active contacts
                String accId = String.valueOf(result.get('AccountId'));
                Integer totalContacts = Integer.valueOf(result.get('totalContacts'));
                // make sure you use Id feild in your account to update it
                Account acc = new Account(Id = accId, Active_Contacts__c = totalContacts);
                accountsToUpdate.add(acc);
            }
            // update the final list of account
            update accountsToUpdate;
        }
    }
}
4

0 回答 0