【问题标题】:Avoid duplicates when updating records in CRM 2011 C#在 CRM 2011 C# 中更新记录时避免重复
【发布时间】:2015-06-10 15:26:49
【问题描述】:

我编写从 CRM 检索记录并更新此记录的应用程序。 但是当我运行“service.Update(...)”时,记录是重复的(我在 SQL Server DB 中看到它)。 我只想拥有来自特定 Guid 的一条记录,现在我有 2 条。

            foreach (DataRow row in rsltFromSql.Rows)
            {
                ActivityMimeAttachment attachmentMimeTemp = new ActivityMimeAttachment();
                try
                {
                    attachmentMimeTemp = (ActivityMimeAttachment)handlerCrm.CrmService.Retrieve(ActivityMimeAttachment.EntityLogicalName, Guid.Parse(row["ActivityMimeAttachmentId"].ToString()), new ColumnSet(true));
                }
                catch (Exception ex)
                {
                    /////
                }

                //delete body field
                attachmentMimeTemp.Body = null;

                //update the attachment with body = null
                handlerCrm.CrmService.Update(attachmentMimeTemp);

                attachmentMimeGuidList.Add(new Guid(row["ActivityMimeAttachmentId"].ToString()));
            }

【问题讨论】:

  • 您是否有任何其他插件或工作流在更新 ActivityMimeAttachment 时执行?你在用 attachmentMimeGuidList 做什么?您是执行更新还是在其他地方创建?

标签: c# sql-server dynamics-crm-2011 crm


【解决方案1】:

不是创建ActivityMimeAttachment 的新对象实例,而是直接从Retrieve 方法为其分配返回值。像这样:

foreach (DataRow row in rsltFromSql.Rows)
{
    ActivityMimeAttachment attachmentMimeTemp;
    try
    {
        attachmentMimeTemp = (ActivityMimeAttachment)handlerCrm.CrmService.Retrieve(ActivityMimeAttachment.EntityLogicalName, Guid.Parse(row["ActivityMimeAttachmentId"].ToString()), new ColumnSet(true));
    }
    catch (Exception ex)
    {
         /////
    }

    //delete body field
    attachmentMimeTemp.Body = null;

    //update the attachment with body = null
    handlerCrm.CrmService.Update(attachmentMimeTemp);

    attachmentMimeGuidList.Add(new Guid(row["ActivityMimeAttachmentId"].ToString()));
}

【讨论】:

    【解决方案2】:

    删除activityMimeAttachment并新建一个:

                //Retrieve ActivityMimeAttachment
                ActivityMimeAttachment attachmentMimeTemp;
    
                attachmentMimeTemp = (ActivityMimeAttachment)handlerCrm.CrmService.Retrieve(ActivityMimeAttachment.EntityLogicalName, Guid.Parse(row["ActivityMimeAttachmentId"].ToString()), new ColumnSet(true));
    
                //delete the old attachment (kit)
                handlerCrm.CrmService.Delete(ActivityMimeAttachment.EntityLogicalName, attachmentMimeTemp.Id);
    
                //create new attachment to the Email
                ActivityMimeAttachment newAttachment = new ActivityMimeAttachment
                {
                    ObjectId = attachmentMimeTemp.ObjectId,
                    ObjectTypeCode = "email",
                    Body = System.Convert.ToBase64String(
                                    new ASCIIEncoding().GetBytes("Example Attachment")),
                    FileName = String.Format("newFile")
                };
                handlerCrm.CrmService.Create(newAttachment);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多