C# · Debugging · Dynamics CRM 2011 · Workflow

CRM 2011: Cancel Waiting System Jobs Programmatically

Code below to cancel waiting System Jobs programmatically.

var job = CrmContext.AsyncOperationSet.First(a => a.StatusCode.Value == 10); //First waiting job

job.StateCode = AsyncOperationState.Completed;
job.StatusCode = new Microsoft.Xrm.Sdk.OptionSetValue(32);
			
CrmContext.UpdateObject(job);
CrmContext.SaveChanges();

LINQPad C# Statement to cancel all waiting system jobs.

var stillHave = true;

while (stillHave)
{
	var jobs = AsyncOperationSet.Where(a => a.StatusCode.Value == 10)
				    .Take(100)
				    .ToList();
	
	stillHave = jobs.Any();
	
	if (stillHave)
	{
		foreach (AsyncOperation job in jobs)
		{
			job.StateCode = AsyncOperationState.Completed;
			job.StatusCode = new Microsoft.Xrm.Sdk.OptionSetValue(32);
			
			UpdateObject(job);		
		}
		SaveChanges();
	}
}

Cheers – Sy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s