CRM 2011: Filter the customerid lookup field on quote form

And here we go for another highly unsupported customization.

This one has the following assumptions:

  • You know customerid is used only for showing contacts
  • There is another field on the form to show accounts, named new_AccountId
  • You want customerid to show only contacts from the selected account
  • You have jquery, otherwise the code will be slighly different and longer, but it’s of course possible

Here’s the code to run in the onLoad of the quote form:

 

$(‘#customerid’).attr(‘relationshipid’, ‘contact_customer_accounts’);
$(‘#customerid’).attr(‘dependantattributename’, ‘quote.new_AccountId’);
$(‘#customerid’).attr(‘dependantattributetype’, ‘1’);

 

And your lookup is filtered!

CRM 2011 Dialog: how to filter a lookup field

From what I’ve discovered while playing with dialogs, when using a lookup field, the filter criteria that you put on the field are simply ignored by the dialog. By default, the dialog will set the lookup default view to the concerned entity’s lookup view. That can be a problem sometimes. To solve this, here is another highly unsupported customization that I’ve come up with today.

 

1. Find the guid of your dialog (just open it and look for the id parameter in the url)
2. Create the view you want to use to filter the lookup, and get its guid (same method)
3. Open this file: “C:Program FilesMicrosoft Dynamics CRMCRMWebCSdialogrundialog.aspx”
4. Open the dialog, and using Chrome for example, find the lookup img’s id. It will look like this: “InteractionStep7”
5. At the end of the script tag, add the following function:

function setId(dialogGuid, viewGuid) {
if (window.location.href.indexOf(dialogGuid) > -1) {
var lookupImg = document.getElementById(‘InteractionStep7’);
if(!lookupImg) {
setTimeout(setId, 100);
} else {
lookupImg.setAttribute(‘defaultviewid’, viewGuid);
}
}
}
6. Then in the  same file, make the body tag like this, with the appropriate guids of course: <body onload=”setId(‘123124435’, ‘123124345’)”>
Explanations
Finding the guids are obivous.
Then editing the rundialog file, please note that this is highly unsupported of course.
Looking at the dialog page’s dom elements, you will see that the lookup image has an id. But that id is not set when onload is called. Probably some javascript executed there to get the dialog’s data.
This is why I use a timeout, that just waits for that id to be set. When the element is found, then I update the default view id, ie the view that will be use in the lookup window url.
And that’s it!

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. 🙂