I had a requirement this week where a customer was adding products to a quote and when they selected a product they then want to see previous quotes which featured the same product and the price and quantity it sold for. Basically the sales person wanted to see what the product had been sold for before to make sure the price they were quoting wasn’t too radical.
The idea I came up with was to have a grid on the Quote Product form, which displayed quote products of the same type as the quote product selected.
This would mean I would have to dynamically change a view or show items in the grid.
I created a view showing the fields of the Quote Product and then specified a product I was searching for.
I found this excellent blog post
CRM 2011 – Change Subgrid View Java Script
This blog post explains how you can load in new fetchXML into a subgrid, you can inject the FetchXML on the form onload, which enables you to get some variables from the form and inject them into FetchXML which then is used to update the SubGrid.
So on my Quote Product form I checked to see if a product had been selected on the form OnLoad, on this particular form the subgrid doesn’t load until you have selected a product, unit and a quantity, which then reloads the form.
The code itself which I have taken and changed from the blog post referenced above, is quite clever. The function is called and if the SubGrid has not been loaded, it calls the function again after a small time delay. This ensures the SubGrid is loaded before you try and inject the fetchXML into the view.
The FetchXML is created, I got the fetchXML by doing an advanced find on my previously created view, there is a button which allows you to retrieve the FetchXM, which is very useful.
function UpdateSubGrid() { var quoteGrid = document.getElementById("Quote_Products"); var productId = Xrm.Page.getAttribute("productid").getValue(); if (productId != null){ //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding if (quoteGrid == null) { //The subgrid hasn't loaded, wait 1 second and then try again setTimeout('UpdateSubGrid()', 1000); return; } var fetchXml = "" + " <entity name='quotedetail'>" + " <attribute name='productid' />" + " <attribute name='productdescription' />" + " <attribute name='priceperunit' />" + " <attribute name='quantity' />" + " <attribute name='extendedamount' />" + " <attribute name='salesrepid' />" + " <attribute name='uomid' />" + " <attribute name='manualdiscountamount' />" + " <attribute name='baseamount' />" + " <attribute name='new_quoteid' />" + " <attribute name='quotedetailid' />" + " <order attribute='productid' descending='false' />" + " <filter type='and'>" + " <condition attribute='productid' operator='eq' uiname='1 Function Easy Clean Mini Kit' " + " uitype='product' value='" + productId[0].id +"' />" + " </filter>" + " <link-entity name='quote' from='quoteid' to='quoteid' visible='true' link-type='outer' alias='a_ff5a49bd001a45f7a14dd99a28f37f91'>" + " <attribute name='customerid' />" + " <attribute name='name' />" + " <attribute name='quotenumber' />" + " <attribute name='quoteid' />" + " </link-entity>" + " <link-entity name='product' from='productid' to='productid' visible='false' link-type='outer' alias='a_e0b689bded554f07ab0b241a932d482e'>" + " <attribute name='productnumber' />" + " </link-entity>" + " </entity>" + "</fetch>"; // alert(fetchXml); //Inject the new fetchXml quoteGrid.control.setParameter("fetchXml", fetchXml); //Force the subgrid to refresh quoteGrid.control.refresh(); }
Hello!
I have a problem when execute the line var quoteGrid = document.getElementById(“Quote_Products”) because the objects is null. what is the event where it is register? I have registered in onload.
Thanks
LikeLike
The subgrid I added to the Quote Products form was called Quote_Product.
LikeLike
I have tried this but keep getting an error on the subgrid refresh line, “Can’t execute code from a freed script!” I have RU 6 and this happens on both the webpage and Outlook client. Any help would be great. I did use a chart instead for a list if that makes a difference.
LikeLike
I have not ever come across this error before, you could perhaps try rollup 7 to see if that resolves the problem.
I did this web page released from the Microsoft premier team
http://blogs.msdn.com/b/crminthefield/archive/2012/01/12/podcast-and-overview-microsoft-dynamics-crm-2011-update-rollup-6.aspx
and it seems there are some problems with rollup 6 and internet explorer 9, the text below was taken from the bottom of the webpage
LikeLike
The problem with this approach is that it doesn’t work with User or Team owned entities because the grid is not available via document.getElementById(“GridName”) for those types of entities. I haven’t found a solution…
LikeLike
And the other problem with this approach is that even for Organization owned entities, the grid data displayed in Print Preview or the Outlook client will be the data for the view selected for the grid, NOT the result of the injected FetchXML.
LikeLike
Hi,
I’ve tried this, it doesn’t error yet doesn’t show any results, my xml hasn’t even got any filters! The grid visibly tries to reload but shows nothing
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
xml += ‘ ‘;
var grid = document.getElementById(“People”);
grid.control.setParameter(“fetchXml”, xml);
grid.control.refresh();
LikeLike
is the xml you are injecting into the fetchXml correct? perhaps it’s returning nothing.
LikeLike
figured it out, the fetchXml only seems to work on data that is in the grid already.
What I am doing is building a form on an entity called ‘Search’, Search has a many-to-many relationship to Contact, so I have the Contact grid on the Search form. When the user fills in some fields it tries to populate the Contact grid dynamically building the xml based on the fields completed.
If I actually add records to the Search-Contact link entity it works
LikeLike
This post helped me
http://crmblog.sentri.com/2012/04/adding-advanced-find-query-to-form.html
What I needed was to add a subgrid and select “All Record Types”, I didn’t need to create a dummy many-to-many relationship, all works now and I have a custom search form 🙂
LikeLike
good work, perseverance paid off. Thanks for adding the solution to the comments, hopefully you will help other people who experience this problem.
LikeLike
Do you know if it’s possible to do the same for layoutXML on the grid?
LikeLike
You want to change the grid columns dynamically?
I think I might have read about someone doing that but I haven’t done it myself. So I’m afraid you will have to give it a good goggling
LikeLike
Hi, I have a followong problem.
When I create a new quote, and i try to add quote products, i can filter the products. in the look up there is displayed all products from absolutely different product categories..
Any idea how can i filter this to leave only products which is related to qoute i want to create?
LikeLike
you need to create a view with criteria which only shows products from a selected quote.
then in your fetchxml you change the value of the quote to be the quote you are on.
LikeLike
setParameter(“fetchXml”, fetchXml); doesn’t work with online 2013.
LikeLike
I even tried changing the casing of the function name to SetParameter..as suggested in other posts…but it doesn’t work.
LikeLike