Apex类中的“AfterUpdate-TotalContactsRoll-UpSummary
创始人
2024-11-10 04:30:22
0

在Apex类中,可以使用触发器(trigger)和Roll-Up Summary字段(滚动汇总字段)来实现联系人对象(Contact)上基于Master-Detail关系的一个“Total Contacts”字段自动更新,以便在每次联系人被更新时都能够正确地计算总的联系人数量。

以下是代码示例:

触发器部分:

trigger TotalContactsTrigger on Contact (after insert, after update, after delete) { TotalContactsRollupHandler.handleTrigger(triggerOperation, triggerNew, triggerOldMap); }

处理程序部分:

public with sharing class TotalContactsRollupHandler {

public static void handleTrigger(Operation triggerOperation, List contacts, Map oldContacts) {
  
    // Only handle after update events
    if (triggerOperation == Operation.AFTER_UPDATE) {
      
        // Create a set of all parent account ids to update
        Set accountIds = new Set();
        for (Contact contact : contacts) {
            accountIds.add(contact.AccountId);
        }
      
        // Update the parent accounts with the new contact count using the roll-up summary field
        List accountsToUpdate = new List([SELECT Id FROM Account WHERE Id IN :accountIds]);
        if (accountsToUpdate.size() > 0) {
            update accountsToUpdate;
        }
    }
}

}

通过这个方法,每当联系人被更新时,“Total Contacts Roll-Up Summary”字段会自动更新,从而反映出更新后联系人的总数。

相关内容

热门资讯

透视讲解"wepok... 透视讲解"wepokerplus万能挂"详细辅助透明教程(透视有用方法)1、许多玩家不知道wepok...
透视讲解"wepok... 透视讲解"wepoker黑侠辅助器正版下载"详细辅助高科技教程(好友助力码方法)准备好在wepoke...
透视辅助"德州私人局... 透视辅助"德州私人局脚本"详细辅助2025新版技巧(可以买辅助技巧);1.德州私人局脚本 ai辅助创...
透视存在"哈糖大菠萝... 1、透视存在"哈糖大菠萝可以开挂吗"详细辅助力荐教程(买脚本靠谱教程)2、进入游戏-大厅左侧-新手福...
透视计算"智星德州插... 《透视计算"智星德州插件2024最新版"详细辅助新2025版(可以破解游戏教程)》 智星德州插件20...
透视了解"xpoke... 透视了解"xpoker辅助器"详细辅助透牌教程(有机器人教程);1.xpoker辅助器 ai辅助创建...
透视好友房"wepo... 透视好友房"wepoker模拟器哪个"详细辅助2025新版总结(可以开辅助神器);建议优先通过wep...
透视玄学"wpk透视... 透视玄学"wpk透视插件"详细辅助专业教程(买脚本靠谱透明)关于wpk透视插件机制的,其中提到了后台...
透视辅助"aapok... 透视辅助"aapoker怎么选牌"详细辅助插件教程(私人局有透视攻略)关于aapoker怎么选牌机制...
透视实锤"wepok... 透视实锤"wepoker私人局辅助挂"详细辅助透牌教程(可以下载游戏攻略);大神普及一款德州ai内幕...