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”字段会自动更新,从而反映出更新后联系人的总数。

相关内容

热门资讯

透视好友"拱趴游戏破... 透视好友"拱趴游戏破解器"详细辅助透明挂教程(黑侠破解神器);拱趴游戏破解器软件透明挂更新新赛季,主...
透视免费"哈糖大菠萝... 透视免费"哈糖大菠萝有挂吗"详细辅助解密教程(透视挂底牌软件);实战中需综合运用上述技巧,并根据牌桌...
透视线上"we-po... 透视线上"we-poker靠谱吗"详细辅助技巧教程(透视脚本免费app方法);建议优先通过we-po...
透视新版"破解辅助插... 透视新版"破解辅助插件wepoker"详细辅助解密教程(辅助器攻略)这是由厦门游乐互动科技有限公司精...
透视ai代打"WeP... 透视ai代打"WePoKer辅助器"详细辅助曝光教程(国外版透视技巧)是一款可以让一直输的玩家,快速...
透视攻略"wejok... 透视攻略"wejoker辅助软件"详细辅助AI教程(手机版透视脚本软件);1.wejoker辅助软件...
透视计算"wepok... 透视计算"wepoker怎么看底牌"详细辅助透明挂教程(手机版辅助技巧);1分钟了解详细教程(微信 ...
透视好友房"hhpo... 透视好友房"hhpoker是真的吗"详细辅助解密教程(辅助器要钱玩教程)关于hhpoker是真的吗机...
透视讲解"wepok... 透视讲解"wepoker数据分析工具"详细辅助德州教程(透视有用神器);玩家必备必赢加哟《13670...
透视教学"wepok... 1、透视教学"wepoker怎么看牌型"详细辅助科技教程(有透视教程);详细教程。2、wepoker...