Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

how to set Multi Option Set value based on Value written in a Text Field and vice- versa

Posted on by 255

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

  • Suggested answer
    Flydancer Profile Picture
    Flydancer 1,332 on at
    RE: how to set Multi Option Set value based on Value written in a Text Field and vice- versa

    You have to identify the values of the optionset (split your text, then search in the metadata if there's a optionsetvalue with this label, access it by using .Value property)

    As soon as you know the values, you can add them by using a OptionSetValueCollection:

    OptionSetValueCollection activities = new OptionSetValueCollection();
    activities.Add(new OptionSetValue(1)); //Swimming
    activities.Add(new OptionSetValue(9)); //Camping
    
    contact["sample_outdooractivities"] = activities;


    Does this help solving your Problem?

  • Nagaraj M Profile Picture
    Nagaraj M 161 on at
    RE: how to set Multi Option Set value based on Value written in a Text Field and vice- versa

    Hi anant,

    As i know about multi optionsets the text formatted values will come in this below format.

    For Eg: if you have a multioptionset field for to select City , and if you retrieve text then you will get it like     hyderabad;banglore;delhi   with semi colon separated values. so if you want to set the these values using your text field then you need to pass in that semicolon format mostly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans