在 Apex Trigger 期间连接不相关的对象 [英] Connecting Unrelated Objects during Apex Trigger
问题描述
我正在尝试创建一个执行以下操作的触发器:
I am trying to create a trigger that does the following:
- 创建帐户后,假设默认 RecordTypeId,创建一个同名的无关记录(称为门户内容"记录)
- 获取新创建的门户内容"记录的 ID,并将其插入到原始创建的帐户的查找字段中
- 添加原账号的ID,并将其输入到新创建的门户内容"记录的字段中
步骤 1 和 2 在 使用记录填充查找字段从触发器创建.新问题是当尝试第 3 项时,在 Trigger.isAfter
代码块中,a.Portal_Content_Record__r
返回 null 而不是在 insert p 之后填充的 Id 值
在 Trigger.isBefore
块中.
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;
}
}
}
}
我的问题有两个部分:
- 如何在新插入的 Portal_Content__c 记录上分配一个字段,其中包含启动触发器的帐户的 ID?
- 这是否可以在此触发器内完成,或者是否需要辅助辅助"触发器?
推荐答案
我找到了在同一个 Trigger 中回答这个问题的方法.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 Trigger 期间连接不相关的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!