locked
Javascript problem with Two Option field and getting code to execute. RRS feed

  • Question

  • I have a Two Option field, called new_bitfield, by default the bitfield is set to true, this was done when creating the field, as you have to select a default value, so I set it as "True".

    My problem is this: I have an if/else function that executes code when a field value is changed:

    function bitFieldTest() {
        if (Xrm.Page.getAttribute("new_bitfield").getValue() == false) {
            alert("false");
        } else if (Xrm.Page.getAttribute("new_bitfield").getValue() == true) { //
            alert("true");
        }
    }

    If a user clicks on "True" in the Two Option field, I can't get the code to trigger an alert or execute the corresponding code. This is because the value was already set to "true", so there is no real change, thus the code is not executed. Is there a solution to this?

    Tuesday, July 2, 2013 11:05 AM

Answers

  • Hi Marcus,
    if there is no a real change the javascript function is not executed (the crm event is OnChange, different from standard javascript events like focus/blur)

    You can always add your code inside the onload/onsave event (checking if  the new_bitfield value is true) or create a ribbon button that will execute the javascript code.


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Tuesday, July 2, 2013 11:28 AM
    • Marked as answer by Sebd.DD Tuesday, July 2, 2013 11:29 AM
    Tuesday, July 2, 2013 11:28 AM
  • The problem you are facing is due to selection of wrong attribute type. As the bit field will alway a default value therefore when ever to want that initially the field should be empty i.e. no default value than use the picklist by adding "Please Select" as the default picklist Value and "Yes" and "No" as options. Now the good thing is that if the user don't select a value from "Yes" or "No" than  we can prompt  him. The only extra work you have to do is to update all old data into new picklist field. You must thing of any other workflow, plugin, reports using the old bit field.


    Regards Faisal

    • Marked as answer by Sebd.DD Tuesday, July 2, 2013 11:48 AM
    Tuesday, July 2, 2013 11:30 AM

All replies

  • Hi Marcus,
    if there is no a real change the javascript function is not executed (the crm event is OnChange, different from standard javascript events like focus/blur)

    You can always add your code inside the onload/onsave event (checking if  the new_bitfield value is true) or create a ribbon button that will execute the javascript code.


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Tuesday, July 2, 2013 11:28 AM
    • Marked as answer by Sebd.DD Tuesday, July 2, 2013 11:29 AM
    Tuesday, July 2, 2013 11:28 AM
  • The problem you are facing is due to selection of wrong attribute type. As the bit field will alway a default value therefore when ever to want that initially the field should be empty i.e. no default value than use the picklist by adding "Please Select" as the default picklist Value and "Yes" and "No" as options. Now the good thing is that if the user don't select a value from "Yes" or "No" than  we can prompt  him. The only extra work you have to do is to update all old data into new picklist field. You must thing of any other workflow, plugin, reports using the old bit field.


    Regards Faisal

    • Marked as answer by Sebd.DD Tuesday, July 2, 2013 11:48 AM
    Tuesday, July 2, 2013 11:30 AM
  • Thanks for the reply guys. I've gone with both suggestions and amended my code where the bit field is not necessary.
    Tuesday, July 2, 2013 11:49 AM