Dynamics CRM 2011 Filter Subgrid and Refresh


/ Published in: JavaScript
Save to your folder(s)

Filters a dynamics CRM 2011 subgrid...implementation example included at bottom


Copy this code and paste it in your HTML
  1. if (typeof (EASI) == "undefined")
  2. { EASI = {}; }
  3. EASI._construct = function () {
  4. //filter subgrid and refresh function
  5. this.FilterSubgridandRefresh = function (subgridname, stringArrayOfCondtionsToAdd, removeExistingConditionsBool) {
  6. try {
  7. window.setTimeout(function () {
  8.  
  9. var subgrid = document.getElementById(subgridname);
  10.  
  11. if (subgrid == undefined || subgrid == null)
  12. {
  13. throw "EASI SubGridFetchBuilder...subgrid not ready...contact [email protected]";
  14. }
  15.  
  16. var oldFetch = $("#" + subgridname).find("#effectiveFetchXml").attr("value");
  17. if (oldFetch == undefined) {
  18. //we have to refresh the grid now because it was in a tab that was collapsed so it hasn't run yet
  19. subgrid.control.refresh();
  20. //re-run this operation
  21. setTimeout(function () {
  22. EASI.FilterSubgridandRefresh(subgridname, stringArrayOfCondtionsToAdd, removeExistingConditionsBool);
  23. }, 1000);
  24. }
  25. else {
  26. var html_fetch = $("<div>" + oldFetch + "</div>");
  27. if (removeExistingConditionsBool)//remove the existing conditions
  28. html_fetch.find("condition[operator!='']").remove();
  29. for (var i in stringArrayOfCondtionsToAdd) {
  30. var cond = $(stringArrayOfCondtionsToAdd[i]);
  31. html_fetch.find("filter").append(cond);
  32. }
  33. var newFetch = html_fetch.html();
  34. subgrid.control.SetParameter("fetchXml", newFetch);
  35. subgrid.control.refresh();
  36. }
  37. }, 1000);
  38. }
  39. catch (err) {
  40. alert("Error in subgrid refresh\r\n" + err.message);
  41.  
  42. }
  43. }
  44. //get crm field value function
  45. this.GetFieldValue = function (fieldlogicalname) {
  46. if (typeof (Xrm) == "undefined")
  47. Xrm = window.parent.Xrm;
  48. var atype = Xrm.Page.getControl(fieldlogicalname).getAttribute().getAttributeType();
  49.  
  50. if (atype == this.CRM.type_optionset) {
  51. return this.GetPicklistSelectedValue(fieldlogicalname);
  52. }
  53. else if (atype == this.CRM.type_lookup) {
  54. return this.GetLookupId(fieldlogicalname);
  55. }
  56. else {
  57. return Xrm.Page.getControl(fieldlogicalname).getAttribute().getValue();
  58. }
  59. }
  60.  
  61. }
  62.  
  63. EASI._construct();
  64.  
  65. /*here is an implementation example*/
  66. function filterGrid_Load() {
  67.  
  68. var oid = $("#opportunityid").attr("value");
  69. var configno = EASI.GetFieldValue("easi_configurationno");
  70. FilterSubGrid_OppDetailSubgrid("DoNotRemove_options_subgrid", oid, configno, 101);
  71. function FilterSubGrid_OppDetailSubgrid(subgridname, oppid, configno, lineitemtype) {
  72. try {
  73. var newcond1 = "<condition attribute=\"easi_configurationno\" operator=\"eq\" value=\"" + configno + "\" />";
  74. var newcond2 = "<condition attribute=\"opportunityid\" operator=\"eq\" value=\"" + oppid + "\" />";
  75. var newcond3 = "<condition attribute=\"easi_lineitemtype\" operator=\"eq\" value=\"" + lineitemtype + "\" />";
  76. var newcond4 = "<condition attribute=\"easi_removeoption\" operator=\"ne\" value=\"" + 1 + "\" />";
  77. var condArray = new Array(newcond1, newcond2, newcond3, newcond4);
  78. EASI.FilterSubgridandRefresh(subgridname, condArray, true);
  79. }
  80. catch (err) {
  81. alert("error filtering subgrid: " + err.message);
  82. }
  83. }
  84. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.