Obtaining the value of an option in a picklist

时光毁灭记忆、已成空白 提交于 2019-12-12 04:43:11

问题


I'm creating a phony instance of an entity beep. It has a required field of type picklist called pickaboo. First I omitted it but then the application started to lament throwing error messages at me due to some business logic demanding all the newly created instances of beep to have that field assigned.

Entity entity = new Entity { LogicalName = "beep" };
Guid guid = proxy.Create(entity);
proxy.Delete("beep", guid);

I don't give a rodent's tail section about that demand because right after I've created the instance, I'm removing it. However CRM gives a huge rodent and doesn't let me do my magic. So, I went clever and added an attribute for the missing attribute.

OptionSetValue option = new OptionSetValue(0);
Entity entity = new Entity { LogicalName = "beep" };
entity.Attributes.Add("pickaboo", option);
Guid guid = proxy.Create(entity);
proxy.Delete("beep", guid);

Of course, it didn't work because zero isn't a valid value. Apparently, CRM adds a hash number based on the solution so the actual "zero" has the numeric value like "846000000000", the actual "one" has "846000000001" etc.

How can I obtain that value programmatically?

Right now I have an ugly workaround obtaining all the beeps and getting the value from the first of them. Don't even get me started on how much sleep I'm loosing knowing how embarrassing it looks, would anybody take time to give me some feed-back. :(


回答1:


You've got two options.

  1. You can use the CrmSrvcUtil to generate your OptionSetValues as enums... This would create a pickaboo enum that you could then reference in your code entity.Attributes.Add("pickaboo", new OptionSetValue((int)pickaboo.YourEnumValue);

  2. You could also use the RetrieveOptionSetRequest message to get a list of all values for the particular option set you're interested in. See this SO question




回答2:


Since I know that all CRM programmers are lazy pigs (own experience, haha), I know that you'd prefer a short and comprehensive solution. I realize that you're looking for a quick access to just a single valid value. If I'm mistaken, stop reading - use the suggestion of @Daryl - he's got a good answer for ya.

If I'm right, though, use this code to get the first valid option value (provided it exists). Just in case, remember to surround it with try/catch so if you misspell or such, you won't end up scratching your head.

RetrieveAttributeRequest request = new RetrieveAttributeRequest
{
  EntityLogicalName = "beep",
  LogicalName = "pickaboo",
  RetrieveAsIfPublished = true
};
RetrieveAttributeResponse response 
  = proxy.Execute(request) as RetrieveAttributeResponse;
PicklistAttributeMetadata metaData 
  = response.AttributeMetadata as PicklistAttributeMetadata;
OptionSetValue option 
  = new OptionSetValue(metaData.OptionSet.Options[0].Value ?? -1);
  1. NB - I'm assuming that you've got a working connection via a proxy called proxy.
  2. Set a try/catch around the whole code, just in case out of bound exception occurs.
  3. Make sure to handle the option of -1, since Value returned is a nullable integer.



回答3:


This question has inspired me to blog.

If you have a look at my post below its got a function that can be used to find the int values of picklists (global and local optionsets), statecode, statuscode and the boolean (two option) fields.

CRM 2011 Programatically Finding the Values of Picklists, Optionsets, Statecode, Statuscode and Boolean (Two Options)



来源:https://stackoverflow.com/questions/13309869/obtaining-the-value-of-an-option-in-a-picklist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!