Quantcast
Channel: CRM Development 論壇
Viewing all articles
Browse latest Browse all 10280

Changing the data in a subgrid (Crm 2011).

$
0
0

Greetings,

 

I have a problem where I need to change the results of a subgrid, here is my scenarion.

On the appointment entity form I added a subgrid (Just a random subgrid to a random entity), what I need to do is change the fetchxml for this grid to read new data (trying to imitate contacts list from the account form if an account is selected in the appointment otherwise just hide the grid.)

Currently the Tab that the grid is on is not visible and is only shown when I verify that the regarding object is an account.

I found the code below from James at http://crmgreenbible.blogspot.com/2011/07/crm-2011-change-subgrid-fetchxml.html

But the problem is it is not working for me,   var leadGrid = document.getElementById("GRIDNAME"); gives me null (I did replace the datagrid name with the correct grid name).

The following code worked for me to get the grid:
var leadGrid = Xrm.Page.ui.controls.get("GRIDNAME");

But then the "setParameter", "refresh" and "readyState" commands don't work...

 

/*******************************************************************************************************\

With a bit of, unsupported, scripting I managed to work around this issue. Here's how:

1. Add a subgrid to the Lead form showing all leads. Take a note of the subgrid name.

2. Generate the FetchXML that you want to inject into the subgrid (Advanced Find is your friend here).

3. The following JScript method demonstrates changing the FetchXml for a subgrid called "RelatedLeads"


function UpdateSubGrid()
{
var leadGrid = document.getElementById("RelatedLeads");

//If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding
if (leadGrid.readyState != "complete")
{
//The subgrid hasn't loaded, wait 1 second and then try again
setTimeout('UpdateLeadSubGrid()', 1000);
return;
}

//Update the fetchXML that will be used by the grid.
var fetchXml = "";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += " ";
fetchXml += "";

//Inject the new fetchXml
leadGrid.control.setParameter("fetchXml", fetchXml);
//Force the subgrid to refresh
leadGrid.control.refresh();
}
/***********************************************************************************************************\
Following is the complete code I made, modified for my needs.
function SetSubGridInfo()
{
  var accountid = GetAccountNumber();
  var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
  "  <entity name='contact'>"+
  "    <attribute name='fullname' />"+
  "    <attribute name='parentcustomerid' />"+
  "    <attribute name='telephone1' />"+
  "    <attribute name='emailaddress1' />"+
  "    <attribute name='ownerid' />"+
  "    <attribute name='contactid' />"+
  "    <order attribute='fullname' descending='false' />"+
  "    <filter type='and'>"+
  "      <condition attribute='statecode' operator='eq' value='0' />"+
  "      <condition attribute='ownerid' operator='eq-userid' />"+
  "      <condition attribute='parentcustomerid' operator='eq' uitype='account' value='"+accountid+"' />"+
  "    </filter>"+
  "  </entity>"+
  "</fetch>";
  UpdateSubGrid("account_contacts", fetchXml);
}
function UpdateSubGrid(gridIdName, fetchXml)

  var leadGrid = document.getElementById(gridIdName); 
 
  //If this method is called from the form OnLoad, make sure that the grid is loaded before proceeding 
  if (leadGrid.readyState != "complete") 
  {     
    //The subgrid hasn't loaded, wait 1 second and then try again     
 setTimeout('UpdateSubGrid('+gridIdName+','+fetchXml+')', 1000);     
 return; 
  } 
 
  //Inject the new fetchXml 
  leadGrid.control.setParameter("fetchXml", fetchXml); 
 
  //Force the subgrid to refresh 
  leadGrid.control.refresh();
 
  setVisibleTabSection("account_tab", null, true);
}
function setVisibleTabSection(tabname, sectionname, show) 
{
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab != null) {
        if (sectionname == null)
            tab.setVisible(show);
        else {
            var section = tab.sections.get(sectionname);
            if (section != null) {
                section.setVisible(show);
                if (show)
                    tab.setVisible(show);
            }
        }
    }
}
function GetAccountNumber()
{
  // This will get the lookup for the attribute
 var lookupItem = Xrm.Page.getAttribute("regardingobjectid").getValue();
 //Check if the lookup found anything
 if (lookupItem != null)
 {
    // If there is data in the field, show it in a series of alerts
    if (lookupItem[0] != null &&  lookupItem[0].typename == "account")
    {
      //Create the XML send request
      var xml = "" +
      "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
      "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
      "  <soap:Header>" +
      "    <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
      "      <AuthenticationType xmlns=\"0http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" +
      "      <OrganizationName xmlns=\""+ORG_UNIQUE_NAME+"http://schemas.microsoft.com/crm/2007/CoreTypes\">"+ORG_UNIQUE_NAME+"</OrganizationName>" +
      "      <CallerId xmlns=\"00000000-0000-0000-0000-000000000000http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" +
      "    </CrmAuthenticationToken>" +
      "  </soap:Header>" +
      "  <soap:Body>" +
      "    <Retrieve xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
      "        <entityName>account</entityName>" +
      "        <id>" + lookupItem[0].id + "</id>"+
      "        <columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type=\"q1:ColumnSet\">" +
      "          <q1:Attributes>" +
      "            <q1:Attribute>accountid</q1:Attribute>" + //Here you need to set the columns you intend to fetch
      "            <q1:Attribute>name</q1:Attribute>" +
      "            <q1:Attribute>accountnumber</q1:Attribute>" + 
      "            <q1:Attribute>ownerid</q1:Attribute>" +
      "            <q1:Attribute>telephone1</q1:Attribute>" +
      "            <q1:Attribute>telephone2</q1:Attribute>" +
      "            <q1:Attribute>emailaddress1</q1:Attribute>" + 
      "          </q1:Attributes>" +
      "        </columnSet>" +
      "    </Retrieve>" +
      "  </soap:Body>" +
      "</soap:Envelope>" +
      "";  
    var xmlHttpRequestOtherOffers = new ActiveXObject("Msxml2.XMLHTTP");
   
    xmlHttpRequestOtherOffers.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequestOtherOffers.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlHttpRequestOtherOffers.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequestOtherOffers.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequestOtherOffers.send(xml);
   
    //Get response
    var resultXml = xmlHttpRequestOtherOffers.responseXML;
//alert(resultXml.xml);
   
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
      var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
    }
    // Display the retrieved value.
    else
    {
      if(resultXml.selectSingleNode("//q1:accountnumber") != null)
      {
        return resultXml.selectSingleNode("//q1:accountnumber").nodeTypedValue;
      }
    }
    }// if (lookupItem[0] != null) END
  }//if (lookupItem != null) END
 
return null;
}
Any suggestions would be appreciated.

Halldór Jóhannsson

Viewing all articles
Browse latest Browse all 10280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>