Salesforce Apex 错误:SELF_REFERENCE_FROM_TRIGGER

发布于 2024-12-04 04:58:01 字数 2225 浏览 1 评论 0原文

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger triggerOpportunityCloseInstallDateChange caused an unexpected exception, contact your administrator: triggerOpportunityCloseInstallDateChange: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00o30000003ySNhAAM; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0063000000i23T9) is currently in trigger triggerOpportunityCloseInstallDateChange, therefore it cannot recursively update itself: []: Class.OpportunitySchedule.BuildScheduleAndUpdateDates: line 17, column 5

当我尝试执行下面的代码时,出现上述错误。

我在机会“之前”有一个触发器。然后它使用trigger.new调用下面的类。

public with sharing class OpportunitySchedule {

    public static void BuildScheduleAndUpdateDates(List<Opportunity> OpportunityList) {

        for (Integer i = 0; i < OpportunityList.size(); i++)
        {
            Opportunity opp_new = OpportunityList[i];

            List<OpportunityLineItem> lineItems = [Select o.Id, (Select OpportunityLineItemId From OpportunityLineItemSchedules), o.System_Add_on__c, o.ServiceDate, o.Schedule_Length__c , o.Monthly_Quantity__c, o.Monthly_Amount__c
                                                From OpportunityLineItem o
                                                where o.Opportunity.Id =  :opp_new.Id];

            for (OpportunityLineItem item : lineItems)
            {
                item.ServiceDate = opp_new.CloseDate;
                update item;
                delete item.OpportunityLineItemSchedules;       
            }                                   
        }
    }
}

当有人编辑机会时,我尝试删除所有机会行项目计划。奇怪的是,我可以删除删除 item.OpportunityLineItemSchedules 行并且代码运行,它将更新该项目。我不明白为什么删除孩子的孩子(机会 - > OpportunityLineItem - > OpportunityLineItemSchedule)会导致递归循环。

我尝试在此链接中实现以下代码,但没有成功: http:// boards.developerforce.com/t5/Apex-Code-Development/Trigger-is-fired-twice-due-to-the-workflow...

我还注释掉了所有其他触发器以确保其中之一不是造成这种情况的原因。

有谁知道我做错了什么?

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger triggerOpportunityCloseInstallDateChange caused an unexpected exception, contact your administrator: triggerOpportunityCloseInstallDateChange: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00o30000003ySNhAAM; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0063000000i23T9) is currently in trigger triggerOpportunityCloseInstallDateChange, therefore it cannot recursively update itself: []: Class.OpportunitySchedule.BuildScheduleAndUpdateDates: line 17, column 5

I'm getting the above error when I try to excute the code below.

I have a trigger on the "before" of an opportunity. It then calls the below class with trigger.new.

public with sharing class OpportunitySchedule {

    public static void BuildScheduleAndUpdateDates(List<Opportunity> OpportunityList) {

        for (Integer i = 0; i < OpportunityList.size(); i++)
        {
            Opportunity opp_new = OpportunityList[i];

            List<OpportunityLineItem> lineItems = [Select o.Id, (Select OpportunityLineItemId From OpportunityLineItemSchedules), o.System_Add_on__c, o.ServiceDate, o.Schedule_Length__c , o.Monthly_Quantity__c, o.Monthly_Amount__c
                                                From OpportunityLineItem o
                                                where o.Opportunity.Id =  :opp_new.Id];

            for (OpportunityLineItem item : lineItems)
            {
                item.ServiceDate = opp_new.CloseDate;
                update item;
                delete item.OpportunityLineItemSchedules;       
            }                                   
        }
    }
}

I'm trying to delete all of the Opportunity Line Item Schedules when someone edits an opportunity. The weird thing is, I can remove the delete item.OpportunityLineItemSchedules line and the code runs, it will update the item. I don't understand why deleting a childs children (Opportunity -> OpportunityLineItem -> OpportunityLineItemSchedule) would cause a recursive loop.

I've tried implimenting the below code in this link with no luck:
http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-is-fired-twice-due-to-the-workflow...

I've also commented out all other triggers to make sure one of them aren't causing it.

Does anyone know what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-12-11 04:58:01

我注意到一些事情。首先,切勿将 DML 放入循环内,尤其是在触发器内时。在这里阅读批量触发器会有所帮助:http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers.htm

