2011-10-07 46 views
0

此错误一直困扰着我两周。我是Apex的新手,所以它可能很容易。Salesforce.com - 机会订单项上的数量更改无效

我有机会后更新触发器。

trigger triggerOpportunityCloseInstallDateChange on Opportunity (after update) 
{ 
    if(!Validator_cls.IsOpportunityScheduleUpdated()) 
    { 
     Validator_cls.SetOpportunityScheduleUpdatedDone(); 
     OpportunitySchedule.BuildScheduleAndUpdateDates(trigger.new, true); 
    } 
} 

我有它的包装,以确保它只被调用一次每更新。

当触发器触发时,应该更新订单项的计划日期,然后更新订单项计划。

public static void BuildScheduleAndUpdateDates(List<Opportunity> OpportunityList, Boolean DoUpdateItems) 
{ 
    system.debug('***********OpportunitySchedule    : BuildScheduleAndUpdateDates HIT'); 
    Map<String, Opportunity> OppMap = new Map<String, Opportunity>(); 
    List<OpportunityLineItem> ItemsToUpdate = new List<OpportunityLineItem>(); 
    List<String> IdList = new List<String>(); 

    for (Integer i = 0; i < OpportunityList.size(); i++) 
    { 
     IdList.add(OpportunityList[i].Id); 
     OppMap.put(OpportunityList[i].Id, OpportunityList[i]); 
    } 

    List<OpportunityLineItem> lineItems = [Select o.Id, (Select OpportunityLineItemId From OpportunityLineItemSchedules), o.Opportunity.Id, o.Systems_Add_On__c, o.ServiceDate, o.Product_Family__c, o.Schedule_Length__c , o.Monthly_Quantity__c, o.Monthly_Amount__c, o.Quantity 
              From OpportunityLineItem o 
              where o.Opportunity.Id in:IdList]; 

     for (OpportunityLineItem item : lineItems) 
     { 
      Opportunity opp_new = OppMap.get(item.Opportunity.Id); 

      Boolean hasSchedule = OpportunityProductLineItems.DoesLineItemHaveSchedule(item.Id); 

      if (opp_new.Term_Conversion__c != null) 
      { 
       item.ServiceDate = opp_new.CloseDate; 
      } 
      else 
      { 
       if(item.Product_Family__c == 'Systems') 
       { 
        if(item.Systems_Add_On__c == true) 
        { 
         item.ServiceDate = opp_new.Install_Date__c; 
         system.debug('***********BuildScheduleAndUpdateDates  : Systems With Systems Add On ' + item.ServiceDate); 
        } 
        else 
        { 
         item.ServiceDate = opp_new.Install_Date__c.addDays(30); 
         system.debug('***********BuildScheduleAndUpdateDates  : Systems With NO Systems Add On ' + item.ServiceDate); 
        } 
       } 
       else if(hasSchedule) 
       { 
        item.ServiceDate = opp_new.Install_Date__c.addDays(30); 
        system.debug('***********BuildScheduleAndUpdateDates  : Has Schedule ' + item.ServiceDate); 
       } 
       else 
       { 
        item.ServiceDate = opp_new.Install_Date__c; 
        system.debug('***********BuildScheduleAndUpdateDates  : No Schedule ' + item.ServiceDate); 
       } 
      } 

      //item.Quantity = 1; 
      ItemsToUpdate.add(item); 
      if(hasSchedule && !DoUpdateItems) 
      { 
       ProcessSchedule(item); 
      } 
     } 

    if(DoUpdateItems) 
    { 
     update ItemsToUpdate; 
    }  
    upsert schedulesToUpsert; 

} 

当我更新的机会,我得到这个错误:

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 AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00kV0000002cHhTIAU; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: unknown (invalid quantity change on opportunity line item): [unknown]: Class.OpportunitySchedule.BuildScheduleAndUpdateDates: line 68, column 1 

该错误对应于这一行

update ItemsToUpdate; 

正如你可以在上面看到,我不会改变的该订单项的数量。我很困惑。如果我注释掉这条线,一切正常(计划建立和更新)。

我甚至把item.Quantity = 1;看看我是否可以手动设置数量。

这个错误是什么意思,我该如何克服它?

回答

0

您遇到的错误通常归因于某些类型的salesforce需要在后端保持同步的特殊字段。例如,如果您添加具有与针对商机设置的不同的价格手册的行项目,则会出现该错误。

在这种情况下,可能是由于某些外力改变了数量,或者可能由于行项目的数量是特定值而导致附表记录。

对于外力改变数量,我会尝试在打电话给ProcessSchedule(item)前后发出调试声明。由于SObject被传递给函数by reference,因此该函数可能会在您没有意识到的情况下修改item

如果不知道其他代码的含义,很难确切地说明问题所在,但希望这可以帮助您朝正确的方向发展!

0

试试这个:

OppMap.put(OpportunityList[i].Id, OpportunityList[i].clone(true, true)); 

在克隆调用第一个“真正的”保留您的sObjects的识别码与第二指示克隆进行深拷贝,通过引用问题打破了。

[编辑]

重读您的文章我才意识到,这可能不是解决你的问题 - 对不起!但深层次的克隆技巧确实有助于引用问题。祝你好运。