2015-04-06 64 views
0

我编写从CRM中检索记录并更新此记录的应用程序。 但是,当我运行“service.Update(...)”,记录是重复的(我在SQL Server数据库中看到它)。 我想有从特定的Guid只有一个记录,我现在有2在CRM 2011中更新记录时避免重复C#

  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())); 
      } 
+0

你有上ActivityMimeAttachment的更新执行任何其他插件或工作流?你在用attachmentMimeGuidList做什么?你是否执行更新或创建其他地方? – Patrick 2015-04-23 16:47:38

回答

0

删除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); 
0

,而不是创建的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())); 
}