Sam
Sam

Reputation: 149

How do I check the statuscode of incident entity using an if statement?

How do I check the statuscode of a incident entity, if the statuscode is 1 or 3 then the if method executes. The statuscode is an optionset value so I am unsure how to pass it in an if statement.

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()

Upvotes: 2

Views: 1372

Answers (2)

Gauravsa
Gauravsa

Reputation: 6514

You can do this:

OptionSetValue localTempVariable;

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

This way you can check for null and then access the localTempVariable.Value (which is an integer). Like below:

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

More here: OptionSetValue behaving like an integer

Upvotes: 1

OptionSetValue is integer. This should work.

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

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

}

Upvotes: 2

Related Questions