locked
JavaScript and FetchXML RRS feed

  • Question

  • Hi,

    in my project i have 2 files :

    1- Opportunity.JS contains 3 js functions

    function CRMRequest(FiscalYear)
    {
    
        var FetchXmlRequest = '<fetch distinct="false" mapping="logical" aggregate="true" output-format="xml-platform" >';
        FetchXmlRequest += '<entity name="opportunity" >';
        FetchXmlRequest += '<attribute name="actualvalue" alias="opportunity_sum" aggregate="sum" />';
        FetchXmlRequest += '<attribute name="createdbyname" alias="Commercial" groupby="true" />';
        FetchXmlRequest += '<attribute name="actualclosedate" groupby="true" dategrouping="quarter" alias="Trimestre" />';
        FetchXmlRequest += '<filter type="and" >';
        FetchXmlRequest += '<condition attribute="statecode" operator="eq" value="1" />';
        FetchXmlRequest += '</filter>';
        FetchXmlRequest += '<filter type="and" >';
        FetchXmlRequest += '<condition attribute="createdon" operator="in-fiscal-year" value="'+ FiscalYear +'" />';
        FetchXmlRequest += '</filter>';
        FetchXmlRequest += '</entity>';
        FetchXmlRequest += '</fetch>';
    
        return FetchXmlRequest;
    }
    
    function PrepareSOAPEnvelop(Request)
    {
        var Xml = "<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\">"
        Xml += GenerateAuthenticationHeader()
        Xml += "<soap:Body>";
        Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">";
        Xml += "<fetchXml>";
        Xml += _HtmlEncode(Request);
        Xml += "</fetchXml>";
        Xml += "</Fetch>";
        Xml += "</soap:Body>";
        Xml += "</soap:Envelope>";
    
        return Xml;
    }
    
    function PrepareExecuteXmlHttpRequest(Xml,ServerAddress,port)
    {
        var XmlHttp = CreateXmlHttp();
        XmlHttp.open("POST", 'http://' + SERVER + ':' + port + '/mscrmservices/2007/crmservice.asmx', false);
        XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
        XmlHttp.send(Xml);
        var resultDoc = loadXmlDocument(XmlHttp.responseXML.text);
        return resultDoc;
    
       
    }
    
    
    

    2- DataTableView.html contains dataGrid component

    my problem :>

    - i would like to verify 3 JavaScript functions in Opportunity.js and how to insert the result of query in my dataGrid.

    thanks for all.


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, April 3, 2015 10:15 AM

Answers

  • Hi Ammar,

    Try XrmServiceToolkit. I always use this.

    https://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions

    First download the toolkit library and add to CRM as Web resource. After that you can refer it or add to any form and use it.

          var fetchXml =
                    "<fetch mapping='logical' aggregate='true'>" +
                       "<entity name='contact'>" +
                          "<attribute name='contactid' aggregate='count' alias='count' />" +
                          "<filter>" +
                             "<condition attribute='contactid' operator='eq' value='" + contactId + "' />" +
                          "</filter>" +
                       "</entity>" +
                    "</fetch>";
    
                var fetchedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);    

    Thanks!

    Kalim Khan

    • Edited by Kalim Khan Friday, April 3, 2015 10:52 AM
    • Marked as answer by Ammar.Zaied Friday, April 3, 2015 1:47 PM
    Friday, April 3, 2015 10:52 AM

All replies

  • Hello,

    I recommend using SDK.REST.js for these types of operations. You can find samples and the library here.


    Cornel Croitoriu - Senior Software Developer

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    Friday, April 3, 2015 10:22 AM
  • can use fetchXML with SDK.REST.js ?

    i need tutorial to use fetchXML with SDK.REST.js, thanks.


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, April 3, 2015 10:43 AM
  • Hi Ammar,

    Try XrmServiceToolkit. I always use this.

    https://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions

    First download the toolkit library and add to CRM as Web resource. After that you can refer it or add to any form and use it.

          var fetchXml =
                    "<fetch mapping='logical' aggregate='true'>" +
                       "<entity name='contact'>" +
                          "<attribute name='contactid' aggregate='count' alias='count' />" +
                          "<filter>" +
                             "<condition attribute='contactid' operator='eq' value='" + contactId + "' />" +
                          "</filter>" +
                       "</entity>" +
                    "</fetch>";
    
                var fetchedContacts = XrmServiceToolkit.Soap.Fetch(fetchXml);    

    Thanks!

    Kalim Khan

    • Edited by Kalim Khan Friday, April 3, 2015 10:52 AM
    • Marked as answer by Ammar.Zaied Friday, April 3, 2015 1:47 PM
    Friday, April 3, 2015 10:52 AM
  • thanks Kalim,

    i download the XRMToolkit,now i uploading files as web resource, just i need to know how to insert the result of fetchedOpportunity to my DataGrid ?

    thanks.


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, April 3, 2015 11:04 AM
  • thanks, work !

    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Friday, April 3, 2015 1:47 PM