使用触发器创建的记录填充查找字段

问题描述 投票:1回答:1

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

  1. 创建帐户后,创建一个具有相同名称的无关记录(称为“门户内容”记录),假设默认RecordTypeId
  2. 获取新创建的“门户内容”记录的ID,并将其插入最初创建的帐户的查找字段中

我当前的代码成功完成了第1项,但是当我添加新代码来完成第2项时,我收到以下错误:

Line: 6, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: 
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, newAccountCreated: execution of 
AfterInsert caused by: System.FinalException: Record is read-only 
Trigger.newAccountCreated: line 12, column 1: []

我目前的触发器如下:

trigger newAccountCreated on Account (after insert) {

    List<Account> alist = Trigger.New;

    for(Account a : alist) {

        if (a.RecordTypeId == '012i0000001Iy1H') {
//            system.debug(a.Name);
            Portal_Content__c p = new Portal_Content__c(Name=a.Name,School_SFDC_ID__c=a.Id);
            insert p;

            a.Portal_Content_Record__c = p.Id;
            update a;
        }
    }

}

我不明白为什么错误消息指出“记录是只读的”,或者如何修改此错误。

triggers salesforce apex apex-code
1个回答
1
投票

你不能在'after'触发器上更新相同的记录,它必须在before触发器上完成;改成:

trigger newAccountCreated on Account (before insert) {
© www.soinside.com 2019 - 2024. All rights reserved.