public void WorkflowTaskMonitorTurnOnTest()
        {
            var workflowTask = InitialiseValidWorkflowTask();

            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_targetworkflow, GetTargetAccountWorkflow());
            //queues required when sending
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, null);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, null);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures, true);
            try
            {
                workflowTask = CreateAndRetrieve(workflowTask);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsFalse(ex is AssertFailedException);
            }

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures, false);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationforschedulefailures, true);
            try
            {
                workflowTask = CreateAndRetrieve(workflowTask);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsFalse(ex is AssertFailedException);
            }

            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, TestQueue);
            try
            {
                workflowTask = CreateAndRetrieve(workflowTask);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsFalse(ex is AssertFailedException);
            }
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures, true);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TestQueue);
            workflowTask = CreateAndRetrieve(workflowTask);
            Assert.IsNotNull(workflowTask.GetField(Fields.jmcg_workflowtask_.jmcg_minimumtargetfailuredatetime));
            Assert.IsNotNull(workflowTask.GetField(Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime));

            //okay need to set conditions for the target having not executed
            WaitTillTrue(() => WorkflowSchedulerService.GetMonitorInstances(workflowTask.Id).Any(), 60);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_on, false);
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_on);
            Assert.IsFalse(WorkflowSchedulerService.GetMonitorInstances(workflowTask.Id).Any());

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_on, true);
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_on);
            Assert.IsTrue(WorkflowSchedulerService.GetMonitorInstances(workflowTask.Id).Any());
        }
        public void WorkflowTaskMonitorOnlyTest()
        {
            var workflowName = "Test Account Target Failure";
            var taskName     = "Test Monitor Only";

            DeleteWorkflowTasks(taskName);

            var workflowWillFail = GetWorkflow(workflowName);
            //create the workflow task montior only
            var workflowTask = new Entity(Entities.jmcg_workflowtask);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_name, taskName);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_notesfortargetfailureemail, "These are some fake instructions\nto fix the problem");
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_targetworkflow, workflowWillFail);
            workflowTask.SetOptionSetField(Fields.jmcg_workflowtask_.jmcg_workflowexecutiontype,
                                           OptionSets.WorkflowTask.WorkflowExecutionType.MonitorOnly);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, TestQueue);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TestQueue);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_on, true);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_crmbaseurl, "jmcg_wstestsettings.jmcg_crminstanceurl");
            workflowTask = CreateAndRetrieve(workflowTask);

            //wait a second and verify only the montior workflow spawned
            //i.e this doesn't have a continuous workflow as it doesn't
            //run a process on a schedule it just monitors
            Thread.Sleep(5000);
            Assert.IsFalse(WorkflowSchedulerService.GetRecurringInstances(workflowTask.Id).Any());
            Assert.IsTrue(WorkflowSchedulerService.GetMonitorInstances(workflowTask.Id).Any());
            //stop the monitor we will start it again in a second
            WorkflowSchedulerService.StopMonitorWorkflowFor(workflowTask.Id);

            //run the workflow which will fail
            var account = CreateAccount();

            XrmService.StartWorkflow(workflowWillFail.Id, account.Id);
            //wait a second and spawn the monitor
            Thread.Sleep(5000);
            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);
            //verify the notification email created
            WaitTillTrue(() => GetRegardingEmails(workflowTask).Any(), 60);

            DeleteMyToday();
        }