Development Desk

Just another WordPress.com site

Tag Archives: CRM online

Get Option Set Value by Option Set Text in CRM on-prem /Online

To read crm option set information from the CRM, CRM matadata call need to made and it will bring the information on the based of following input attributes. below input attributes are showing test data.
string entityName = "contact";
string fieldName = "title"; // could be any optionset entity attribute like agegroug etc
string optionSetText = "Mr"
pass these values to given method and it will return optionset value of the selected option set.
GetOptionSetValueByText(entityName , attributeName, attributeValue);

public static int? GetOptionSetValueByText(string entityName, string fieldName, string optionSetText)
 {
 var retrieveAttributeRequest = new RetrieveAttributeRequest
 {
 EntityLogicalName = entityName,
 LogicalName = fieldName,
 RetrieveAsIfPublished = true
 };

 var retrieveAttributeResponse = (RetrieveAttributeResponse)_orgService.Execute(retrieveAttributeRequest);
 var attributeMetadataList = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

 var optionSetInfo = (from optionSetDetail in attributeMetadataList.OptionSet.Options
 from labelInfo in optionSetDetail.Label.LocalizedLabels.Where(label => label.Label == optionSetText).Where(l => optionSetDetail.Value != null)
 select optionSetDetail).ToList();

 return optionSetInfo.Any() && optionSetInfo.Count > 0 && optionSetInfo[0].Value.HasValue ? optionSetInfo[0].Value : null;
 }
Advertisements