Asked by:
Updated CRM 2013 On premise breaking plugin code

Question
-
Hi,
I have CRM 2013 on premise environment. Today i started installing the rollup's. It went fine when i installed UR1, UR2 and UR3 and my plugin executions is working as expected. After i installed SP1 and UR1 for SP1, my plugin code is breaking.
In my plugin i had code to retrieve option set text value. The below is the code
private static string GetOptionsSetTextForValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
{
try
{
if (service != null)
{
string AttributeName = attributeName;
string EntityLogicalName = entityName;
RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = EntityLogicalName
};
RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
IList<OptionMetadata> OptionsList = (from o in options.Options
where o.Value.Value == selectedValue
select o).ToList();
string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Label;
return optionsetLabel;
}
}
catch (Exception ex)
{
CustomServiceManagementPortalException customEx = new CustomServiceManagementPortalException(string.Format(CultureInfo.InvariantCulture, "Fetching option set label for fieldname {0}, value {1} and entity {2} failed.", attributeName, selectedValue, entityName), ex);
throw customEx;
}
return string.Empty;
}In this function my code is failing with error "An unexpected error occured.
I am not able to figure out what went wrong. Can anyone suggest me some solution.
Regards,
Kishan.
Tuesday, December 2, 2014 1:29 PM
All replies
-
Hi,
It's sort of hard saying anything with that error, do you have to option to enable tracing and post the message there instead?
Regards
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.seTuesday, December 2, 2014 1:54 PM -
Personally I don't like this type of constructions.
IList<OptionMetadata> OptionsList = (from o in options.Options
where o.Value.Value == selectedValue
select o).ToList();
string optionsetLabel = (OptionsList.First()).Label.UserLocalizedLabel.Labelwith the OptionsList.First() you assume that there is always a list containing data. This is a wrong assumption and this might be causing the exception. Furthermore (OptionsList.First()).Label could result in a null value
Tuesday, December 2, 2014 3:29 PM -
The UserLocalizedLabel value can differ based on the context in which the plugin runs. This is a possible source of the error. Under what user context is the plugin step registered ?
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Tuesday, December 2, 2014 4:42 PMModerator