locked
display selected optionset value in a text field on update of case RRS feed

  • Question

  • Hi All,

    Could any one please help me in displaying an optionset field value in a text field..? I want to retrieve the value selected in optionset and display the same in a text field using plugin.. Iam writing this plugin on "update" of "case' entity...

    Tuesday, April 29, 2014 5:58 AM

Answers

  • Hi,

    You can do like below to get your option set value

    int Selectedvale = EntityNameObject.GetAttributeValue<OptionSetValue>("Youroptionsetfieldname").Value;

    and once you have this value you can use metadata service to get text for this value, check this post for sample code.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Thursday, May 1, 2014 6:34 AM
    • Marked as answer by Amy.4 Thursday, May 1, 2014 11:42 AM
    Thursday, May 1, 2014 6:34 AM
    Moderator
  • Hi Amy,

    //use following code to get selected optionset option Text

    //Note: Register this plugin in MESSAGE: UPDATE, PRIMARY ENTITY:incident ,POST Operation

    // Image for this step: POSTIMAGE: PostImage (Image EntityAlias= PostImage)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Metadata;

    namespace CRM_Plugins

    {

        public class OptionsetvalueinTextfiledonupdateofCASE:IPlugin

        {

            string optionsetText = string.Empty;

            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 incident=(Entity)context.InputParameters["Target"];

                Entity incidentpostimage = (Entity)context.PostEntityImages["PostImage"];

                if(context.Depth>1)

                {

                    return

                }

               

                //Retrieve option set value change your optionset field schemaname(ex: casetypecode)

                OptionSetValue casetypecodevalue = incidentpostimage.GetAttributeValue<OptionSetValue>("casetypecode");

                //following code used to retrieve optionset option text

                RetrieveAttributeRequest attributereq = new RetrieveAttributeRequest

                {

                    EntityLogicalName = incident.LogicalName,

                    LogicalName = "casetypecode",

                    RetrieveAsIfPublished = true

                };

                RetrieveAttributeResponse attributeres = (RetrieveAttributeResponse)service.Execute(attributereq);

                PicklistAttributeMetadata picklistmetadata = (PicklistAttributeMetadata)attributeres.AttributeMetadata;

                OptionMetadata[] optionList = picklistmetadata.OptionSet.Options.ToArray();

                foreach(OptionMetadata options in optionList)

                {

                   

                    if(options.Value==casetypecodevalue.Value)

                    {

                        //get the selected optionset option Text

                         optionsetText = options.Label.UserLocalizedLabel.Label;

                    }

                   

                }

                //following code is used to Update Optionset text in text field

               

                Entity cse = new Entity("incident");

                cse["incidentid"] = incident.Id;

               

              //instead of "description" change it to your TEXT field schemaname

                cse["description"] = optionsetText;

                service.Update(cse);

            }

        }

    }

    ----------------------------

    note: registration steps


    plugin image :


    • Proposed as answer by Gugan A Thursday, May 1, 2014 7:01 AM
    • Edited by Gugan A Thursday, May 1, 2014 7:15 AM
    • Marked as answer by Amy.4 Thursday, May 1, 2014 11:42 AM
    Thursday, May 1, 2014 7:01 AM

