Reputation: 351
I was trying to use an optionset input parameter in custom workflow activity, so I have used the below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using System.Globalization;
namespace ITWorx.VisitManagement.Workflows
{
public class FormatDate : CodeActivity
{
[Input("Preferred Language")]
[AttributeTarget("contact", "itworx_preferredlanguage")]
[Default("894330000")]
public InArgument<OptionSetValue> PreferredLanguage { get; set; }
protected override void Execute(CodeActivityContext context)
{
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
int preferredLanguage = PreferredLanguage.Get<OptionSetValue>(context).Value;
}
}
}
But an exception is thrown in the below line:
int preferredLanguage = PreferredLanguage.Get<OptionSetValue>(context).Value;
Exception: NullReferenceException {"Object reference not set to an instance of an object."}
Please advise
Upvotes: 1
Views: 11646
Reputation: 7234
The Default("894330000")
attribute does not generate a Default value in code, it only tells the Workflow designer what to suggest as a Value. When you set the Value to a Dynamic Value, based on your entity record (which I assume you are doing, you must set the Default Value at that time. This is what it should look like in your Workflow editor:
You can also modify your code to ensure that if a null
value does get passed it will use your desired default:
int preferredLanguage = PreferredLanguage.Get<OptionSetValue>(context) != null
? PreferredLanguage.Get<OptionSetValue>(context).Value
: 894330001;
Upvotes: 7
Reputation: 7948
PreferredLanguage.Get<OptionSetValue>(context)
yields an OptionSetValue
object.
OptionSetValue
is a class and can therefore be null.
My best guess: field itworx_preferredlanguage is null for the given record. Trying to get its .Value property throws the NullReferenceException
.
Upvotes: 0