Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 79901

Can't put multiselect values into string

$
0
0

Hello, I have 3 custom multiselectoption in opportunity.

I'm trying to put selected values into string

It works only if I edit all multiselectoptions; otherwise, even if a value in multiselect option is flagged, it string become empty

Anyone can help me?

public string getSelectedOptionSetText(IOrganizationService service, Entity enRecord, string fieldName, ITracingService tracingService) { string selectedText = string.Empty; // Get the Formatted Values of MultiSelect OptionSet var multiSelectTextCollection = GetOptionSetTextCollection(service, enRecord.LogicalName, fieldName); if (enRecord.Contains(fieldName)) { OptionSetValueCollection multiSelectValueCollection = new OptionSetValueCollection(); try { if (((OptionSetValueCollection)enRecord[fieldName]).Count == 0) return ""; } catch (Exception) { return ""; } multiSelectValueCollection = (OptionSetValueCollection)enRecord[fieldName]; foreach (var relationshipType in multiSelectValueCollection) { if (selectedText.Equals(string.Empty)) { selectedText = multiSelectTextCollection.Where(x => x.Value == relationshipType.Value).FirstOrDefault().Label.UserLocalizedLabel.Label; } else { selectedText = selectedText + ", " + multiSelectTextCollection.Where(x => x.Value == relationshipType.Value).FirstOrDefault().Label.UserLocalizedLabel.Label; } } } return selectedText; } public static List GetOptionSetTextCollection(IOrganizationService service, string entityName, string fieldName) { var retrieveAttributeRequest = new RetrieveAttributeRequest() { EntityLogicalName = entityName, LogicalName = fieldName, RetrieveAsIfPublished = true }; var retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest); var attMetadata = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata; return attMetadata.OptionSet.Options.ToList(); } public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); if (context.Depth == 1 && context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity)context.InputParameters["Target"]; string messageName = context.MessageName; if (entity.LogicalName == "opportunity" && (messageName == "Create" || messageName == "Update")) { try { String new_strsegment = getSelectedOptionSetText(service, entity, "new_segment", tracingService); entity.Attributes["new_strsegment"] = new_strsegment; } catch (Exception exc0) { throw new InvalidPluginExecutionException(exc0.Message.ToString()); } try { String new_strvesseltype = getSelectedOptionSetText(service, entity, "new___vessel_type", tracingService); entity.Attributes["new_strvesseltype"] = new_strvesseltype; } catch (Exception exc1) { throw new InvalidPluginExecutionException(exc1.Message.ToString()); } try { String new_strcompetitors = getSelectedOptionSetText(service, entity, "new_competitors", tracingService); entity.Attributes["new_strcompetitor"] = new_strcompetitors; } catch (Exception exc2) { throw new InvalidPluginExecutionException(exc2.Message.ToString()); } service.Update(entity); } } } }


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>