Hi Dynamicweb,
We are having some issues with importing secondary user relations as we end up have duplicates entries.
In the method: UpdateUserSecondaryRelationBeforeMoveToMainTable we have this section:
if (existingUserId > 0)
{
string sourceSecondaryUserId = row["AccessUserSecondaryRelationSecondaryUserID"].ToString();
List<int> existingSecondaryUserIds = new List<int>();
if (!processedSecondaryIdUserIdDictionary.TryGetValue(sourceSecondaryUserId, out existingSecondaryUserIds))
{
existingSecondaryUserIds = new List<int>();
var usersFoundByCustomerNumber = GetExistingUsersBySearchColumn("AccessUserCustomerNumber", sourceSecondaryUserId);
if (usersFoundByCustomerNumber != null)
{
existingSecondaryUserIds.AddRange(usersFoundByCustomerNumber.Select(u => Converter.ToInt32(u["AccessUserId"])));
}
int existingSecondaryUserId = GetExistingUserOrGroupID(sourceSecondaryUserId);
if (existingSecondaryUserId > 0)
{
existingSecondaryUserIds.Add(existingSecondaryUserId);
}
existingSecondaryUserIds = existingSecondaryUserIds.Distinct().ToList();
if (existingSecondaryUserIds.Count == 0)
{
_logger.Log(string.Format("Error importing user Secondary Relation: No secondary user or group found with ID or {0} or CustomerNumber equal to: '{1}'", searchColumn, sourceSecondaryUserId));
}
processedSecondaryIdUserIdDictionary.Add(sourceSecondaryUserId, existingSecondaryUserIds);
}
if (existingSecondaryUserIds.Count > 0)
{
row["AccessUserSecondaryRelationUserID"] = existingUserId;
row["AccessUserSecondaryRelationSecondaryUserID"] = existingSecondaryUserIds[0];
foreach (int existingSecondaryUserId in existingSecondaryUserIds.Skip(1))
{
DataRow newRow = dataTable.NewRow();
newRow["AccessUserSecondaryRelationUserID"] = existingUserId;
newRow["AccessUserSecondaryRelationSecondaryUserID"] = existingSecondaryUserId;
rowsToAdd.Add(newRow);
}
}
}
Our issue is that: GetExistingUsersBySearchColumn("AccessUserCustomerNumber", sourceSecondaryUserId); is executed no matter what. I think a better approach will be only to search by CustomerNumber if we have no hits. Please see this changed block of code:
int existingSecondaryUserId = GetExistingUserOrGroupID(sourceSecondaryUserId);
if (existingSecondaryUserId > 0)
{
existingSecondaryUserIds.Add(existingSecondaryUserId);
}
else
{
var usersFoundByCustomerNumber = GetExistingUsersBySearchColumn("AccessUserCustomerNumber", sourceSecondaryUserId);
if (usersFoundByCustomerNumber != null)
{
existingSecondaryUserIds.AddRange(usersFoundByCustomerNumber.Select(u => Converter.ToInt32(u["AccessUserId"])));
}
existingSecondaryUserIds = existingSecondaryUserIds.Distinct().ToList();
}
I hope you will find it worth changing?
Best regards, Anders