Bulk delete records in dynamics crm

  • Digg
  • Facebook
  • LinkedIn
  • Twitter
 

To bulk delete records in crm you need to use the BulkDeleteRequest/Response. One of the parameters in there is the QuerySet. It allows you to set multiple conditions for deleting a set of records. QuerySet takes in an array of QueryExpressions but writing QueryExpressions are not that fun, so we’ve added functionality into XrmLinq to make this process easier.

The simplified process is to write your linq query (query to delete a bunch of records), then call the Execute method on the CrmDataContext with a BulkDeleteRequest.

// query specifying which accounts to bulk delete
var query = (from a in context.Accounts
             // delete all accounts starting with ad
             where a.Name.StartsWith("ad")
             select a);
 
// get the query expression from the above query
var queryExpression = query.ToQueryExpression();
 
// execute a bulk delete request
var response = context.Execute<BulkDeleteResponse>(new BulkDeleteRequest
{
    JobName = "delete accounts starting with 'ad'",
    QuerySet = new QueryBase[] { queryExpression },
    StartDateTime = new CrmDateTime(DateTime.Now.ToString("s")),
    SendEmailNotification = false,
    ToRecipients = new Guid[] { },
    CCRecipients = new Guid[] { },
    RecurrencePattern = ""
});
 
Guid jobId = response.JobId;
Console.WriteLine("delete job queued: {0}", jobId);

No Feedback

No feedback yet.

RSS feed for feedback on this post. TrackBack URI

Leave your feedback

© XrmLinq 2009.