Hi All,
I have a requirement where i have to set Multi Option Set value based on Value written in a Text Field and set that Text Field Area based on selected Multi Option Set Values.
Heer is my code where i have set the text field area based on selected Multi Option set values. But unable to set the Multi Option sets based on Text.
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity targetEntity = (Entity)context.InputParameters["Target"];
ColumnSet cs = new ColumnSet("new_webmembership","new_description");
Entity processes = (Entity)service.Retrieve(targetEntity.LogicalName, targetEntity.Id, cs);
string text = string.Empty;
OptionSetValueCollection webmembership = (OptionSetValueCollection)processes["new_webmembership"];
if (processes.Attributes.Contains("new_description"))
{
}
foreach (OptionSetValue s in webmembership)
{
text += GetOptionsSetTextOnValue(service, "new_auditlog", "new_webmembership", s.Value) + ", ";
}
Entity contact = new Entity("new_auditlog");
contact.Attributes["new_name"] = "Multi";
contact.Attributes["new_description"] = text;
service.Create(contact);
}
private string GetOptionsSetTextOnValue(IOrganizationService service, string entityName, string attributeName, int selectedValue)
{
RetrieveAttributeRequest retrieveAttributeRequest = new
RetrieveAttributeRequest
{
EntityLogicalName = entityName,
LogicalName = attributeName,
RetrieveAsIfPublished = true
};
// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
// Access the retrieved attribute.
MultiSelectPicklistAttributeMetadata retrievedPicklistAttributeMetadata = (MultiSelectPicklistAttributeMetadata)
retrieveAttributeResponse.AttributeMetadata;// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
string selectedOptionLabel = string.Empty;
foreach (OptionMetadata oMD in optionList)
{
if (oMD.Value == selectedValue)
{
selectedOptionLabel = oMD.Label.UserLocalizedLabel.Label;
}
}
return selectedOptionLabel;
}
}
}
Urgent help is required.
*This post is locked for comments