Reputation: 405
Do you know if there is a solution to modify the value (not a label) of an option in optionset by code C#.
for example, I have this Option: (Label: OldLabel, Value: OldValue)
and I want this: (Label: OldLabel, Value: NewValue)
I tried by the following code, but is not working:
UpdateOptionValueRequest updateOptionValueRequest =
new UpdateOptionValueRequest
{
OptionSetName = _globalOptionSetName,
// Update the second option value.
Value = newValue,
Label = new Label("oldLabel", _languageCode)
};
_serviceProxy.Execute(updateOptionValueRequest);
//Publish the OptionSet
PublishXmlRequest pxReq3 = new PublishXmlRequest { ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", _globalOptionSetName) };
_serviceProxy.Execute(pxReq3);
Upvotes: 1
Views: 2629
Reputation: 22846
UpdateOptionValueRequest
is the message to update the Label (Display text) using Key (Integer). You are expecting to update the Integer key using Label, no way.
This will create data inconsistency across the system for existing data vs new data. Even if you manually update like this or delete the option & recreate a new option using SDK, data loss is unavoidable.
If you see the code sample in MSDN, the comments clearly calls out.
#region How to update an option item in a picklist
// In order to change *labels* on option set values (or delete) option set
// values, you must use UpdateOptionValueRequest
// (or DeleteOptionValueRequest).
UpdateOptionValueRequest updateOptionValueRequest =
new UpdateOptionValueRequest
{
OptionSetName = _globalOptionSetName,
// Update the second option value.
Value = optionList[1].Value.Value,
Label = new Label("Updated Option 1", _languageCode)
};
_serviceProxy.Execute(updateOptionValueRequest);
//Publish the OptionSet
PublishXmlRequest pxReq3 = new PublishXmlRequest { ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", _globalOptionSetName) };
_serviceProxy.Execute(pxReq3);
Console.WriteLine("Option Set option *label* changed.");
#endregion How to update an option item in a picklist
Upvotes: 2
Reputation: 3945
As per @Arun Vinoth, this does not appear to be possible via code, with good reason, as it could break the data.
Let's say you have this option set of salutations:
1000 Ms.
1001 Mrs.
1002 Mr.
1003 Dr.
When storing option set values on records, CRM stores only the integer value. So in the Contact's Salutation column will be values like 1000, 1001, etc. If you go in and change Mrs.'s value from 1001 to 2002, the records that had 1001 will still have 1001. When you got to load that Contact, it will try to find 1001, which no longer exists, and you'll get an error.
The master list of option set values gets stored in a table called StringMap that matches the integer value to its labels (which are language dependent). Whenever CRM retrieves an option set column, it looks up the integer value in the stringmap table and returns the label for the active language.
Because of all this, the way to do what you want is to create a new value in the option set, set all records that have the old value to have the new value, then delete the old value from the option set.
What are you really trying to do here?
Upvotes: 1