Salesforce 顶点触发连接对象

Salesforce 顶点触发连接对象,salesforce,apex,apex-trigger,Salesforce,Apex,Apex Trigger,请容忍我,因为我是Apex和triggers的新手 我有一个自定义对象,会议。此自定义对象是自定义连接对象“连接”的主对象。该连接对象设置为允许我显示标准对象联系人的相关列表,从而将联系人与会议关联 我正在尝试创建一个Apex触发器,它将在插入、更新、删除和取消删除通过连接对象分配给会议的联系人后触发。触发器需要做的就是更新Conferences对象上的一个字段,该字段显示与会议关联的联系人数 我觉得我必须让事情变得更难。我正在为这项任务的数据建模而苦苦挣扎——我假设我的触发器需要在Juncti

请容忍我,因为我是Apex和triggers的新手

我有一个自定义对象,会议。此自定义对象是自定义连接对象“连接”的主对象。该连接对象设置为允许我显示标准对象联系人的相关列表,从而将联系人与会议关联

我正在尝试创建一个Apex触发器,它将在插入、更新、删除和取消删除通过连接对象分配给会议的联系人后触发。触发器需要做的就是更新Conferences对象上的一个字段,该字段显示与会议关联的联系人数

我觉得我必须让事情变得更难。我正在为这项任务的数据建模而苦苦挣扎——我假设我的触发器需要在Junction对象上,因为这是触点计数的位置,所以我假设我的代码将开始:

