I have an Option set in CRM 2011. It has four options:
Through plugin I want to set the value of this option set. Can anyone provide me the statement to set the value of this option set?
These are all stored in the StringMapBase table.
An option set is a type of field that can be included in an entity. It defines a set of options. When an option set is displayed in a form it uses a drop-down list control.
We use enum to achieve it. public enum Gender { Male = 864630000, Female = 864630001, }; my_optionSet = new OptionSetValue((int)Gender. Male); These are going to be pre-defined key:value pairs that never changes between environments.
How to set optionsetvalue in plugins
In plugins you can write yourEntity.yourAttribute = new OptionSetValue(INDEX);
The INDEX is an int you can look up in your optionset editor (default values are several digit long).
OR
You set the optionset like yourEntity.Attributes.Add(“yourAttribute”, new OptionSetValue(INDEX));
You can set an option set value using the following:-
OptionSetValue myOptionSet = new OptionSetValue();
myOptionSet.Value = xxxx
myEntity.Attributes["optionSetAttributeName"] = myOptionSet;
// Where xxxx represents the value desired and can be checked on the attribute metadata page within the entity customisations
Whether 'myEntity' is actually preImage/postImage or just a dynamically created entity in the plug-in will determine whether you need to actually call the update method, but essentially this is the way you set the option set value and update the attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With