none
CRM 2011 : Issue with Two Option Set in javascript

    Question

  • Hi

    I have a created a small javascript functionality where in, onload both the radio buttons(Two - Option Set) are set to null and when i click 'Yes' or 'No' a field becomes required.

    Here is the Code -

        document.EntityScript.IsDocumentSubmittted_OnChange = function () {
        if (Xrm.Page.getAttribute("new_hassubmitted").getValue()) != null) {
             var field = Xrm.Page.getAttribute("new_specifydetail");
             field.setRequiredLevel("required");
        }
        else
            var field = Xrm.Page.getAttribute("new_specifydetail");
            field.setRequiredLevel("none");
        }

    and on load i have written this -

        Xrm.Page.getAttribute("new_hassubmitted").setValue(null);

    Now the problem is 

    <b>On page load,when i click 'No' for the first time , the function does not get invoked however when i click 'Yes' the function gets invoked and after that if i click 'No' the function gets invoked </b>

    I want to know What value gets stored in the two-optionset when we set it as 'null'


    Thursday, November 21, 2013 6:38 AM

All replies

  • Hi,

    Please try to attach following code in Form onload event hander also :

    var el = document.getElementById("new_hassubmitted");
    if (el.addEventListener) {
        el.addEventListener('click', document.EntityScript.IsDocumentSubmittted_OnChange, false);
    } else if (el.attachEvent) {
        el.attachEvent('onclick', document.EntityScript.IsDocumentSubmittted_OnChange);
    }


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Thursday, November 21, 2013 6:48 AM
  • Hi Vikram,

    I tried putting this code onload but still the same thing is happening. onclick of 'No' it does not get invoked.
    Thursday, November 21, 2013 7:24 AM
  • Please try as mentioned on the following blog :

    http://blog.avtex.com/2011/03/27/trigger-on-change-on-checkboxes-in-crm-2011/

    Note : this is unsupported code.


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Thursday, November 21, 2013 7:30 AM
    Thursday, November 21, 2013 7:30 AM
  • Hi

    On page load,when i click 'No' for the first time , the function does not get invoked however when i click 'Yes' the function gets invoked and after that if i click 'No' the function gets invoked 

    This all happens because in crm with two option field it always have one value as default selected.

    when you check the default value of your field(new_hassubmitted), it is set to 'No'. 

    and once you change the default value to yes same should be happen with 'Yes' condition. 

    Thursday, November 21, 2013 7:34 AM
  • Hi,

    The default behavior of the two option is it will always have a default value (which you have mentioned in your post, either yes/no whichever you select). Consider "no" as the default selected:

    Now, since "No" is already selected so clicking "No" will not trigger the change event. This should be handled on load of the form and the code that you have written for the onchange will remain same to be called for the OnChange event of the fields.

    function myform_Onload() {
        var objfield1 = Xrm.Page.getAttribute("new_hassubmitted");
    
        if (objfield1 != null && objfield1.getValue() == false)
        {
            var field = Xrm.Page.getAttribute("new_specifydetail");
            field.setRequiredLevel("none");
        }   
    }
    
    function IsDocumentSubmitted_OnChange() {
        if (Xrm.Page.getAttribute("new_hassubmitted").getValue()) != null) {
             var field = Xrm.Page.getAttribute("new_specifydetail");
             field.setRequiredLevel("required");
        }
        else
            var field = Xrm.Page.getAttribute("new_specifydetail");
            field.setRequiredLevel("none");
        }
    }

    I hope this should resolve your issue.

    Thanks & Regards,


    Anubhav Bajpai


    • Edited by Anubhav Bajpai Thursday, November 21, 2013 8:09 AM updated objfield1
    Thursday, November 21, 2013 8:06 AM
  • Hi Anubhav

    In my code onload of the page both the radiobuttons are set to null 

    Xrm.Page.getAttribute("new_hassubmitted").setValue(null);

    But no matter i have done this it is defaulted to 'No' only.

    With your code also the issue is not resolved. 

    Thursday, November 21, 2013 8:50 AM
  • Perhaps you could do an alternative solution? Recreate the two fields as options sets with two options, yes and no. You can set that field to be required as well and then based on the option they choose you can fire your code. Using an optionset instead of two options will allow you to leave the field null when the form is loaded.
    Thursday, November 21, 2013 3:25 PM