none
我用BulkDeleteRequest 删除一批记录,CRM会异步执行删除操作,请问怎么查询删除的状态,查看是否完成? RRS feed

答案

  • SDK 里有例子

    #region check success
             
             // Query for bulk delete operation and check for status.
             QueryByAttribute bulkQuery = new QueryByAttribute();
             bulkQuery.ColumnSet = new AllColumns();
             bulkQuery.EntityName = EntityName.bulkdeleteoperation.ToString();
    
             // NOTE: When the bulk delete operation was submitted, the GUID that was returned 
             // was the asyncoperationid, not the bulkdeleteoperationid.
             bulkQuery.Attributes = new string[] { "asyncoperationid" };
             bulkQuery.Values = new object[1];
             bulkQuery.Values[0] = response.JobId;
    
             // With only the asyncoperationid at this point, a RetrieveMultiple is required to get the
             // bulk delete operation created previously.
             BusinessEntityCollection entityCollection = service.RetrieveMultiple(bulkQuery);
             bulkdeleteoperation createdBulkDeleteOperation = null;
    
             // Monitor the async operation via polling until it is complete or the max polling time expires.
             const int ARBITRARY_MAX_POLLING_TIME = 60;
             int secondsTicker = ARBITRARY_MAX_POLLING_TIME;
             while (secondsTicker > 0)
             {
                // Make sure the async operation was retrieved.
                if (entityCollection.BusinessEntities.Length > 0)
                {
                   // Grab the one bulk operation that has been created.
                   createdBulkDeleteOperation = (bulkdeleteoperation)entityCollection.BusinessEntities[0];
    
                   // Check the operation's state.
                   if (createdBulkDeleteOperation.statecode.Value != BulkDeleteOperationState.Completed)
                   {
                      // The operation has not yet completed.  Wait a second for the status to change.
                      System.Threading.Thread.Sleep(1000);
                      secondsTicker--;
    
                      // Retrieve a fresh version of the bulk delete operation.
                      entityCollection = service.RetrieveMultiple(bulkQuery);
                   }
                   else
                   {
                      // Stop polling because the operation's state is now complete.
                      secondsTicker = 0;
                   }
                }
                else
                {
                   // Wait a second for async operation to activate.
                   System.Threading.Thread.Sleep(1000);
                   secondsTicker--;
    
                   // Retrieve the entity again.
                   entityCollection = service.RetrieveMultiple(bulkQuery);
                }
             }
    
             // Validate that the operation was completed.
             if (createdBulkDeleteOperation.statecode.Value != BulkDeleteOperationState.Completed)
             {
                // This will happen if it took longer than the polling time allowed 
                // for this operation to compete.
                success = false;
             }
             else
             {
                    Console.WriteLine("Records successfully deleted: " + createdBulkDeleteOperation.successcount.Value.ToString());
                    Console.WriteLine("Records failed: " + createdBulkDeleteOperation.failurecount.Value.ToString());
                    if (createdBulkDeleteOperation.statuscode.name != "Succeeded")
                    {
                        Console.WriteLine("The Bulk delete operation is still running and might take more time to complete.");
                        Console.WriteLine("Please check status of this operation in CRM system Settings > Data Management.");
                        success = false;
                    }
                    else
                    {
                        Console.WriteLine("The Bulk delete operation completed successfully.");
                        success = true;
                    }
             }
    
             #endregion
    
    

    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://www.mscrm.cn
    2010年2月1日 10:09
    版主