using(Erp.Contracts.CustCntSvcContract svcCust = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.CustCntSvcContract>(Db))
{
int vConNum = (Db.CustCnt.Where(r => r.Company == Session.CompanyID && r.WebUserUID_c == row.UD02_WebUserUID_c).Select(r => r.ConNum).DefaultIfEmpty(0).Max());
Erp.Tablesets.CustCntTableset ccntTs = new Erp.Tablesets.CustCntTableset();
ccntTs = svcCust.GetByID(row.UD02_CustNum_c, "", vConNum);
if (ccntTs != null)
{
foreach (var custCnt in ccntTs.CustCnt)
{
if (custCnt.Company == Session.CompanyID); //callContextClient.CurrentCompany)
{
if (custCnt.ConNum == vConNum);
{
//custCnt.AttrCodeList = row.UD02_JobRole_c.Trim();
custCnt.Inactive = true;
custCnt.NoContact = true;
/*The customer tableset doesn’t directly include CustAttr. Instead the entries in the table are in Customer.AttrCodeList. Apparently the Update method thinks that anything that is in this field should be added to erp.CustAttr. So, my solution is to set that field to "" when I update dsCustomer.*/
custCnt.AttrCodeList = "";
custCnt.RowMod = "U";
svcCust.Update(ref ccntTs);
}
}
}
}
We make a change to the AttrCodeList… CustCnAttr already contains the previous AttrCode and won’t allow it to be added again. My JobRole_c contains that existing
Is there anyway to query the table and only add the AttrCode of which is missing?
case "UPDATE":
// Code to execute when switchVar equals "Update"
Ice.Diagnostics.Log.WriteEntry("Processing update...");
using (Erp.Contracts.CustCntSvcContract svcCust = Ice.Assemblies.ServiceRenderer.GetService<Erp.Contracts.CustCntSvcContract>(Db))
{
int vConNum = (Db.CustCnt.Where(r => r.Company == Session.CompanyID && r.WebUserUID_c == row.UD02_WebUserUID_c).Select(r => r.ConNum).DefaultIfEmpty(0).Max());
Erp.Tablesets.CustCntTableset ccntTs = svcCust.GetByID(row.UD02_CustNum_c, "", vConNum);
if (ccntTs != null)
{
foreach (var custCnt in ccntTs.CustCnt)
{
if (custCnt.Company == Session.CompanyID)
{
if (custCnt.ConNum == vConNum)
{
custCnt.AttrCodeList = ""; // Add the attribute to AttrCodeList
custCnt.FirstName = row.UD02_FirstName_c;
custCnt.LastName = row.UD02_LastName_c;
custCnt.EMailAddress = row.UD02_Email_c;
custCnt.PhoneNum = row.UD02_Telephone_c;
custCnt.RowMod = "U";
svcCust.Update(ref ccntTs);
}
if (custCnt.ConNum == vConNum && string.IsNullOrEmpty(custCnt.AttrCodeList))
{
custCnt.AttrCodeList = row.UD02_JobRole_c.Trim();
custCnt.RowMod = "U";
svcCust.Update(ref ccntTs);
}
}
}
}
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_CustCnAttr'. Cannot insert duplicate key in object 'Erp.CustCnAttr'. The duplicate key value is (TEAGLE, 15394, , 4, OR).
The statement has been terminated.