Answered by:
getting optionset attribute value in custom workfolw activity crm 2011

Question
-
Hi,
in custom workflow activity I use it to retrieve entities:
string fetchXMl = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='opportunity'>
<attribute name='new_optionsetattribute' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='statuscode' operator='eq' value='1' />
</filter>
</entity>
</fetch>";
EntityCollection ents = service.RetrieveMultiple(new FetchExpression(fetchXMl));
Next in code I try to get, a optionset value label;
foreach (var ent in ents.Entities)
{
if (enti.Attributes.Contains("new_optionsetattribute"))
{
oppValue = ((OptionSetValue)enti["new_optionsetattribute"]).Value;
prospectLevelText = GetOptionsSetTextOnValue(service, "opportunity", "new_optionsetattribute", oppValue);
--------------OR-----------------------
if (enti.FormattedValues["new_optionsetattribute"] != null)
prospectLevelText = enti.FormattedValues["new_optionsetattribute"];
}
}
public string GetOptionsSetTextOnValue(string entityName, string attributeName, int optionSetValue)
{
string optionsetLabel = String.Empty;
try
{
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = entityName
};
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute =>
String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
OptionSetMetadata options = picklistMetadata.OptionSet;
IList<OptionMetadata> OptionsList = (from o in options.Options
where o.Value.Value == optionSetValue
select o).ToList();
if (OptionsList != null && OptionsList.Count != 0)
{
optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
return optionsetLabel;
Unfortunatelly any of this doesent work.
How to get a optionset value label from custom OptionSet attribute in custom workflow activity ?
- Edited by rogus1 Tuesday, January 14, 2014 5:24 PM
Tuesday, January 14, 2014 5:00 PM
Answers
-
Hi,
Check this
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by rogus1 Wednesday, January 15, 2014 7:24 AM
Tuesday, January 14, 2014 5:55 PMModerator -
Hello,
If enti["new_optionsetattribute"] will be null it will throw error something like "the given key not present in the dictionary ...."
you should always check before getting value from attribute like below
if(enti.Contains("new_optionsetattribute"))
{
oppValue = ((OptionSetValue)enti["new_optionsetattribute"]).Value;
}
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by rogus1 Wednesday, January 15, 2014 7:23 AM
Wednesday, January 15, 2014 5:55 AMModerator
All replies
-
Hi,
Check this
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by rogus1 Wednesday, January 15, 2014 7:24 AM
Tuesday, January 14, 2014 5:55 PMModerator -
Are you receiving an error?
Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4netTuesday, January 14, 2014 6:03 PM -
doesent work tooTuesday, January 14, 2014 6:51 PM
-
Is it correct for retrieve OptionSet int value from my entity even if enti["new_optionsetattribute"] is null ?
oppValue = ((OptionSetValue)enti["new_optionsetattribute"]).Value;
- Edited by rogus1 Tuesday, January 14, 2014 9:17 PM
Tuesday, January 14, 2014 9:13 PM -
Hello,
If enti["new_optionsetattribute"] will be null it will throw error something like "the given key not present in the dictionary ...."
you should always check before getting value from attribute like below
if(enti.Contains("new_optionsetattribute"))
{
oppValue = ((OptionSetValue)enti["new_optionsetattribute"]).Value;
}
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Marked as answer by rogus1 Wednesday, January 15, 2014 7:23 AM
Wednesday, January 15, 2014 5:55 AMModerator -
Thanks, everything is ok right now:)Wednesday, January 15, 2014 7:23 AM