Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to retrieve Multi OptionSet Values and set it on a text field.

Posted on by 255

Hi All,

I have a requirement where i have to retrieve Multi OptionSet Values and set it on a text field using c# code.

Below is the code but i am getting error like invalid logic

ColumnSet cs = new ColumnSet("bmw_webmembership");
Entity processes = (Entity)organizationService.Retrieve(targetEntity.LogicalName,targetEntity.Id,cs);


string text = string.Empty;

OptionSetValueCollection webmembership = (OptionSetValueCollection) targetEntity.GetAttributeValue<OptionSetValueCollection>(Contact.Fields.bmw_webmembership);
foreach (OptionSetValue OSV in webmembership)
{
text = text + OSV.Value.ToString() + ",";
}

bmw_log blog = new bmw_log();
blog.bmw_ReasonText = "Entity:" + processes.LogicalName + "Value:" + processes.Attributes["bmw_webmembership"];
organizationService.Create(blog);

Entity contact = new Entity("contact");
contact.Attributes["bmw_webmemberstext"] = text;
organizationService.Update(contact);

Urgent Help is required.

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to retrieve Multi OptionSet Values and set it on a text field.

    Hi Anant,

    Try using the code below using Formatted Values.

    Also, while updating the contact entity, we need to provide the ID of the records to be updated.

                    ColumnSet cs = new ColumnSet("bmw_webmembership");
                    Entity processes = (Entity)organizationService.Retrieve(targetEntity.LogicalName, targetEntity.Id, cs);
                    string text = targetEntity.FormattedValues[Contact.Fields.bmw_webmembership].ToString();
    
                    bmw_log blog = new bmw_log();
                    blog.bmw_ReasonText = "Entity:" + processes.LogicalName + "Value:" + processes.Attributes["bmw_webmembership"];
                    organizationService.Create(blog);
    
                    Entity contact = new Entity("contact", targetEntity.Id);
                    contact.Attributes["bmw_webmemberstext"] = text;
                    organizationService.Update(contact);

    Hope this helps.

  • Suggested answer
    Drew Poggemann Profile Picture
    Drew Poggemann 9,079 on at
    RE: How to retrieve Multi OptionSet Values and set it on a text field.

    Hi Anant,


    I hope the following is close to what you are looking for...  

    QueryExpression queryContact = new QueryExpression();

    queryContact.EntityName = EntityName;  // Entity Name you are searching for (i.e. Contact)
    queryContact.ColumnSet.Columns.Add("bmw_webmembership");

    queryContact.ColumnSet.Columns.Add("contactid");
    queryContact.Criteria.AddCondition("[fieldname]", ConditionOperator.Equal, "[Value to select right Contact in Search]"); 
    EntityCollection ec = _orgService.RetrieveMultiple(queryContact);
    foreach (var item in ec.Entities)  // You may want to check count as I am expecting you only want 1 returned in this list
    {

         String concatenatedValue;
         OptionSetValueCollection op= (OptionSetValueCollection)item["bmw_webmembership"];

         foreach (var options in op)

         {

                int i = options.Value;

               concatenatedValue += i.ToString();  //Note you may need code to add your "commas" :)

         }

         Entity contact = new Entity("contact");
         // Create a column set to define which attributes should be retrieved.
        ColumnSet attributes = new ColumnSet(new string[] { "name", "ownerid" });

        // Retrieve the contact and its name and ownerid attributes.
        contact = _orgService.Retrieve(contact.LogicalName, _contactid, attributes);

        contact["bmw_webmemberstext"] = concatentatedValue;
        // Update the contact.
        _orgService.Update(contact);
    }

    Hope this helps.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to retrieve Multi OptionSet Values and set it on a text field.

    Hello,

    Try to check FomattedValues like I did it here - github.com/.../GetFieldLabel.cs

  • Anant Profile Picture
    Anant 255 on at
    RE: How to retrieve Multi OptionSet Values and set it on a text field.

    Yes. i reviewd it. Instead of getting Text , i am getting Values.

  • Suggested answer
    Drew Poggemann Profile Picture
    Drew Poggemann 9,079 on at
    RE: How to retrieve Multi OptionSet Values and set it on a text field.

    Hi Anant,

    Have you reviewed the following community.dynamics.com/.../282806

    This provides details to do what you need here I think.

    Hope this helps.

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