Pages

Wednesday 19 February 2014

Filtering Records in Sub-Grid Based upon the Lookup

Hi Folks,

The Below scenario explains how to filter the Subgrid using a record selected in Lookup.

In this scenario I have a lookup for selecting the courses in Post Graduation. If we select any of the record in lookup, then the corresponding subjects must be displayed in the grid view.

The below screen shot shoes the subgrid name



For creating this we require the FetchXml code, which we can get through Advanced Find in CRM.
The below snippet code can be used to filter the records,

---------------->>

function updateSubGrid() {  
    var Subjects = document.getElementById("Subjects");
    var lookupfield = new Array;
var dept;
    lookupfield = Xrm.Page.getAttribute("lookup schema name").getValue();
    if (lookupfield != null) {
        var lookupid = lookupfield[0].id;
dept=lookupfield[0].name;                              //I am storing the name of record in "dept" variable 
}
    else 
{
        return;
}
    if (Subjects ==null || Subjects.readyState != "complete")
{
        setTimeout('updateSubGrid()', 2000);
        return;
}    
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "<entity name='new_subjects'>";
    fetchXml += "<attribute name='new_subjectsid' />";
    fetchXml += "<attribute name='new_name' />";
    fetchXml += "<attribute name='createdon' />";
    fetchXml += "<order attribute='new_name' descending='false' />";
    fetchXml += "<filter type='and'>";
    fetchXml += "<condition attribute='new_dept' operator='eq' uiname='"+ dept +"' uitype='new_department' value='" + lookupid + "' />";                                                               //dept specifies the name of record and lookupid represents Guid
    fetchXml += "</filter>";
    fetchXml += "</entity>";
    fetchXml += "</fetch>";
    Subjects.control.SetParameter("fetchXml", fetchXml);
    Subjects.control.refresh(); 

}   

Thursday 13 February 2014

Add Button to CRM forms using JScript

Hi Folks,

For this Scenario, I want my custom button below a specific attribute. So i've created a text field and passed that attribute name to my Web Resource. The blow Snippet Code should be added On-Load function of your entity.


------------>
On-Load code
------------->
function addButton(attribute_name) {
    if (document.getElementById(attribute_name) != null) {
        var FieldID = "field" + attribute_name;
        var elementID = document.getElementById(attribute_name + "_d");
        var div = document.createElement("div");
        div.style.width = "20%";
        div.style.textAlign = "right";
        div.style.display = "inline";
        elementID.appendChild(div, elementID);
        div.innerHTML = '<button id="' + FieldID + '" type="button" style="margin-left: 4px; width: 50%; " >Click Me</button>';
        document.getElementById(attribute_name).style.width = "80%";
        document.getElementById(FieldID).onclick = function () {onbuttonclick(); };
    }
}

function onbuttonclick() {
    alert('Button Clicked');
}


While triggering the code we should have to send the schema name of the created textfield, since i want my custom button to be below this text field.

 

the below screen shot shows my custom button on the form



Wednesday 12 February 2014

Converting Option set into CheckBox

For converting Option set into check box we require the following code. here in the below snippet I have created an option set and multiple line of text. I am storing all the checked options in a Multiple line of text and later again retrieving. Where the multiple line of text field is hidden

Step 1: Create an option set
 

Step 2:- Create a multiline of text field
 

Now add both the field to the form and uncheck the visibility of multipleline of text field from its property

Now add the Following snippet codes

For Onload Code
-------------->
 function Form_onload() {

    var optionset = document.getElementById("optionset_schema_name");
    var multiline = document.getElementById("multiline_schema_name");

    if (optionset != null && multiline != null) {

        optionset.style.display = "none";            //"none" represents override of existing, the optionset field is overrided
        var pdiv = document.createElement('div');    //Create an element
        pdiv.style = 'overflow-y:auto; height:100px; border:2px #6699cc solid; background-color:#ffffff;';

        //overflow y represents vertical scroll bar for options if options are more which will display if options are more.

        optionset.parentNode.appendChild(pdiv);

// Convert option set to check box

        for (var i = 1; i < optionset.options.length; i++) {
            var OptionSetItems = optionset.options[i];
            if (!IsChecked(OptionSetItems.text, multiline)) {
                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');

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

        }
    }
}


// To check if which check box is selected
function IsChecked(pText, multiline) {
    if (multiline.value != "") {
        var multiline = multiline.value.split(",");
        for (var i = 0; i <multiline.length; i++) {
            if (multiline[i] == pText)
                return true;
        }
    }
    return false;
}







On Save Code
---------------->
 function Form_onSave() {
 
    var optionset = document.getElementById("
optionset_schema_name");
 
    var multiline = "";
    var getInput = optionset.nextSibling.getElementsByTagName("input");

    for (var i = 0; i < getInput.length; i++) {
        if (getInput[i].checked) {
            multiline += getInput[i].nextSibling.innerText + ",";

//placing all the options into multiline of text
        }
    }
    Xrm.Page.getAttribute("
multiline_schema_name").setValue(multiline);

}



After Adding the code to your entity, the option sets will be displayed as shown  in the below manner.