2015-04-06 101 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())); 
}