private void BatchCreateBulkDeleteJobs(List <BulkDeleteRequest> items, Action completedCallback) { DelegateItterator.CallbackItterate(delegate(int index, Action nextCallback, ErrorCallBack errorCallBack) { BulkDeleteRequest pendingDeleteItem = items[index]; OrganizationServiceProxy.BeginExecute(pendingDeleteItem, delegate(object result) { try { OrganizationServiceProxy.EndExecute(result); IsBusyProgress.SetValue((index / items.Count) * 100); nextCallback(); } catch (Exception ex) { errorCallBack(ex); } }); }, items.Count, completedCallback, delegate(Exception ex) { // Error ReportError(ex); }); }
public static void RunWorkflow(string name, string entityId, string completeCallback, string errorCallback) { // Get the name of the workflow string fetch = String.Format(@"<fetch count='1'> <entity name='workflow'> <attribute name='workflowid'/> <filter type='and'> <condition attribute='name' operator='eq' value='{0}'/> <condition attribute='ondemand' operator='eq' value='true'/> <condition attribute='statuscode' operator='eq' value='2'/> <condition attribute='type' operator='eq' value='1'/> </filter> </entity> </fetch>", name); OrganizationServiceProxy.BeginRetrieveMultiple(fetch, delegate(object state) { EntityCollection results = OrganizationServiceProxy.EndRetrieveMultiple(state, typeof(Entity)); if (results.Entities.Count == 0) { Page.Ui.SetFormNotification("Workflow " + name + " is not published", FormNotificationLevel.Error, "RibbonWorkflowError"); } bool isError = false; foreach (Entity row in results.Entities) { // Run Workflow ExecuteWorkflowRequest request = new ExecuteWorkflowRequest(); request.EntityId = entityId.Replace("{", "").Replace("}", ""); request.WorkflowId = row.GetAttributeValueString("workflowid"); OrganizationServiceProxy.BeginExecute(request, delegate(object executeState) { try { ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)OrganizationServiceProxy.EndExecute(executeState); if (completeCallback != null) { if (response.Id == Guid.Empty.Value) { Script.Eval(completeCallback); } else { // Query until completed WaitForWorkflowToComplete(response.Id, completeCallback, errorCallback, null); } } } catch (Exception e) { string stackTrace = e.StackTrace; Script.Literal("console.log(stackTrace)"); Script.Eval(errorCallback); } }); break; } }); }
private void CreateBulkDeleteJobs(ScheduledJob job) { IsBusyMessage.SetValue("Creating new schedule..."); IsBusyProgress.SetValue(0); // Convert bulk delete fetch into QueryExpression string fetchxml = "<fetch distinct='false' no-lock='false' mapping='logical'><entity name='lead'><attribute name='fullname' /><attribute name='statuscode' /><attribute name='createdon' /><attribute name='subject' /><attribute name='leadid' /><filter type='and'><condition attribute='ownerid' operator='eq-userid' /><condition attribute='statecode' operator='eq' value='0' /><condition attribute='address1_county' operator='eq' value='deleteme' /></filter><order attribute='createdon' descending='true' /></entity></fetch>"; FetchXmlToQueryExpressionRequest convertRequest = new FetchXmlToQueryExpressionRequest(); convertRequest.FetchXml = fetchxml; OrganizationServiceProxy.BeginExecute(convertRequest, delegate(object state) { FetchXmlToQueryExpressionResponse response = (FetchXmlToQueryExpressionResponse)OrganizationServiceProxy.EndExecute(state); List <BulkDeleteRequest> bulkDeleteRequests = new List <BulkDeleteRequest>(); // If the recurrance is minutely, hourly, weekly we need to schedule multiple jobs if (job.Recurrance.GetValue().Value != RecurranceFrequencyNames.DAILY) { DateTime startDate = DateTimeEx.UTCToLocalTimeFromSettings(job.StartDate.GetValue(), OrganizationServiceProxy.GetUserSettings()); DateInterval interval = DateInterval.Days; int incrementAmount = 1; int dayInterval = 1; int recurranceCount = 0; int totalCount = 0; string freq = RecurranceFrequencyNames.DAILY; switch (job.Recurrance.GetValue().Value) { case RecurranceFrequencyNames.MINUTELY: interval = DateInterval.Minutes; incrementAmount = job.RecurEvery.GetValue(); dayInterval = 1; recurranceCount = (60 * 24) / incrementAmount; break; case RecurranceFrequencyNames.HOURLY: interval = DateInterval.Hours; incrementAmount = job.RecurEvery.GetValue(); dayInterval = 1; recurranceCount = 24 / incrementAmount; break; case RecurranceFrequencyNames.WEEKLY: case RecurranceFrequencyNames.YEARLY: // To schedule weekly, me must create a job per week day for the whole year, and set recurrance to every 365 days // but this doesn't deal with leap years, so we can't do it! throw new Exception("The selected schedule interval is currently not supported due to the limitation of bulk delete"); } if (incrementAmount < 0) { throw new Exception("Invalid schedule"); } // Increment in the recurrency frequence for (int i = 0; i < recurranceCount; i++) { BulkDeleteRequest request = new BulkDeleteRequest(); request.QuerySet = response.Query.Replace(@"<d:anyType ", @"<d:anyType xmlns:e=""http://www.w3.org/2001/XMLSchema"" "); request.SendEmailNotification = false; request.StartDateTime = startDate; request.RecurrencePattern = "FREQ=DAILY;INTERVAL=" + dayInterval.ToString(); request.JobName = "Scheduled Job " + i.Format("0000") + " " + job.ScheduledJobId.GetValue().Value; ArrayEx.Add(bulkDeleteRequests, request); startDate = DateTimeEx.DateAdd(interval, incrementAmount, startDate); } } else { // Just a single request BulkDeleteRequest request = new BulkDeleteRequest(); request.QuerySet = response.Query.Replace(@"<d:anyType ", @"<d:anyType xmlns:e=""http://www.w3.org/2001/XMLSchema"" "); request.SendEmailNotification = false; request.StartDateTime = job.StartDate.GetValue(); request.RecurrencePattern = RecurrancePatternMapper.Serialise(job); request.JobName = "Scheduled Job " + job.ScheduledJobId.GetValue().Value; ArrayEx.Add(bulkDeleteRequests, request); } BatchCreateBulkDeleteJobs(bulkDeleteRequests, delegate() { IsBusy.SetValue(false); JobsViewModel.Reset(); JobsViewModel.Refresh(); }); }); }