CRM 2011 – displaying similar quote products in a subgrid by injecting FetchXML

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();
}

17 thoughts on “CRM 2011 – displaying similar quote products in a subgrid by injecting FetchXML

  1. Montse April 2, 2012 / 11:07 am

    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

    Like

    • Hosk April 2, 2012 / 11:31 am

      The subgrid I added to the Quote Products form was called Quote_Product.

      Like

  2. Cory Bonallo April 2, 2012 / 9:17 pm

    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.

    Like

    • Hosk April 3, 2012 / 12:05 am

      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

      Internet Explorer 9 Compatibility

      The Microsoft Dynamics CRM Sustained Engineering team extensively tested Microsoft Dynamics CRM 2011 against pre-release versions of Internet Explorer 9 and continues to address compatibility issues, as appropriate, against the RTW (Released to Web) version of Internet Explorer 9. When necessary, Microsoft plans to release enhancements via future Microsoft Dynamics CRM 2011 Update Rollups to assure compatibility with Internet Explorer 9.

      There are three CRM 2011 application compatibility fix released via Update Rollup 6 related to Internet Explorer 9:

      When you browse in the Microsoft Dynamics CRM web client by using Internet Explorer 9, Internet Explorer consumes excessive memory. Therefore, you experience slow performance
      When you use the Schedule feature in a service appointment in Microsoft Dynamics CRM 2011 with Internet Explorer 9, you receive the following error message:
      Can’t execute code from freed script
      In Internet Explorer 8 or 9, ribbons are not displayed immediately when you open a form in Microsoft Dynamics CRM
      There is a Update Rollup 5 fix for an expanding memory footprint issue associated with Internet Explorer, but it impacts all versions of Internet Explorer supported by CRM 2011 – those being Internet Explorer versions 7, 8, and 9.

      Like

  3. Dave H May 11, 2012 / 1:29 pm

    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…

    Like

  4. Dave H May 11, 2012 / 1:31 pm

    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.

    Like

  5. Dave October 29, 2012 / 11:28 am

    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();

    Like

    • Hosk October 29, 2012 / 12:15 pm

      is the xml you are injecting into the fetchXml correct? perhaps it’s returning nothing.

      Like

      • Dave October 29, 2012 / 12:21 pm

        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

        Like

  6. Hosk October 29, 2012 / 2:54 pm

    good work, perseverance paid off. Thanks for adding the solution to the comments, hopefully you will help other people who experience this problem.

    Like

  7. Dave November 6, 2012 / 2:06 pm

    Do you know if it’s possible to do the same for layoutXML on the grid?

    Like

    • Hosk November 6, 2012 / 2:14 pm

      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

      Like

  8. david December 5, 2012 / 7:36 pm

    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?

    Like

    • Hosk December 6, 2012 / 9:19 am

      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.

      Like

  9. HG December 30, 2014 / 2:18 pm

    setParameter(“fetchXml”, fetchXml); doesn’t work with online 2013.

    Like

  10. HG December 31, 2014 / 12:51 pm

    I even tried changing the casing of the function name to SetParameter..as suggested in other posts…but it doesn’t work.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.