在Apex触发期间连接不相关的对象 [英] Connecting Unrelated Objects during Apex Trigger

查看:70
本文介绍了在Apex触发期间连接不相关的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个执行以下操作的触发器:

I am trying to create a trigger that does the following:

  1. 创建帐户后,请使用默认的RecordTypeId创建一个不相关的记录(称为门户内容"记录),并使用相同的名称
  2. 获取新创建的门户内容"记录的ID,并将其插入到最初创建的帐户"上的查找字段中.
  3. 添加原始帐户的ID,并将其输入到新创建的门户内容"记录的字段中

步骤1和2在帖子带有记录的查找字段中得到了解决从触发器创建.新的问题是,在尝试第3项时,在Trigger.isAfter代码块中,a.Portal_Content_Record__r返回null,而不是使用Trigger.isBefore块中insert p之后填充的Id值.

Steps 1 and 2 were addressed in post Populate Lookup Field with Record Created From Trigger. The new problem is that when attempting Item 3, in the Trigger.isAfter code block, a.Portal_Content_Record__r returns null rather than with the Id value populated after insert p in the Trigger.isBefore block.

trigger newAccountCreated on Account (before insert, after insert) {

    List<Account> alist = Trigger.New;

    if(Trigger.isBefore){

        for(Account a : alist) {

            if (a.RecordTypeId == '012i0000001Iy1H') {
                Portal_Content__c p = new Portal_Content__c(
                    Name=a.Name,
                    RecordTypeId='012i0000001J1zZ'
                );
                insert p;

                a.Portal_Content_Record__c = p.Id;
                system.debug('Made it to insert p. P = ' + p.Id +'. a.Portal_Content_Record__c = ' + a.Portal_Content_Record__c);                
            }
        }
    }

    if (Trigger.isAfter) {
        for(Account a : alist){
            system.debug('a.Id = ' + a.Id + ', p = ' +a.Portal_Content_Record__r);
            String p = a.Portal_Content_Record__c;
            for(Portal_Content__c port : [SELECT ID FROM Portal_Content__c WHERE Id = :p]){
                port.School_SFDC_ID__c = a.Id;
                update port;
            }
        }
    }
}

我的问题分为两个部分:

My question has two parts:

  1. 如何在新插入的Portal_Content__c记录上为启动触发器的帐户的ID分配一个字段?
  2. 可以在此触发器内完成此操作,还是需要辅助辅助"触发器?

推荐答案

我能够找到在同一触发器中回答此问题的方法. Trigger.isAfter && Trigger.isInsert代码块解决了我的问题.

I was able to figure out a way to answer this issue within the same Trigger. The Trigger.isAfter && Trigger.isInsert code block is what solved my problem.

trigger newAccountCreated on Account (before insert, after insert, after delete) {

    List<Account> alist = Trigger.New;
    List<Account> oldlist = Trigger.old;

    if(Trigger.isBefore){

        for(Account a : alist) {

            if (a.RecordTypeId == '012i0000001Iy1H') {
                Portal_Content__c p = new Portal_Content__c(
                    Name=a.Name,
                    RecordTypeId='012i0000001J1zZ'
                );
                insert p;

                a.Portal_Content_Record__c = p.Id;
            }
        }
    }
    else if (Trigger.isAfter && Trigger.isDelete){
        for(Account a : oldlist){
            for(Portal_Content__c p : [SELECT ID FROM Portal_Content__c WHERE ID = :a.Portal_Content_Record__c]){
                delete p;
            }
        }

    }

    if (Trigger.isAfter && Trigger.isInsert){
        for(Account a : alist){

            List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Id = :a.Portal_Content_Record__c];

            for(Portal_Content__c p : plist){
                p.School_SFDC_ID__c = a.Id;
                update p;
            }

        }
    }

}

此代码块对与在第一个代码块中分配的客户记录的Portal_Content_Record__c字段值匹配的Portal_Contact__c记录进行查询.然后,它将获取找到的Portal_Content__c记录,并将原始帐户的ID分配给记录的School_SFDC_ID__c字段值.

This code block does a query for Portal_Contact__c records that match the Account record's Portal_Content_Record__c field value, which is assigned in the first code block. It then takes the Portal_Content__c records found, and assigns the original Account's Id to the record's School_SFDC_ID__c field value.

这篇关于在Apex触发期间连接不相关的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