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

相关内容

热门资讯

2分钟细说!佛手在线是不是有挂... 2分钟细说!佛手在线是不是有挂,wepoker透视脚本安卓,安装教程(有挂方法);1.佛手在线是不是...
查到实测!wepoker手机助... 查到实测!wepoker手机助手,拱趴大菠萝挂哪里,解说技巧(有挂技巧)准备好在拱趴大菠萝挂哪里ia...
必备教程!德普之星透视辅助,哈... 必备教程!德普之星透视辅助,哈糖大菠萝怎么挂,曝光教程(有挂软件);实战中需综合运用上述技巧,并根据...
玩家必看攻略!红龙poker辅... 玩家必看攻略!红龙poker辅助工具,pokernow辅助工具,详细教程(有挂辅助)是一款可以让一直...
推荐一款!poker worl... 推荐一款!poker world辅助,hhpkoer辅助挂是真的吗,透明挂教程(有挂神器)这是由厦门...
我来教教大家!newpoker... 我来教教大家!newpoker脚本,wepoker辅助插件功能,解说技巧(有挂神器);建议优先通过w...
信息共享!wejoker透视方... 信息共享!wejoker透视方法,hhpoker辅助挂下载,科技教程(有挂攻略);致您一封信;亲爱h...
玩家攻略!hhpoker透视脚... 玩家攻略!hhpoker透视脚本视频,hhpoker辅助软件,必备教程(有挂攻略);1分钟了解详细教...
推荐一款!aapoker如何设... 此外,数据分析德州(aapoker如何设置胜率)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
如何分辨真伪!约局吧德州真的有... 如何分辨真伪!约局吧德州真的有透视挂吗,aapoker怎么拿好牌,2025新版(有挂方法)1、许多玩...