我在CRM 2011内部服务器上有一个非常标准的工作流程设置。有时,在运行任何工作流程步骤之前,工作流程会以等待状态停止。工作流包含对自定义工作流活动的一些调用,但执行似乎没有调用它们。我在下面列出了一些屏幕截图。感谢。
编辑:以下是所请求的工作流活动系数。
public sealed class HandleInspectedIssues : CodeActivity
{
#region Public Properties
[RequiredArgument]
[Input("Inspection")]
[ReferenceTarget("appointment")]
public InArgument<EntityReference> Inspection { get; set; }
[Output("Result")]
public OutArgument<Boolean> Result { get; set; }
[Output("Errors")]
public OutArgument<String> Errors { get; set; }
#endregion
protected override void Execute(CodeActivityContext executionContext)
{
#region Workflow Context Setup
ITracingService tracingService = executionContext.GetExtension<ITracingService>();
if (tracingService == null)
throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
tracingService.Trace("Entered HandleInspectedIssues.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
executionContext.ActivityInstanceId,
executionContext.WorkflowInstanceId);
// Create the context
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
if (context == null)
throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
tracingService.Trace("HandleInspectedIssues.Execute(), Correlation Id: {0}, Initiating User: {1}",
context.CorrelationId,
context.InitiatingUserId);
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Errors.Set(executionContext, String.Empty);
Result.Set(executionContext, false);
#endregion
StringBuilder errors = new StringBuilder();
Entity inspection = null;
tracingService.Trace("Retrieving Inspection Appointment Record: {0}", DateTime.Now);
try
{
inspection = service.Retrieve("appointment", Inspection.Get(executionContext).Id, new ColumnSet(true));
}
catch (FaultException<OrganizationServiceFault> e)
{
tracingService.Trace("Exception: {0}", e.ToString());
// Handle the exception.
throw;
}
Int32 inspectionState = Int32.MinValue;
Int32 inspectionStatus = Int32.MinValue;
if (inspection.Contains("statecode") && inspection.Attributes["statecode"].GetType() == typeof(OptionSetValue))
{
tracingService.Trace("{0}", inspection.FormattedValues["statecode"].ToString());
inspectionState = ((OptionSetValue)inspection.Attributes["statecode"]).Value;
}
if (inspection.Contains("statuscode") && inspection.Attributes["statuscode"].GetType() == typeof(OptionSetValue))
{
tracingService.Trace("{0}", inspection.FormattedValues["statuscode"].ToString());
inspectionStatus = ((OptionSetValue)inspection.Attributes["statuscode"]).Value;
}
//Stop processing if the StateCode is not Complete (1) and StatusCode is not Complete (3)
if (inspectionState != 1 && inspectionStatus != 3)
throw new Exception("You cannot mark the issues as inspected before the inspection is complete.");
#region Fetch Query
string fetchIssues = String.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='ipm_issue'>
<attribute name='statecode' />
<attribute name='statuscode' />
<attribute name='ipm_issueid' />
<link-entity name='anhwp_appointmentissue' from='anhwp_issue' to='ipm_issueid' alias='ac'>
<link-entity name='appointment' from='activityid' to='anhwp_appointment' alias='ad'>
<filter type='and'>
<condition attribute='activityid' operator='eq' uitype='appointment' value='{0}' />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>", inspection.Id);
#endregion
tracingService.Trace("Retrieving ipm issue records: {0}", DateTime.Now);
EntityCollection result = null;
try
{
result = service.RetrieveMultiple(new FetchExpression(fetchIssues));
}
catch (FaultException<OrganizationServiceFault> e)
{
tracingService.Trace("Exception: {0}", e.ToString());
// Handle the exception.
throw;
}
tracingService.Trace("Setting ipm issue status to inspected: {0}", DateTime.Now);
foreach (Entity entity in result.Entities)
{
var issueState = entity.Contains("statecode") ? ((OptionSetValue)entity.Attributes["statecode"]).Value : default(Int32);
var issueStatus = entity.Contains("statuscode") ? ((OptionSetValue)entity.Attributes["statuscode"]).Value : default(Int32);
//if the issue is active(0), set status reason to inspected(755390001)
if (issueState == 0 && issueStatus != 755390001)
{
errors.AppendFormat("The issue {0} has already been inspected and cannot be inspected again.", entity.Attributes["bdo_issuenumber"].ToString());
continue;
}
var entityToUpdate = new Entity("ipm_issue");
entityToUpdate["id"] = entity.Id;
entityToUpdate["statuscode"] = new OptionSetValue(755390001);
tracingService.Trace("Updating ipm issue records: {0}", DateTime.Now);
service.Update(entityToUpdate);
}
if (errors.Length > 0)
{
Errors.Set(executionContext, errors.ToString());
Result.Set(executionContext, false);
return;
}
Errors.Set(executionContext, String.Empty);
Result.Set(executionContext, true);
tracingService.Trace("Exiting HandleInspectedIssues.Execute(), Correlation Id: {0}", context.CorrelationId);
}
}