我正在尝试在更新母帐户时将默认销售人员从母帐户复制到当前客户。由于某种原因,销售人员的记录被复制了。因此,我在“帐单设置”选项卡上选择一个上级帐户,然后在“销售人员”选项卡上,从上级帐户复制了两个重复的销售人员。上级帐户只有一名销售人员。保存客户时,我当然会出错:
“违反了主键约束'CustSalesPeople_PK'。无法在对象'dbo.CustSalesPeople'中插入重复的键”
我在代码中放置了一个断点,以确认它只被运行了一次。为什么会这样?
TIA!
public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
{
public PXSelect<CustSalesPeople, Where<CustSalesPeople.bAccountID,
Equal<Current<Customer.parentBAccountID>>>> ParentSalesPeople;
#region Event Handlers
protected virtual void Customer_ParentBAccountID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
{
if (e.Row == null)
return;
Customer cust = (Customer)e.Row;
if ((e.OldValue == null) || ((int?)e.OldValue != cust.ParentBAccountID))
{
if (cust.ParentBAccountID > 0)
{
foreach (CustSalesPeople salesPerson in ParentSalesPeople.Select())
{
if (salesPerson.IsDefault == true)
{
CustSalesPeople sp = Base.SalesPersons.Insert();
sp.BAccountID = cust.BAccountID;
sp.CommisionPct = salesPerson.CommisionPct;
sp.IsDefault = true;
sp.LocationID = salesPerson.LocationID;
sp.SalesPersonID = salesPerson.SalesPersonID;
sp = Base.SalesPersons.Update(sp);
//sender.PersistUpdated(sp);
break;
}
}
}
}
}
#endregion
}
答案 0 :(得分:0)
我会在这里碰到几件事。
首先,如错误消息所述,您可能正在插入重复的销售员记录。我先检查它是否存在于数据库中。
这也可能是由于销售人员已附加到客户所在位置。您将其附加到父级位置的locationID。您可能需要搜索客户(例如MAIN)的匹配locationCD,并根据需要使用该键更新销售人员。