Wednesday, June 22, 2011

If you are looking for a simpler solution that only allows for changing the fetchXML used by the lookup control check out James's post at CRM 2011 Change SubGrid FetchXML. I had hoped that Microsoft would provide support for applying custom views to the lookup used by sub-grids, since you can use the Xrm.Page.ui.addCustomView method to apply a custom view to a lookup control. Unfortunately, the Xrm.Page.ui.addCustomView method only works for lookup controls. So for the past three full days I have spent my time trying to understand how the addCustomView method works so I could extend it to support sub-grids. The code below does this; however, IT HAS BEEN TESTED MINIMALLY and thus may not address your particular case. If you find issues or improve this please let me know so I can share it with the rest of the community.

27 comments:

Hi, I hope I understand your purpose of this post in right way. I need to prepare custom view for subgrid lookup which would filter records of my custom entity(services) according to a field on a form (records of selected client on invoice form). I would like to use your code but it's very difficult and i don't understand it at all. Please say sth about your project to make it easier to analyze and configure for our custom needs. Which part of your code define the name of the subgrid? What entity you are working on and what entity records your are filtering (on what parameters)? Please, I need your help. Regards. Monika

Please provide the name of the entity you are filtering, the filtered entities object type code, and the name of the grid and I should be able to show you how to use the above code to filter the sub-grid lookup form.

So…I prepared custom entity called „invoiced services ” and I have relationship 1:N between “invoice” and this custom entity ("invoiced services" are my equivalent of "invoiced products" which we can add to invoice, but they become from executed and finished services from serviceappointment entity). I need to be able to add one or many “invoiced services” to invoice but my aim is to give my users ability to choose only from those records which have the same name (customerid) of the client as the name (customerid) chosen on invoice form.

First I wanted to have this relationship visible on navigation panel on invoice form but I can put the subgrid on general form if it is really/only possibility to filter records to select.

So I want to filter “invoiced services” entity, subgrid name is “invoicedservices”, the name of the field on invoice form on what I want to filter is “customerid”, the name of the field on “invoiced services” is “kan_client”, the primary key name of “invoiced services” is “kan_invoicedservicesid”.

I checked object type code of “invoice services” entity by your code (var etc = Xrm.Page.context.getQueryStringParameters().etc) using it onLoad of “invoice services” form. It has given me number “10009”, but I’m not sure you were really asking me about it.

I hope thet this is enough clear. If you need more informations please give me to know. I would be very, very, very grateful for your help.

(By the way I have seen Jim’s Wang blog and his solution for multilookup filtering but it’s for version 4.0 and not for online crm which I’m using (I think his solution with plugin and javascript is far more difficult than yours - if obviously they really do what i need). I hope your solution will help in my case). Regards, Mona.

Hi, I'm trying to use your code to filter a subgrid which represents a many-to-many relationship between two custom entities. The Custom View "Test" is succesfully created but the filtering (my own fetchxml) is not applied. Do you have any idea what could go wrong?

FYI : I've also replaced the condition on line 21 with "if (1==2)" otherwise the code in the else statement would never be executed.

Based on the information you provided I believe the code below will get you the results you seek. You may need to adjust some items such as to and from attributes in the link-entity line of the fetchXML variable.

Please post the fetchXML, with the names of the entities in the relationship, and the name of the N:N relationship between the entities. All so, have you tried using Fiddler2, http://www.fiddler2.com/fiddler2/, to see the fetchXML that is being sent to the server when the lookup form displays or you refresh the grid on the lookup form?

