2011-09-08 130 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行和代碼運行,它會更新該項目。我不明白爲什麼刪除孩子的孩子(機會 - > 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; 

然後你會作出公正OpportunityLineItemSchedules說了的ParentId == OpportunityLineItem.Id的新列表。然後,你會刪除整個列表在一個電話:

delete saidList; 

至於遞歸,在主從關係Force.com將自動處理孩子的刪除。在查找中不是這樣,你需要手動刪除它們。雖然我不確定有關OpportunityLineItemSchedules的具體情況,但我會嘗試使用AFTER觸發器開始,或者使用觸發器線程保存在內存中的助手類,以確保一旦觸發器處理程序類內部沒有再次輸入。

不幸的是,以上所有我有一個時刻分享!祝好運,歡迎來到Force.com編程。希望它在你身上成長。

1

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

當使用收入時間表時,基於關聯的OpportunityLineItemSchedules更新父代OpportunityLineItem上的TotalPrice。因此,當您刪除OpportunityLineItemSchedule記錄時,您正在有效更新導致SELF_REFERENCE_FROM_TRIGGER DML異常的OpportunityLineItem。

查看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