Translate

Monday, 10 June 2013

How to Convert Option Set in to a Check Box list ?

How to Convert Option Set in to a Check Box list with java script.

Here is my new update, in this blog you can find the process and code to convert option set into a check box list. And one important thing is that it will work on CRM roll-up 11, CRM roll-up 12 and CRM roll-up 13. So just enjoy the great technology like CRM.

Step 1:- First Create option set which you want to convert into Checkbox list.
Step 2:- Create another attribute to store value of option set.
Step 3:- Add both the fields on the entity form.
Step 4:- Create new web resource or update existing web resource with the fallowing code.  

    function Form_onload() {

    var _crtlSociaklNW = document.getElementById("new_socialnetwork");
    var _crtlTextSocial = document.getElementById("new_textsocial");

    if (_crtlSociaklNW != null && _crtlTextSocial != null) {

        _crtlSociaklNW.style.display = "none";
        var pdiv = document.createElement('div');
        pdiv.style = 'overflow-y:auto; height:100px; border:1px #6699cc solid;          background-color:#ffffff;';
        _crtlSociaklNW.parentNode.appendChild(pdiv);
        ///////////////////////////////////////////////////////////////////////////
///////////////////// Convert option set to check box//////////////////////
///////////////////////////////////////////////////////////////////////////
        for (var i = 1; i < _crtlSociaklNW.options.length; i++) {
            var OptionSetItems = _crtlSociaklNW.options[i];
            if (!IsChecked(OptionSetItems.text, _crtlTextSocial)) {
                var addInput = document.createElement('input');
                addInput.type = 'checkbox';
                addInput.style.pixelWidth = 30;
            }
            else {
                var addInput = document.createElement('input');
                addInput.type = 'checkbox';
                addInput.checked = true;
                addInput.style.pixelWidth = 30;
            }

            var addLabel = document.createElement('label');
            addLabel.innerText = OptionSetItems.text;
            var addBr = document.createElement('br');
            var formname = Xrm.Page.getAttribute("new_name").getValue();

            _crtlSociaklNW.nextSibling.appendChild(addInput);
            _crtlSociaklNW.nextSibling.appendChild(addLabel);
            _crtlSociaklNW.nextSibling.appendChild(addBr);

        }
    }
}
///////////////////////////////////////////////////////////////////////////
///////////////// To check if which check box is selected//////////////////
///////////////////////////////////////////////////////////////////////////
function IsChecked(pText, _crtlTextSocial) {
    if (_crtlTextSocial.value != "") {
        var _crtlTextSocial = _crtlTextSocial.value.split(",");
        for (var i = 0; i < _crtlTextSocial.length; i++) {
            if (_crtlTextSocial[i] == pText)
                return true;
        }
    }
    return false;
}
///////////////////////////////////////////////////////////////////////////
///function to save the selected Items from check box list to TextSocial///
///////////////////////////////////////////////////////////////////////////
function Form_onSave() {
  
    var _crtlSociaklNW = document.getElementById("new_socialnetwork");
  
    var txtSocialValue = "";
    var getInput = _crtlSociaklNW.nextSibling.getElementsByTagName("input");

    for (var i = 0; i < getInput.length; i++) {
        if (getInput[i].checked) {
            txtSocialValue += getInput[i].nextSibling.innerText + ",";
        }
    }
    Xrm.Page.getAttribute("new_textsocial").setValue(txtSocialValue);

}

Step 5:- Now call Form_onload() function on Form OnLoad event and Form_onSave() function on             form OnSave event. And then save and publish form.
            Now your form will look like this.
Step 6:- Select check boxes and then save then it will look like this.
Step 7:- Now Hide TextSocial on the entity form.
Step 8:- Now it will look like.
 Soon i will write my new post "How to hide these check boxes on the basis of condition"

Enjoy the great technology Dynamics CRM. Good luck.....!!!!! 
Post a Comment