在交叉点_uu_uC(插入后, 更新后,, 删除后, 取消删除后 ){

这看起来像是草率的代码,因为我认为只要连接对象被更新,触发器就会触发,而实际上唯一需要触发的时间就是触点数量发生变化。出于某种原因,我在概念上很难掌握我需要什么方法。我想它会包括junction_uc.Contacts.size()分配给会议的联系人数


任何帮助都将不胜感激。

是的,如果您想计算与联系人相关的连接记录的数量,您可以将触发器放在连接对象上。最佳做法是将逻辑放在类中而不是触发器中。下面是您想要的大致实现

trigger ConferenceAttendeesUpdater on Junction__c (after insert, after update, after delete, after undelete ) {
  if ( Trigger.isAfter && Trigger.isInsert ){
    junctionHelper.afterInsert(Trigger.new);
  }
  if ( Trigger.isAfter && Trigger.isUpdate ){
    junctionHelper.afterUpdate(Trigger.new, Trigger.old);
  }
  if ( Trigger.isAfter && Trigger.isDelete ){
    junctionHelper.afterInsert(Trigger.old);
  }
  if ( Trigger.isAfter && Trigger.isUndelete ){
    junctionHelper.afterInsert(Trigger.new);
  }
}

public without sharing junctionHelper(){
  public static void afterInsert(List<Junction__c> newList){
    Map<Id,Contact> contactRollup = new Map<Id,Contact>();
    for ( Integer i=0;i<newList[i].size();i++ ){
      if ( newList[i].Contact__c != null ){
        contactMap.put(newList[i].Contact__c, new Contact(Id=newList[i].Contact__c,Number_of_Junctions__c=0));
      }
    }
  }
  public static void afterUpdate(List<Junction__c> newList, List<Junction__c> oldList){
    Map<Id,Contact> contactRollup = new Map<Id,Contact>();
    for ( Integer i=0;i<newList[i].size();i++ ){
      if ( newList[i].Contact__c != oldList[i].Contact__c ){
        contactMap.put(newList[i].Contact__c, new Contact(Id=newList[i].Contact__c,Number_of_Junctions__c=0));
      }
    }
  }
  public static void rollUpContacts(Map<Id,Contact> contactMap){
    for ( AggregateResult ar : [
      SELECT COUNT(Id) cnt, Contact__c parentId
      FROM Junction__c
      WHERE Contact__c IN :contactMap.keySet()
    ]{
      Id parentId = (String)ar.get('parentId);
      Decimal cnt = (Decimal)ar.get('cnt');
      contactMap.put(parentId,new Contact(Id=parentId,Number_of_Junctions__c=cnt));
    }
    update contactMap.values();
  }
}
在联合体上触发与会者支持者会议(插入后、更新后、删除后、取消删除后){
if(Trigger.isAfter&&Trigger.isInsert){
junctionHelper.afterInsert(Trigger.new);
}
if(Trigger.isAfter&&Trigger.isUpdate){
junctionHelper.afterUpdate(Trigger.new、Trigger.old);
}
if(Trigger.isAfter&&Trigger.isDelete){
junctionHelper.afterInsert(Trigger.old);
}
if(Trigger.isAfter&&Trigger.isUndelete){
junctionHelper.afterInsert(Trigger.new);
}
}
public而不共享junctionHelper(){
插入后公共静态无效(列表新建列表){
Map contactRollup=new Map();

对于(整数i=0;iYes),如果要计算与联系人相关的连接\uuuuc记录的数量,可以将触发器放在连接\uuuc对象上。最佳做法是将逻辑放在类中,而不是放在触发器中。下面是您想要的大致实现

trigger ConferenceAttendeesUpdater on Junction__c (after insert, after update, after delete, after undelete ) {
  if ( Trigger.isAfter && Trigger.isInsert ){
    junctionHelper.afterInsert(Trigger.new);
  }
  if ( Trigger.isAfter && Trigger.isUpdate ){
    junctionHelper.afterUpdate(Trigger.new, Trigger.old);
  }
  if ( Trigger.isAfter && Trigger.isDelete ){
    junctionHelper.afterInsert(Trigger.old);
  }
  if ( Trigger.isAfter && Trigger.isUndelete ){
    junctionHelper.afterInsert(Trigger.new);
  }
}

public without sharing junctionHelper(){
  public static void afterInsert(List<Junction__c> newList){
    Map<Id,Contact> contactRollup = new Map<Id,Contact>();
    for ( Integer i=0;i<newList[i].size();i++ ){
      if ( newList[i].Contact__c != null ){
        contactMap.put(newList[i].Contact__c, new Contact(Id=newList[i].Contact__c,Number_of_Junctions__c=0));
      }
    }
  }
  public static void afterUpdate(List<Junction__c> newList, List<Junction__c> oldList){
    Map<Id,Contact> contactRollup = new Map<Id,Contact>();
    for ( Integer i=0;i<newList[i].size();i++ ){
      if ( newList[i].Contact__c != oldList[i].Contact__c ){
        contactMap.put(newList[i].Contact__c, new Contact(Id=newList[i].Contact__c,Number_of_Junctions__c=0));
      }
    }
  }
  public static void rollUpContacts(Map<Id,Contact> contactMap){
    for ( AggregateResult ar : [
      SELECT COUNT(Id) cnt, Contact__c parentId
      FROM Junction__c
      WHERE Contact__c IN :contactMap.keySet()
    ]{
      Id parentId = (String)ar.get('parentId);
      Decimal cnt = (Decimal)ar.get('cnt');
      contactMap.put(parentId,new Contact(Id=parentId,Number_of_Junctions__c=cnt));
    }
    update contactMap.values();
  }
}
在联合体上触发与会者支持者会议(插入后、更新后、删除后、取消删除后){
if(Trigger.isAfter&&Trigger.isInsert){
junctionHelper.afterInsert(Trigger.new);
}
if(Trigger.isAfter&&Trigger.isUpdate){
junctionHelper.afterUpdate(Trigger.new、Trigger.old);
}
if(Trigger.isAfter&&Trigger.isDelete){
junctionHelper.afterInsert(Trigger.old);
}
if(Trigger.isAfter&&Trigger.isUndelete){
junctionHelper.afterInsert(Trigger.new);
}
}
public而不共享junctionHelper(){
插入后公共静态无效(列表新建列表){
Map contactRollup=new Map();
对于(整数i=0;i