In the contact entity, modify the related orders default view

That one is extremely tricky. My customer had this sweet, short request: change the default view of related orders, in the contact entity.

RelatedOrders

By default, the defaule view is set to “Active”, and the customer wanted it to be “All”. How to do that? Well, here’s the answer below. It took me 5 hours to come up with this solution, but it works.
Highly unsupported if course…

Here’s the code below:

function setDefaultView() {
var grid = document.getElementById(“crmGrid_order_customer_contacts”);
    if (grid) {
        XMLHttpRequest.prototype.reallysend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function (body) {
            var that = this;
            if (body) {
                body = body.replace(‘<statecode>0</statecode>’, ‘<statecode>All</statecode>’);
                body = body.replace(‘<statecode>1</statecode>’, ‘<statecode>All</statecode>’);
                body = body.replace(‘<statecode>2</statecode>’, ‘<statecode>All</statecode>’);
                body = body.replace(‘<statecode>3</statecode>’, ‘<statecode>All</statecode>’);
                body = body.replace(‘<statecode>4</statecode>’, ‘<statecode>All</statecode>’);
            }
            this.onreadystatechange = function () {
                if (this.responseXML && XMLHttpRequest.prototype.reallysend) {
                    XMLHttpRequest.prototype.send = XMLHttpRequest.prototype.reallysend;
                    XMLHttpRequest.prototype.reallysend = null;
                }
            }
            return this.reallysend(body);
        }
        $(‘#crmGrid_order_customer_contacts_statecode’).find(‘option[value=”All”]’).prop(‘selected’, ‘selected’);
        grid.control.refresh();
}
This deserves a few explanations and configurations:
  1. I use jQuery to change the selected option in the picklist. However, and of course, this doesn’t change the displayed data. And, a call to refresh after changing this value, does not get the right data. It still retrieves only the active records. So to access jquery in this code, as we are in the context of an associated view, I use the ribbon trick (could also have deleted the js web resource directly and evaled it).
  2. I use a global variable, ouch. So I name it carefully.
  3. I replace the send function of the XMLHttpRequest object… That becomes sweet… 🙂 The point is to replace, in the message sent to the server, the statecode 0, by All, so that all records are retrieved, instead of the active ones.
  4. After the first pass, I reset the original send function, otherwise the replace will be applied to all subsequent queries, always retrieving all the records…

And here you go, a nice related view, with the default filter being set on ‘All’ !

That one is my pride. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *