使用从触发器创建的记录来填充查找字段 [英] Populate Lookup Field with Record Created From Trigger

查看:42
本文介绍了使用从触发器创建的记录来填充查找字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

My current code does item 1 successfully, but when I added new code to complete item 2, I received the following error:

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;
        }
    }

}

我不明白为什么错误消息指出记录是只读的"或如何修改此错误.

I do not understand why the the error message states that the "Record is read only," or how to amend this error.

推荐答案

您不能在之后"触发器上更新同一记录,必须在之前触发器上完成;更改为:

You cannot update the same record on an 'after' trigger, it must be done on a before trigger; change to:

trigger newAccountCreated on Account (before insert) {

这篇关于使用从触发器创建的记录来填充查找字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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