此错误一直困扰着我两周。我是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;
看看我是否可以手动设置数量。
这个错误是什么意思,我该如何克服它?