Eric, thank you for this, but I'm still uncertain how can I use all the rest of your code. I guess I should use addSubgridCustomView function with my custom parameters (fetch and layout you gave me) onLoad of my invoice form(?). What about the rest - window.attachEvent("onload", function() { .... which you adviced to place in the onload event of a form containing a sub-grid. I don't understand what is this code for :(. Should I change sth there? Should I change sth in addSubgridCustomView definition? Now, After first test of using your code I get an error : Field:window, event:onLoad, error: cannot get property "paramName": object is empty or undefinied. Please give me more tips how use this code. Is it ok to place var fetchXML and var layoutXML in my javascript file where i call addSubgridCustomView("invoicedservices", 10009, "Filtered Invoiced Services", fetchXML, layoutXML, 0, true)? Regards, Mona.

The <all-attributes> node in the fetchXML variable should not wrap the filter node. Additionally, you need to set the object attribute of the layoutXML to be the object type code of the entity returned by the fetch statement. I have included a function, getObjectTypeCode, that takes an entity name as a parameter and then uses Microsoft logic to return the object type code of the passed in entity name.

Copy and paste all of the code for from line 5 - 164, this would be everything for window.onload and the addSubgridCustomView functions, into the onload event of your invoice form. Then also add something like the code below to the onload of the invoice form. Please let me know if this works out for you.

The code for from line 5 - 164, this would be everything for window.onload and the addSubgridCustomView functions, should be outside the prepareSubform function. Give that a shot and let me know how it works out.

Eric, I've done everything you've written but with some customisations because I have got some web errors (e.g. about lack of fetchXML, addSubgridCustomView function definition and some others).

I've put all of the code from 5 line to 164 into one function start() which I call onLoad of my invoice form. The rest code you have prepared for me I've also put together into one function make() which also I call onLoad. But I don't know where and how I should call function buildCustomView(). I have tried to use it ("buildCustomView();") before call out "init();". Is this ok? One problem was fact that In your definition of init function I don't have fetchXML and layoutXML - so I have copied them to init() from buildCustomView(). Next I've got error that there not exists definition of addSubgridCustomView() function, so I also copied it to make() function from start() (so do I really need that code in start() function?).

Now I get only one error (I'm not sure how it looks in english version becuse I use other language) saying sth like: "There has occured error connected with nonstandard (custom) event of this field; Field:window; Event:onload; Error: Server of automatization can't create an object."

Ehhh :( I know you have done a lot for me for now but I need more your help. Have you any idea what the problem could be? Maybe my usage of your code is still wrong? Please, help me to find out...I was so happy that solution is so near...Regards, Mona.

Line 5 - 105 need to NOT be enclosed in a method. They should be placed before you define any methods or variables. In regards to lines 144 and 145, these are used to generate a GUID for the custom view. If you are having trouble using the ActiveX control try looking into using something like the Math.uuid.js library from http://www.broofa.com/2008/09/javascript-uuid-function/ which can provide you with a RFC 4122-compliant UUID. You would need to include Math.uuid.js on the desired form and then you would remove line 144 and replace line 145 with something like

var viewId = Math.uuid();

If that does not work please post your entire script for the form and I'll take a look and see what I can do to get this to work for you.

(I have all code above in one javascript file but I call out onLoad invoice form start() function at first and make() function next. Now I'm getting one error: Automation server can't create object. I have found your post about "'Automation server can't create object' JavaScript Error" and set changes in my internet explorer security settings but it doesn't change anything. :/ Have you any other ideas?

Ok I resolved problem with this error: Automation server can't create object. You had right. I changed 'Initialize and script ActiveX controls not marked as safe for scripting' to enable but not for my Local Intranet Zone but for Trusted webs where I firstly added my crm firm address. It works...but now nothing happens when I open invoice form...If I choose 'Add existing records' I don't see my custom view:/ Ohhhh..why it is so complicated...sorry that I produce so much comments;)...I'm still testing and waiting for your any ideas. Regards, Mona.

Ok, I think now I understand what should be where. I checked also that addSubgridCustomView() function is call out (I added an alert). My only problem is fact that custom view isn't created:/ Could mistakes in layoutXML or fetchXML be the problem? I checked them a lot of times and I think they are ok but maybe I don't really understand their definitions(?). What could be the reason? I don't get any errors from the web...Please help.Mona

Eric...all day long I analyzed my code. I don't get this. For another entity I tried to use all your code from 1 to 184 to check if it works for accounts as you present. Firstly I've got error about usage of customViews attribute (object is empty or undefinied)...but even I haven't changed anything the error stopped to appear and everything looks fine but my custom view still doesn't exist. I know that you have written that your code was minimally tested...but are you sure it works for you? Where are the differents between ours code? Maybe some subgrid settings are the reason? What subgrid id are you using? What default view have you chosen in those settings? I know it's a little stupid if it was really the problem but I don't have any other ideas. I didn't change anything in code from 1 to 105 because it is too strange and complicated for me..but maybe the problem is there? Regards, Mona.

in the init method before calling addSubgridCustomView. If that does not work, please send me the javascript files you are using on the page and I'll see what else might be causing this not function. I can be reached at mscrmking@gmail.com