locked
CRM 2011: FetchXML in JS RRS feed

  • Question

  •  

    Hi,

    I've a JS file which replace existing lookup with custom lookup... Below is the code which is generated through Stunnware and returns results in "Result View" tab on stunnware...

     

    function createLookup() {
    debugger;
    // Set parameters values needed for the creation of a new lookup view...
    var viewId = Xrm.Page.data.entity.getId(); // Using the same GUID, the viewId only has to be unique within each lookup's set of views
    var entityName = "account";
    var viewDisplayName = "Quote Suppliers";
    var viewIsDefault = true;

    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
    "<entity name='account'>" +
    " <attribute name='accountid' />" +
    " <attribute name='accountnumber' />" +
    " <attribute name='customertypecode' />" +
    " <attribute name='name' />" +
    " <link-entity name='new_product' from='new_supplier' to='accountid'>" +
    " <link-entity name='new_quoteproduct' from='new_product' to='new_productid'>" +
    " <filter>" +
    " <condition attribute='new_quote' operator='eq' value='{<Quote GUID>}' />" +
    " </filter>" +
    " </link-entity>" +
    " </link-entity>" +
    "</entity>" +
    "</fetch>"
    ;

    // Define the appearance of your new lookup view
    // No changes required here compared to the above, the lookups should have the same appearance
    var layoutXml = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
    "<row name='result' id='accountid'>" +
    "<cell name='name' width='200' />" +
    "<cell name='customertypecode' width='150' />" +
    "</row>" +
    "</grid>";

    // Add your new view to the Lookup's set of availabe views and make it the default view
    // The supplier field is specified here, we want to add this view to that lookup field
    Xrm.Page.getControl("new_supplier").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
    }


     

     

    Whereas I am getting error in CRM 2011 as "The XML passed to the platform is not well-formed XML."... Any suggestions?

     

    BTW, exception log is:-

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Invalid XML.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
    <ErrorCode>-2147220991</ErrorCode>
    <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>Invalid XML.</Message>
    <Timestamp>2011-04-15T12:23:00.9115178Z</Timestamp>
    <InnerFault>
    <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>System.Xml.XmlException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #7BFAEDA5</Message>
    <Timestamp>2011-04-15T12:23:00.9271482Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
    </InnerFault>
    <TraceText i:nil="true" />
    </OrganizationServiceFault>

     
    Original Post
    Monday, April 18, 2011 12:20 PM

Answers

  • Thanks for the reply Jaimie... Fixed the issue myself... Issue is,

    "			 <condition attribute='new_quote' operator='eq' value='{<Quote GUID>}' />" +

    Changed above line like below and it works as expected...

    " <condition attribute='new_quote' operator='eq' uitype='new_quote' uiname='<Quote Name/Text>' value='{<Quote GUID>}' />" +

    Ajai
    Wednesday, April 20, 2011 11:01 AM