在您的代码中您就快到了。不要在循环中进行更新,只需在循环后更新整个列表:

for (OpportunityLineItem item : lineItems)
{
   item.ServiceDate = opp_new.CloseDate;
   //update item;
   //delete item.OpportunityLineItemSchedules;       
}

update lineItems;

然后您将创建一个仅包含 ParentId == OpportunityLineItem.Id 的 OpportunityLineItemSchedules 的新列表。然后,您可以在一次调用中删除整个列表:

delete saidList;

至于递归,在主从关系中,Force.com 将自动处理子项的删除。在查找中则不然,您需要手动删除它们。虽然我具体不确定 OpportunityLineItemSchedules,但我会尝试使用 AFTER 触发器来启动,或者使用触发器线程保留在内存中的帮助程序类,以确保一旦进入触发器处理程序类,它就不会再次输入。

不幸的是,以上就是我能分享的全部内容!祝你好运,欢迎来到 Force.com 编程。希望它能在你身上成长。

A few things I noticed. First, never put DML inside a loop and especially when inside of a trigger. Reading up on Bulkified triggers here would help: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers.htm

In your code you're almost there. Instead of doing your update in the loop, simply update your entire list after the loop:

for (OpportunityLineItem item : lineItems)
{
   item.ServiceDate = opp_new.CloseDate;
   //update item;
   //delete item.OpportunityLineItemSchedules;       
}

update lineItems;

Then you would make a new list of just OpportunityLineItemSchedules that had ParentId == OpportunityLineItem.Id. Then you would delete that entire list in one call:

delete saidList;

As for the recursion, in a master-detail relationship Force.com will handle the deletion of children automatically. Not so in a lookup, where you need to delete these by hand. Though I'm uncertain about OpportunityLineItemSchedules specifically, I'd try either starting things with using an AFTER trigger, or use a helper class that your trigger thread keeps in memory to ensure that once inside of your trigger handler class, that it doesn't enter it again.

Unfortunately the above is all I had a moment to share! Good luck and welcome to Force.com programming. Hope it grows on you.

惟欲睡 2024-12-11 04:58:01

我不明白为什么删除子项的子项(Opportunity -> OpportunityLineItem -> OpportunityLineItemSchedule)会导致递归循环。

使用收入计划时,父 OpportunityLineItem 上的 TotalPrice 会根据关联的 OpportunityLineItemSchedules 进行更新。因此,当您删除 OpportunityLineItemSchedule 记录时,您实际上是在更新 OpportunityLineItem,这会导致 SELF_REFERENCE_FROM_TRIGGER DML 异常。

请参阅对机会和机会订单项的影响 OpportunityLineItemSchedule 文档的部分。

删除 OpportunityLineItemSchedule 对相关 OpportunityLineItem 和 Opportunity 具有类似的效果。删除 OpportunityLineItemSchedule 会使 OpportunityLineItem TotalPrice 减少已删除的 OpportunityLineItemSchedule 数量或收入金额。机会金额也会减少 OpportunityLineItemSchedule 数量或收入金额,机会预期收入会减少 OpportunityLineItemSchedule 数量或收入金额乘以机会概率。

I don't understand why deleting a childs children (Opportunity -> OpportunityLineItem -> OpportunityLineItemSchedule) would cause a recursive loop.

When using revenue schedules the TotalPrice on the parent OpportunityLineItem is updated based on the associated OpportunityLineItemSchedules. So when you delete the OpportunityLineItemSchedule records you are effectively updating the OpportunityLineItem, which causes the SELF_REFERENCE_FROM_TRIGGER DML Exception.

See the Effects on Opportunities and Opportunity Line Items section of the OpportunityLineItemSchedule documentation.

Deleting an OpportunityLineItemSchedule has a similar effect on the related OpportunityLineItem and Opportunity. Deleting an OpportunityLineItemSchedule decrements the OpportunityLineItem TotalPrice by the deleted OpportunityLineItemSchedule Quantity or Revenue amount. The Opportunity Amount is also decremented by the OpportunityLineItemSchedule Quantity or Revenue amount, and the Opportunity ExpectedRevenue is reduced by OpportunityLineItemSchedule Quantity or Revenue amount multiplied by the Opportunity Probability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文