locked
Add ability to enter text into option set RRS feed

  • Question

  • Is there a way to do one of these two things:

    • Allow the user to enter text into an option set if there isn't an appropriate option
    • If "Other" is selected in an option set, a text field for entry appears.

    I would say that option 2 is a better solution, but whatever is easier and requires no coding (if possible). 

    Tuesday, August 12, 2014 3:27 PM

Answers

  • Step 1: In the Library field select the name of the web resource that contains your javascript code.

    Step 2: In the Function field enter the name of the function that acts as an entry point to your logic.

    Here is a basic entity.js (your js resource) functional structure and an example with your requirement already built-in. hopefully it makes more sense now.

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.getAttribute("new_othertext");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText;
            );
    }

    Cheers


    Dev Blog: Dynamics CRM - Thinking outside the Box


    • Edited by Adi Katz Wednesday, August 13, 2014 2:26 AM
    • Marked as answer by nasyrax Monday, August 18, 2014 7:52 PM
    Tuesday, August 12, 2014 9:15 PM

All replies

  • If you are using MS CRM 2013, then you can implement option 2 using business rules, in business rule you can check if "Other" is selected make text field visible otherwise keep it hidden.

    Check this post


    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.

    Tuesday, August 12, 2014 4:46 PM
    Moderator
  • Sorry, I forgot to specify I'm using CRM 2011.
    Tuesday, August 12, 2014 5:05 PM
  • Ok, still I think you need simple script for 2 like something like below, you can use it onchange of your optionset

    if(Xrm.Page.getAttribute("OptionsetField")!=null && Xrm.Page.getAttribute("OptionsetField").getSelectedOption()!=null)
    {
    var TxtField=Xrm.Page.ui.controls.get("txtfield");
    if(Xrm.Page.getAttribute("OptionsetField").getSelectedOption().text=="Others")
    {
    if(TxtField!=null) {   TxtField.setVisible(true); }
    }
    else if(Xrm.Page.getAttribute("OptionsetField").getSelectedOption().text!="Others")
    {
    if(TxtField!=null) {  TxtField.setVisible(false);}
    }

    }



    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.

    Tuesday, August 12, 2014 5:40 PM
    Moderator
  • I know I need to make a web resource with the above script, and that I need to make an option set with "Others" as an option, then add the web resource I created to the Form Libraries on the field that has the option set I created, but I'm not clear as to what to put in the handler options when I click Add under Event Handlers.
    Tuesday, August 12, 2014 6:20 PM
  • Step 1: In the Library field select the name of the web resource that contains your javascript code.

    Step 2: In the Function field enter the name of the function that acts as an entry point to your logic.

    Here is a basic entity.js (your js resource) functional structure and an example with your requirement already built-in. hopefully it makes more sense now.

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.getAttribute("new_othertext");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText;
            );
    }

    Cheers


    Dev Blog: Dynamics CRM - Thinking outside the Box


    • Edited by Adi Katz Wednesday, August 13, 2014 2:26 AM
    • Marked as answer by nasyrax Monday, August 18, 2014 7:52 PM
    Tuesday, August 12, 2014 9:15 PM
  • OK, I made a new web resource with the newly written (thank you) code, did everything else and got the error below when I select "Other" from the option list:

         There was an error with the field's Customized event.

         Field:pps_optionsettest

         Event:onchange

         Error:'OnCrmPageLoad' is undefined

    Here are my field properties:

    Here are my handler properties:


    • Edited by nasyrax Wednesday, August 13, 2014 1:00 PM
    Wednesday, August 13, 2014 12:41 PM
  • Share you code what you used, here.

    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.

    Wednesday, August 13, 2014 1:04 PM
    Moderator
  • var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.getAttribute("new_othertext");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText;
            );
    }

    Wednesday, August 13, 2014 1:08 PM
  • Hi,

    try to use below code

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.ui.controls.get("new_othertext"); 
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText);   /no semicolon 
    }


    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.

    Wednesday, August 13, 2014 2:11 PM
    Moderator
  • Same error. Let me make sure I'm doing this right. I create an option set named pps_optiontest with the following options:

    • Option1
    • Option2
    • Option3
    • Other

    Then I create a web resource named pps_optionSetCustom with the following javascript:

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.ui.controls.get("new_othertext"); 
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText)  /no semicolon 
    }
    (I removed the semicolon as instructed in comment)

    I create a new field in a form named pps_AddOtherOption and place it on the form

    Open that fields properties ang go to Events. Under Form Libraries I Add pps_optionSetCustom. Then in Event Handler I click Add and type OnCrmPageLoad. Save and Publish. Open a record with the new field and select Other from the option set.

    Is there a step I'm missing?

    Wednesday, August 13, 2014 3:25 PM
  • Have you changed fields name ??

     optionSet1 = xrmPage.getAttribute("new_optionset1");   //replace optionset name here 
        otherText  = xrmPage.ui.controls.get("new_othertext"); //replace text field name


    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.

    Wednesday, August 13, 2014 4:44 PM
    Moderator
  • You’ve added the OnCrmPageLoad handler to the fields OnChange handler. That’s wrong.

    You need to add the handler to the form OnLoad handler. See image:

    And here is the code again without the semi colon:

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("new_optionset1");
        otherText  = xrmPage.getAttribute("new_othertext");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(optionSet1.getText() === otherOptionText);
    }
    GL


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Wednesday, August 13, 2014 5:16 PM
  • OK, changed them, here is the script:

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_otherfield");
        otherText  = xrmPage.ui.controls.get("pps_addotheroption"); 
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(
                optionSet1.getText() === otherOptionText)  /no semicolon 
    }

    now I get this:

    Wednesday, August 13, 2014 5:35 PM
  • The Form OnLoad event is the only handler that needs binding (see image).
    You must remove the onchange handler from the pps_addotheroption field + save + publish.


    Dev Blog: Dynamics CRM - Thinking outside the Box


    • Edited by Adi Katz Wednesday, August 13, 2014 6:22 PM
    Wednesday, August 13, 2014 6:21 PM
  • Removed handler from field, added handler to Form OnLoad, now I get the following error

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_otherfield");
        otherText  = xrmPage.getAttribute("pps_addotheroption");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(optionSet1.getText() === otherOptionText);
    }


    • Edited by nasyrax Thursday, August 14, 2014 5:54 PM
    Thursday, August 14, 2014 5:53 PM
  • You wrote:

    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_otherfield");
        otherText  = xrmPage.getAttribute("pps_addotheroption");
    }

    Issues is see:
    1. The declaration should be the other way around i.e. pps_otherfield is the text field and pps_addotheroption is the optionset.
    2. Since you’re playing with visibility optionSet1 and othreText should reflect the control instance and not the attribute instance as shown in the example below: 

    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getControl("pps_addotheroption");
        otherText  = xrmPage.getControl("pps_otherfield ");
    }

    Other than it looks ok.
    Cheers

    Dev Blog: Dynamics CRM - Thinking outside the Box


    • Edited by Adi Katz Thursday, August 14, 2014 6:35 PM
    Thursday, August 14, 2014 6:14 PM
  • OK, I changed

    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_otherfield");
        otherText  = xrmPage.getAttribute("pps_addotheroption");
    }

    to

    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getContol("pps_addotheroption");
        otherText  = xrmPage.getContol("pps_otherfield ");
    }
    And get the following:

    Do I need to revert to the script being on the field as oppose to the form? Is there another field I need to add?


    • Edited by nasyrax Thursday, August 14, 2014 6:24 PM
    Thursday, August 14, 2014 6:23 PM
  • LoL ... fixed a small typo. Try it now :-)

    Dev Blog: Dynamics CRM - Thinking outside the Box

    Thursday, August 14, 2014 6:36 PM
  • Now the error is "Object doesn't support property or method 'addOnChange'"
    Thursday, August 14, 2014 6:40 PM
  • // add this function to the onload
    function OnCrmPageLoad() {
       OnOptionSet1Change();
    }
    
    // add this function to the onchange event of the optionset
    function OnOptionSet1Change() {
        var otherText = Xrm.Page.getControl("pps_otherfield");
        if (Xrm.Page.getAttribute("pps_addotheroption").getText() == "Other") {
           otherText.setVisible(true);
        } else {
           otherText.setVisible(false);
        }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile


    Thursday, August 14, 2014 6:52 PM
  • Man, this is getting real confusing for a little block of text! lol! So do I add the OnCrmPageLoad function before or after the first OnCrmPageLoad function? And where does the OnOptionSet1Change function get added? Do I create a new web resource for it?
    Thursday, August 14, 2014 7:30 PM
  • Lucky number 7 :-( 

    var otherOptionText = "Other",
        xrmPage = Xrm.Page,
        optionSet1,
        otherText;
        
    //handler calls this function
    function OnCrmPageLoad() {
        Initialize();
        AttachEvents();
        ProcessForm();
    }
    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_addotheroption");
        otherText  = xrmPage.getControl("pps_otherfield");
    }
    //attach all change/save evnets
    function AttachEvents() {
        optionSet1.addOnChange(OnOptionSet1Change);
    }
    //process form load
    function ProcessForm() {
        OnOptionSet1Change();
    }
    //event handlers
    function OnOptionSet1Change() {
        otherText.setVisible(optionSet1.getText() === otherOptionText);
    }


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Thursday, August 14, 2014 7:31 PM
  • you need to create a single javascript webresource with only the code I posted before.

    attach the function OnCrmPageLoad to the onload event of the form

    attach the function OnOptionSet1Change to the onchange event of the optionset field pps_addotheroption.

    done.

     

    My blog: www.crmanswers.net - Rockstar 365 Profile


    Thursday, August 14, 2014 7:32 PM
  • Don’t listen to Guido :-), he’s messing with your mind … LOL

    Try pasting the last piece of code I changed. It should work.


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Thursday, August 14, 2014 7:41 PM
  • I give up! lol! I get errors using both solutions that say setVisible: object is null or undefined.
    Thursday, August 14, 2014 8:46 PM
  • You’re almost there … don’t give up now :-)

    This error usually means you’re not using the correct field id. Are you sure it’s "pps_otherfield" ?

    If you put this line

    alert(otherText) above otherText.setVisible do you get [Object object] or null?

    i.e.

    function OnOptionSet1Change() {
    
        alert(otherText);
    
        otherText.setVisible(optionSet1.getText() === otherOptionText);
    
    }



    Dev Blog: Dynamics CRM - Thinking outside the Box

    Thursday, August 14, 2014 9:00 PM
  • Yup, still get the error. My option set has the following specs:

    Display Name=Other Field | Name=pps_otherfield

    The field that has this option set applied to it has the following specs:

    Display Name=Add Other Option | Name=pps_addotheroption


    • Edited by nasyrax Friday, August 15, 2014 2:17 PM
    Friday, August 15, 2014 2:14 PM
  • Tested the code. It works fine on my machine. There must be something else causing this issue.

    Do you have other scripts, handlers firing on this form ?  


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Friday, August 15, 2014 3:55 PM
  • I stand corrected, I went through the code and there was a sleight correction, which I made. Now this is the error

    Then I get the 'setVisible': object is null error.
    • Edited by nasyrax Friday, August 15, 2014 5:11 PM
    Friday, August 15, 2014 5:10 PM
  • Oh my …  This means that CRM can’t find pps_otherfield

    Leave the alert there until you see [object Object].

    How many instances of pps_otherfield do you have on the form ? more than 1 ?

    if you do you need to address this in your code by either specifying the correct control id (logical name) or specifying the control index.

    Finally,  I’ve added an alert at the top of the following function. Change it and tell me what you get.

    function OnOptionSet1Change() {
        //Does this produce an error or a number ?
     alert(xrmPage.getAttribute("pps_otherfield").controls.get().length;)
    
        //This line producess null
        alert(otherText);
    
        otherText.setVisible(optionSet1.getText() === otherOptionText);
    }



    Dev Blog: Dynamics CRM - Thinking outside the Box

    Friday, August 15, 2014 8:23 PM
  • pps_otherfield is the option set and pps_addotheroption is the field on the form with pps_otherfield as it's option set.
    Friday, August 15, 2014 8:36 PM

  • You gotta be kidding me ... LOL

    Change the Initialize function and keep your fingers crossed. 

    //init vars
    function Initialize() {
        optionSet1 = xrmPage.getAttribute("pps_otherfield");
        otherText  = xrmPage.getControl("pps_addotheroption");
    }

     



    Dev Blog: Dynamics CRM - Thinking outside the Box

    Friday, August 15, 2014 10:59 PM
  • Well, no error but unable to enter text in the "pps_addotheroption" field either.
    Monday, August 18, 2014 12:22 PM
  • either enable this field in the form customization (save + publish entity) or

    change the following function code (save + publish resource)

    function OnOptionSet1Change() {
        var bShowOther = optionSet1.getText() === otherOptionText,
            bDisableOther = !bShowOther;
    
        //show if Other    
        otherText.setVisible(bShowOther);
    
        //enable if other
        otherText.setDisabled(bDisableOther);
    }



    Dev Blog: Dynamics CRM - Thinking outside the Box

    Monday, August 18, 2014 5:59 PM
  • Not sure exactly what you mean by enable the field, but it is present on the form and I have saved/published it each time I have made changes. I modified the following code:

    function OnOptionSet1Change() {
    alert(otherText);
        otherText.setVisible(optionSet1.getText() === otherOptionText)
    }

    to read:

    function OnOptionSet1Change() {
        var bShowOther = optionSet1.getText() === otherOptionText,
            bDisableOther = !bShowOther;
    
        //show if Other    
        otherText.setVisible(bShowOther);
    
        //enable if other
        otherText.setDisabled(bDisableOther);
    }
    Saved/published, no error, still can't enter text after selecting "Other". This may be a stupid question, but the end result of the script should be when I select "Other" in the field, I could then type text into the same field, correct?

    Monday, August 18, 2014 6:32 PM
  • You have 2 fields:

    One is an optionset ("pps_otherfield") with options, with one of the option’s text set to “Other”  

    The other field is a text field ("pps_addotheroption") that you show when the “Other” option is selected in the optionset.

    When the user selects any option other then “Other” the text field is hidden and disabled

    When the user selects the “Other” option then the text field becomes enabled and you can see it and type “Other” text into it.

    Both fields need to be on the form.

    Isn’t that you goal ?


    Dev Blog: Dynamics CRM - Thinking outside the Box



    • Edited by Adi Katz Monday, August 18, 2014 7:10 PM
    Monday, August 18, 2014 7:08 PM
  • OK, it works now. The problem was that "pps_otherfield" was a global option set, not a local one. So I just removed "pps_otherfield" from the form, deleted it from the entity, then recreated it using a local option set, added both "pps_otherfield" and "pps_addotheroption" to the form with the "Visible by default" option for "pps_addotheroption" unchecked, saved/published, and it works! Thanks for all the help! 
    Monday, August 18, 2014 7:52 PM
  • It might be quick late but in this senario using a lookup is better option then picklist. As you are using of the shelf feature and security is much better

    Regards Faisal

    Tuesday, August 19, 2014 3:39 PM