我正在尝试创建一个执行以下操作的触发器:
第1步和第2步在帖子Populate Lookup Field with Record Created From Trigger中进行了介绍。新问题是,在尝试{3}时,在Trigger.isAfter
代码块中,a.Portal_Content_Record__r
返回null,而不是insert p
块中Trigger.isBefore
之后填充的Id值。 / p>
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;
}
}
}
}
我的问题分为两个部分:
答案 0 :(得分:0)
我能够找到在同一触发器中回答此问题的方法。 Trigger.isAfter && Trigger.isInsert
代码块解决了我的问题。
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
字段值。