All replies

  • hey Ajai.

    Try this if you can.

     

    function createLookup(id) {
     debugger;
     // Set parameters values needed for the creation of a new lookup view...
     var viewId = Xrm.Page.data.entity.getId(); // Using the same GUID, the viewId only has to be unique within each lookup's set of views
     var entityName = "account";
     var viewDisplayName = "Quote Suppliers";
     var viewIsDefault = true;
    
     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
     "<entity name='account'>" +
    		" <attribute name='accountid' />" +
    		" <attribute name='accountnumber' />" +
    		" <attribute name='customertypecode' />" +
    		" <attribute name='name' />" +
    		" <link-entity name='new_product' from='new_supplier' to='accountid'>" + 
    		"	 <link-entity name='new_quoteproduct' from='new_product' to='new_productid'>" +
    		"		 <filter>" +
    		"			 <condition attribute='new_quote' operator='eq' value='" + id + "' />" +
    		"		 </filter>" +
    		"	 </link-entity>" +
    		" </link-entity>" +
    	 "</entity>" +   "</fetch>";
    
     // Define the appearance of your new lookup view
     // No changes required here compared to the above, the lookups should have the same appearance
     var layoutXml = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
     "<row name='result' id='accountid'>" +
     "<cell name='name' width='200' />" +
     "<cell name='customertypecode' width='150' />" +
     "</row>" +
     "</grid>";
    
     // Add your new view to the Lookup's set of availabe views and make it the default view
     // The supplier field is specified here, we want to add this view to that lookup field
     Xrm.Page.getControl("new_supplier").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
    }
    
    
    • Proposed as answer by Jaimie_J Tuesday, April 19, 2011 1:24 AM
    • Unproposed as answer by Ajaikumar Ramamoorthy Tuesday, April 19, 2011 6:45 AM
    • Proposed as answer by Jan AS Friday, August 12, 2011 2:52 PM
    Tuesday, April 19, 2011 12:17 AM
  • Thanks for the reply Jaimie... However, difference between originally posed and your code is, you added id as parameter and added it to fetchXML... Sorry, no much difference and it DOES NOT work (even hardcoded value does not work and so the passing value right?)...

    Ajai
    Tuesday, April 19, 2011 6:49 AM
  • hey Ajai.

    I am not sure why your code fails, but something similar to the code below is the one i used. It works for me. Be sure that there are some settings you do need to enable to make custom view work in crm 2011.

    function ChangeContactLookupView(isOnline) {
    
      //Note: in the form designer make sure the lookup field is set to "Show all views" in its "View Selector" property 
    
      //Set parameters values needed for the creation of a new lookup view...
    
      var viewId = Xrm.Page.context.getUserId();   // Your new lookup views needs a unique id. It must be a GUID. Here I use the GUID of the current user id.
    
      var entityName = 'contact';           // The entity your new lookup view relates to
    
      var viewDisplayName = "Online contact";  // A name for new lookup view
    
      var viewIsDefault = true;            // Whether your new lookup view should be the default view displayed in the lookup or not
    
      //Define the Fetch XML query your new lookup view will use. You can create this via Advanced Find. You'll need to massage the syntax a little though
    
      var fetchXml =
    
           "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
           "<entity name='contact'>" +
           "<attribute name='firstname' />" +
           "<attribute name='lastname' />" +
           "<attribute name='new_online' />" +
           "<filter type='and'>" +
           "<condition attribute='new_online' operator='eq' value='" + isOnline + "' />" +
           "</filter>" +
           "<order attribute='firstname' decending='false' />" +
           "</entity>" +
           "</fetch>";
    
      //Define the appearance of your new lookup view
    
      var layoutXml =
    
              "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
              "<row name='result' id='contactid'>" + // id = the GUID field from your view that the lookup should return to the CRM form
              "<cell name='firstname' width='100' />" +
              "<cell name='lastname' width='100' />" +
              "<cell name='new_online' width='100' />" +
              "</row>" +
              "</grid>";
    
      //Add your new view to the Lookup's set of availabe views and make it the default view
    
      Xrm.Page.getControl("new_contactid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
    
    }
    

    • Proposed as answer by Jaimie_J Tuesday, April 19, 2011 7:18 AM
    • Unproposed as answer by Ajaikumar Ramamoorthy Wednesday, April 20, 2011 10:57 AM
    Tuesday, April 19, 2011 7:16 AM
  • Thanks for the reply Jaimie... Fixed the issue myself... Issue is,

    "			 <condition attribute='new_quote' operator='eq' value='{<Quote GUID>}' />" +

    Changed above line like below and it works as expected...

    " <condition attribute='new_quote' operator='eq' uitype='new_quote' uiname='<Quote Name/Text>' value='{<Quote GUID>}' />" +

    Ajai
    Wednesday, April 20, 2011 11:01 AM
  • Hi,

    a great tool for this is the XRM Toolbox from Tanguy.

    https://xrmtoolbox.codeplex.com/releases/view/103974

    With XML Validator you can validate and check the result of the fetch xml.


    AM

    Wednesday, April 3, 2013 8:28 AM