Friday, February 20, 2015

Minimum product quantity required when creating Quote/order/invoice

Some time in order needs to products with certain Quantity, if product added below certain Quantity, then need to stop user from creating quote.

I am doing this functionality using JavaScript.

In this example I am using crmFetchkit.js.  this kit is available on  http://crmtoolkit.codeplex.com

Here are steps

1. Create JavaScript web resource and add following code in JavaScript.. I created Js web resource with name Quote.js
In this script I am using FetchXML to retrieve products from Quote. To generate FetchXML, easiest option is using Advanced find.



2. Add latest jquery library in form library

3. Add CrmFetchkit.js web resource in Form library

4. Add Quote.Js in form library


5. Call CheckPrductQuantity with product name as parameter function onLoad and OnSave event of Form.



6. Save and publish your form. 

7. Now if product added with quantity less than 5, it will show message as below. 




Wednesday, February 18, 2015

Convert Option set to multi select Checkbox list

1.       Create two custom attributes to store selected value and selected text from check box list. 

2.       Add both attributes on form, this is required, it is not required to show those attributes on form, but those need on Form, so that JavaScript code will find those attributes.

3.        Add latest JQuery library in form library


4.       Create HTML web resource and add following code in HTML web resource

<html><head>
    <title></title>
    <script type="text/javascript" src="new_jquery_1.10.2.js"></script>
    <script type="text/javascript">

        // function will be called when web resource is loaded on Form.
        $(document).ready(function () {
            ConvertDropDownToCheckBoxList();
        });

        //Coverts option list to checkbox list.
        function ConvertDropDownToCheckBoxList() {
            var dropdownOptions = parent.Xrm.Page.getAttribute("new_makeyear").getOptions();
            var selectedValue = parent.Xrm.Page.getAttribute("new_selectedyears").getValue();

            $(dropdownOptions).each(function (i, e) {
                var rText = $(this)[0].text;
                var rvalue = $(this)[0].value;
                var isChecked = false;
                if (rText != '') {
                    if (selectedValue != null && selectedValue.indexOf(rvalue) != -1)
                        isChecked = true;

  /* Remove spaces before input, label word and end tags of input & label*/
                    var checkbox = "< input type='checkbox' name='r'/ >< label> " + rText + "</ label>"
                    $(checkbox)
                        .attr("value", rvalue)
                        .attr("checked", isChecked)
                          .attr("id", "id" + rvalue)
                        .click(function () {
                            //To Set Picklist Select Values
                            var selectedOption = parent.Xrm.Page.getAttribute("new_selectedyears").getValue();
                           
                            if (this.checked) {
                                if (selectedOption == null)
                                    selectedOption = rvalue+"";
                                else
                                    selectedOption = selectedOption + "," + rvalue
                            }
                            else {
                                var tempSelected = rvalue + ",";
                                if (selectedOption != null) {
                                    if (selectedOption.indexOf(tempSelected) != -1)
                                        selectedOption = selectedOption.replace(tempSelected, "");
                                    else
                                        selectedOption = selectedOption.replace(rvalue, "");
                                }
                            }
                            parent.Xrm.Page.getAttribute("new_selectedyears").setValue(selectedOption);


                            //To Set Picklist Select Text
                            var selectedYear = parent.Xrm.Page.getAttribute("new_selectedyeartext").getValue();
                            if (this.checked) {
                                if (selectedYear == null)
                                    selectedYear = rText+"";
                                else
                                    selectedYear = selectedYear + "," + rText
                            }
                            else {
                                var tempSelectedtext = rText + ",";
                                if (selectedYear != null) {
                                    if (selectedYear.indexOf(tempSelectedtext) != -1)
                                        selectedYear = selectedYear.replace(tempSelectedtext, "");
                                    else
                                        selectedYear = selectedYear.replace(rText, "");
                                }
                            }
                            parent.Xrm.Page.getAttribute("new_selectedyeartext").setValue(selectedYear);

                        })
                        .appendTo(checkboxList);
                }
            });
        }
    </script>
    <meta charset="utf-8">
<meta><meta><meta><meta></head><body style="word-wrap: break-word;">
    <div id="checkboxList">    
    </div>

</body></html>


Change new_makeyear   with option set schema name.
Change   new_selectedyears with custom attribute which is storing selected values.
Change   new_selectedyeartext with custom attribute which is storing selected Text.

1.       Add HTML web resource on Form.

2.       Save and publish your customization.

Final output will be like





Tuesday, February 17, 2015

Dynamically Filter form sub grid to show related records

Scenario: 

I have different Cars in system, and cars has different categories, when one car record is opened, I want to show all other cars of same category in sub grid.

Main thing is that there is not any relationship between cars.

Basically need to filter sub grid dynamically based on category of current record and show related records.



Resolution:

To achieve this there is not fully supported way, so I did using unsupported way..

1. Add Sub grid on form for car entity.

When adding sub grid, make sure initially show all records.
2. Add JavaScript web resource in Form library

3. Add following code in JavaScript Web resource




  

4.  Save form and publish. 

5. Now form subgrid is showing only related records based on category