如何使用if语句检查事件实体的状态码?

时间:2018-08-23 14:10:06

标签: c# dynamics-crm dynamics-crm-online dynamics-crm-365

如何检查事件实体的状态码,如果状态码为1或3,则执行if方法。状态码是一个选项集值,所以我不确定如何在if语句中传递它。

public void Execute(IServiceProvider serviceProvider)
{
    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    IOrganizationServiceFactory factory =
        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = factory.CreateOrganizationService(context.UserId);

    //create an entity
    Entity entity = (Entity)context.InputParameters["Target"];

    //after creating the entity, we need to retrieve the required entity: Incident

    //retrieve incident entity
    Incident detail = entity.ToEntity<Incident>();


  // var incident = service.Retrieve("incident", detail.IncidentId.Value, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)).ToEntity<Incident>();


    if (detail.StatusCode== new OptionSetValue(1) || detail.StatusCode == new OptionSetValue(3))
    {
        if (sec != null)
        {
            ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()

2 个答案:

答案 0 :(得分:2)

OptionSetValue是整数。这应该起作用。

int statusCode = detail.GetAttributeValue<OptionSetValue>("statuscode").Value;

if(statusCode == 1 || statusCode == 3)
{

}

答案 1 :(得分:1)

您可以这样做:

OptionSetValue localTempVariable;

localTempVariable = new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute");

通过这种方式,您可以检查 null ,然后访问localTempVariable.Value(整数)。如下所示:

localTempVariable = new_myEntity.GetAttributeValue<OptionSetValue>("new_myOptionSetAttribute") == null ? 

更多内容:OptionSetValue behaving like an integer