All replies

  • Try this to get the optionset selected value

    var sval = Xrm.Page.getAttribute("field_name").getSelectedOption().text

    and then drop it into the text field 

    Xrm.Page.getAttribute("text_field").setValue(sval);

    Tuesday, April 29, 2014 6:33 PM
  • Hi Nico

    Thanks for the reply. I believe that the above code works through javascript but iam doing it through a plugin and iam new to working with plugins.

    I just want to diaplay the "text" value of optionset field and display in another text field.

    Ex: i have two option sets : abc - with value 30

        xyz - with value 40

    if i select abc then i want abc to fall in the other text field.

    Thanks

    Thursday, May 1, 2014 4:43 AM
  • Hi,

    You can do like below to get your option set value

    int Selectedvale = EntityNameObject.GetAttributeValue<OptionSetValue>("Youroptionsetfieldname").Value;

    and once you have this value you can use metadata service to get text for this value, check this post for sample code.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Thursday, May 1, 2014 6:34 AM
    • Marked as answer by Amy.4 Thursday, May 1, 2014 11:42 AM
    Thursday, May 1, 2014 6:34 AM
    Moderator
  • Hi Amy,

    //use following code to get selected optionset option Text

    //Note: Register this plugin in MESSAGE: UPDATE, PRIMARY ENTITY:incident ,POST Operation

    // Image for this step: POSTIMAGE: PostImage (Image EntityAlias= PostImage)

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Metadata;

    namespace CRM_Plugins

    {

        public class OptionsetvalueinTextfiledonupdateofCASE:IPlugin

        {

            string optionsetText = string.Empty;

            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 incident=(Entity)context.InputParameters["Target"];

                Entity incidentpostimage = (Entity)context.PostEntityImages["PostImage"];

                if(context.Depth>1)

                {

                    return

                }

               

                //Retrieve option set value change your optionset field schemaname(ex: casetypecode)

                OptionSetValue casetypecodevalue = incidentpostimage.GetAttributeValue<OptionSetValue>("casetypecode");

                //following code used to retrieve optionset option text

                RetrieveAttributeRequest attributereq = new RetrieveAttributeRequest

                {

                    EntityLogicalName = incident.LogicalName,

                    LogicalName = "casetypecode",

                    RetrieveAsIfPublished = true

                };

                RetrieveAttributeResponse attributeres = (RetrieveAttributeResponse)service.Execute(attributereq);

                PicklistAttributeMetadata picklistmetadata = (PicklistAttributeMetadata)attributeres.AttributeMetadata;

                OptionMetadata[] optionList = picklistmetadata.OptionSet.Options.ToArray();

                foreach(OptionMetadata options in optionList)

                {

                   

                    if(options.Value==casetypecodevalue.Value)

                    {

                        //get the selected optionset option Text

                         optionsetText = options.Label.UserLocalizedLabel.Label;

                    }

                   

                }

                //following code is used to Update Optionset text in text field

               

                Entity cse = new Entity("incident");

                cse["incidentid"] = incident.Id;

               

              //instead of "description" change it to your TEXT field schemaname

                cse["description"] = optionsetText;

                service.Update(cse);

            }

        }

    }

    ----------------------------

    note: registration steps


    plugin image :


    • Proposed as answer by Gugan A Thursday, May 1, 2014 7:01 AM
    • Edited by Gugan A Thursday, May 1, 2014 7:15 AM
    • Marked as answer by Amy.4 Thursday, May 1, 2014 11:42 AM
    Thursday, May 1, 2014 7:01 AM
  •    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity leads = (Entity)context.InputParameters["Target"];
    
                    Entity leadpostimage = (Entity)context.PostEntityImages["PostImage"];
                    var value = leadpostimage.FormattedValues["new_source"];
                    //throw new InvalidPluginExecutionException(value);
                    if (context.Depth > 1)
                    {
                        return;
                    }
    
      if (optionsetText.Equals("Web Visit"))
                    {
                        int value1 = 100;
                        leads.Attributes["new_scorepoint"].Equals(value1);
                    }
    
                    Entity lead1 = new Entity("lead");
                    lead1.Id = leads.Id;
                    lead1["description"] = value.ToString();
                    service.Update(lead1);

    Iam able to retrieve the label of optionset field using this code.. but unable to update it at the last step....

    Here is the error i have been facing

    Wednesday, May 7, 2014 7:16 AM
  • Hi,

       It seems the value may be null. debug the plugin and check the value of value variable


    • Edited by Gugan A Wednesday, May 7, 2014 7:33 AM
    Wednesday, May 7, 2014 7:29 AM