Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Deselecting all elements from a multi select optionset field using plugin code

(0) ShareShare
ReportReport
Posted on by 402

i am trying to update multioptionset field to deselect all values. i am updating it using OptionsetValueCollection successfully but when OptionsetValueCollection has 0 items, update is throwing an error "input string not in a correct format"

Here is the code:

OptionSetValueCollection values = new OptionSetValueCollection();

foreach (var item in list)
{
values.Add(new OptionSetValue(Convert.ToInt32(item)));
}

Entity customEntity= new Entity("new_customentity");
customEntity.Id = context.PrimaryEntityId;
customEntity["multiselectoptionsetfieldname"] = values;
service.Update(customEntity);

Error : 

Exception Details Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): Workflowassemblyname.Workflows.CopyToMultiSelectValues: System.FormatException: Input string was not in a correct format.Detail: <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts"> <ActivityId>affb334b-2b57-4bfd-bd79-e08c05b15b81</ActivityId> <ErrorCode>-2147220956</ErrorCode> <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>Unexpected exception from plug-in (Execute): ImprovementFoundation.CommissioningSystem.Workflows.CopyToMultiSelectValues: System.FormatException: Input string was not in a correct format.</Message> <Timestamp>2018-08-16T05:23:12.5238074Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource>PluginExecution</ExceptionSource> <InnerFault i:nil="true" /> <OriginalException>System.FormatException at System.Activities.WorkflowApplication.Invoke(Activity activity, IDictionary`2 inputs, WorkflowInstanceExtensionManager extensions, TimeSpan timeout) at System.Activities.WorkflowInvoker.Invoke(Activity workflow, IDictionary`2 inputs, TimeSpan timeout, WorkflowInstanceExtensionManager extensions) at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, String customActivityTypeName, IExecutionContext requestContext, Dictionary`2 sandboxServices, Boolean useDrawbridgeEnabled, Boolean chaosFailAppDomain) at Microsoft.Crm.Sandbox.SandboxAppDomainHelper.Execute(IOrganizationServiceFactory organizationServiceFactory, String customActivityTypeName, IExecutionContext requestContext, Dictionary`2 sandboxServices, Boolean useDrawbridgeEnabled, Boolean chaosFailAppDomain) at Microsoft.Crm.Sandbox.SandboxWorker.ExecuteCustomWorkflowActivity(SandboxCallInfo callInfo, SandboxCustomActivityExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, SandboxRequestCounter&amp; workerCounter, Boolean returnTraceInfo)</OriginalException> <TraceText> inside execute. Multi select text values. comma seperated text values. Multi select field name.if_specifyreasonspatientnonattendencemulti update outcome report </TraceText> </OrganizationServiceFault> Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Crm.Sandbox.ISandboxHost.ExecuteCustomWorkflowActivityAndReturnTraceInfo(SandboxCallInfo callInfo, SandboxCustomActivityExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, String assemblyContents, Boolean returnTraceInfo) at Microsoft.Crm.Sandbox.SandboxCustomActivity.Execute(SandboxClient client, SandboxCallTracker callTracker, IExecutionContext requestContext, String assemblyContents, Boolean returnTraceInfo) at Microsoft.Crm.Sandbox.SandboxCodeUnit.ExecuteInternal(SandboxClient client, IExecutionContext context, SandboxTraceContext sandboxTraceContext, SandboxCallTracker callTracker, Guid parentExecutionId, String assemblyContents, Boolean& isSafeToRetry, Boolean& executeDone) at Microsoft.Crm.Sandbox.SandboxCodeUnit.<>c__DisplayClass24_0.<Execute>b__0() Exception Details-Details of the exception.
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): ImprovementFoundation.CommissioningSystem.Workflows.CopyToMultiSelectValues: System.FormatException: Input string was not in a correct format.Detail: 

*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Deselecting all elements from a multi select optionset field using plugin code

    Hi ,

    Your code is correct , just  you need to make sure in the list you have valid integer data. Seems its unable to convert to integer .

    foreach (var item in list)
    {
    values.Add(new OptionSetValue(Convert.ToInt32(item)));
    }

  • Suggested answer
    Ben Thompson Profile Picture
    6,350 on at
    RE: Deselecting all elements from a multi select optionset field using plugin code

    You need to add a check to ensure the option set collection contains at least 1 value. If the collection is empty you should be setting the field to be null.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans