2011-09-08 62 views
1
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 

以下のコードを実行しようとすると、上記のエラーが発生します。これはAPEXの土地の私の二日目です。Salesforce Apexエラー:SELF_REFERENCE_FROM_TRIGGER

私は機会の "前"にトリガーを持っています。それから、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行を削除することができ、コードが実行され、アイテムを更新します。子どもの子供を削除する理由(Opportunity - > OpportunityLineItem - > OpportunityLineItemSchedule)が再帰的ループを引き起こす原因はわかりません。

私は運では、このリンクに以下のコードをimplimenting試してみた: http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-is-fired-twice-due-to-the-workflow ...

私もそのうちの一つがそれを引き起こしていないことを確認する他のすべてのトリガーをコメントアウトしました。

誰かが間違っていることを知っていますか?

答えて

1

私が気づいたいくつかの事柄。最初に、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の新しいリストを作成します。 Force.comは、自動的に子供の削除を処理する主従関係では、再帰については

delete saidList; 

:その後、1回の呼び出しでそのリスト全体を削除します。あなたが手でこれらを削除する必要があるルックアップではそうではありません。 OpportunityLineItemSchedulesについて具体的にはわかりませんが、AFTERトリガーを使用して作業を開始するか、トリガー・スレッドがメモリー内に保持しているヘルパー・クラスを使用して、トリガー・ハンドラー・クラス内でもう一度入力してください。

残念ながら、上記のすべてが私が共有する瞬間でした! Force.comのプログラミングにようこそ。それがあなたに育つことを願っています。

1

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

収入のスケジュールを使用して、親OpportunityLineItem上TotalPriceは、関連OpportunityLineItemSchedulesに基づいて更新されます。したがって、OpportunityLineItemScheduleレコードを削除すると、効果的にOpportunityLineItemが更新され、SELF_REFERENCE_FROM_TRIGGER DML例外が発生します。

OpportunityLineItemScheduleドキュメントのEffects on Opportunities and Opportunity Line Itemsセクションを参照してください。

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.

+0

こんにちは、私は同じproblem.Can持っているあなたはhttp://salesforce.stackexchange.com/questions/43871/system-dmlexception-delete-failed-self-reference-from-triggerを参照してください。このリンクをクリックしてくださいすべてログ私は同じ問題があります。 – Prathyush

関連する問題