locked
Trying to create a record using soap logger crm 2011? RRS feed

  • Question

  • Unable to create a record using soap Logger.i'm calling this soap onLoad Event. Here is My Code. where did i'm wrong.could anybody help me on this.

        var request = ["<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>",
      "<s:Body>",
    
        "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",
    
          "<request i:type='a:CreateRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>",
    
            "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
    
              "<a:KeyValuePairOfstringanyType>",
                "<b:key>Target</b:key>",
                "<b:value i:type='a:Entity'>",
                  "<a:Attributes>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>name</b:key>",
                      "<b:value i:type='c:string' xmlns:c='TEST1http://www.w3.org/2001/XMLSchema'>TEST1</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>address1_city</b:key>",
                      "<b:value i:type='c:string' xmlns:c='NewYorkhttp://www.w3.org/2001/XMLSchema'>NewYork</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                  "</a:Attributes>",
                  "<a:EntityState i:nil='true'/>",
                  "<a:FormattedValues/>",
                  "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>",
                  "<a:LogicalName>account</a:LogicalName>",
                  "<a:RelatedEntities />",
                "</b:value>",
              "</a:KeyValuePairOfstringanyType>",
            "</a:Parameters>",
            "<a:RequestId i:nil='true' />",
            "<a:RequestName>Create</a:RequestName>",
          "</request>",
        "</Execute>",
      "</s:Body>",
    "</s:Envelope>"];
    
    
        var req = new XMLHttpRequest();
        req.open("POST", "http://mscrmserver2011:5555/Retail/XRMServices/2011/Organization.svc/web", true);
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.send(request);


    hsk srinivas


    • Edited by Ppandu Tuesday, January 6, 2015 9:49 AM whole code
    Monday, December 15, 2014 11:45 AM

Answers

  • I tested your code and it works on my CRM 2013 Online trial:

    var request = ["<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>",
      "<s:Body>",
        "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",
          "<request i:type='a:CreateRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>",
            "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
              "<a:KeyValuePairOfstringanyType>",
                "<b:key>Target</b:key>",
                "<b:value i:type='a:Entity'>",
                  "<a:Attributes>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>name</b:key>",
                      "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>TEST3</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>address1_city</b:key>",
                      "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>NewYork3</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                  "</a:Attributes>",
                  "<a:EntityState i:nil='true'/>",
                  "<a:FormattedValues/>",
                  "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>",
                  "<a:LogicalName>account</a:LogicalName>",
                  "<a:RelatedEntities />",
                "</b:value>",
              "</a:KeyValuePairOfstringanyType>",
            "</a:Parameters>",
            "<a:RequestId i:nil='true' />",
            "<a:RequestName>Create</a:RequestName>",
          "</request>",
        "</Execute>",
      "</s:Body>",
    "</s:Envelope>"];
    
    
    var soapEndpoint = Xrm.Page.context.getServerUrl();
    if (soapEndpoint.match(/\/$/)) { soapEndpoint = soapEndpoint.substring(0, soapEndpoint.length - 1); } 
    soapEndpoint += "/XRMServices/2011/Organization.svc/web";
    
    var req = new XMLHttpRequest();
    req.open("POST", soapEndpoint, true);
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    var totalrequest = request.join('');
    req.send(totalrequest);

    the only thing different in my code is the soap endpoint url that is dynamic (and not static as in your code)


    My blog: www.crmanswers.net - Rockstar 365 Profile


    • Edited by Guido PreiteMVP Monday, December 15, 2014 2:07 PM
    • Marked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    • Unmarked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    • Marked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    Monday, December 15, 2014 2:06 PM

All replies

  • these two lines

    "<b:value i:type='c:string' xmlns:c='TEST1http://www.w3.org/2001/XMLSchema'>TEST1</b:value>"
    "<b:value i:type='c:string' xmlns:c='NewYorkhttp://www.w3.org/2001/XMLSchema'>NewYork</b:value>"

    have the values also inside the xmlns:c property, and you are creating the request object as an array, you need to make it a single string before do the req.send(request)


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, December 15, 2014 11:52 AM
  • i changed like this and i checked it with debugging no error but record not creating... ..is there anyotherthing wentwrong.

    "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>TEST1</b:value>"
    "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>NewYork</b:value>"


    hsk srinivas

    Monday, December 15, 2014 12:18 PM
  • as I wrote before you need to make the request object to a single string, something like

    var totalrequest = request.join('');
    req.send(totalrequest);


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, December 15, 2014 12:33 PM
  • i done that above changes in my code but it is not creating a record.is there anything went wrong.

    hsk srinivas

    Monday, December 15, 2014 1:09 PM
  • I tested your code and it works on my CRM 2013 Online trial:

    var request = ["<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>",
      "<s:Body>",
        "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>",
          "<request i:type='a:CreateRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>",
            "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
              "<a:KeyValuePairOfstringanyType>",
                "<b:key>Target</b:key>",
                "<b:value i:type='a:Entity'>",
                  "<a:Attributes>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>name</b:key>",
                      "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>TEST3</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                    "<a:KeyValuePairOfstringanyType>",
                      "<b:key>address1_city</b:key>",
                      "<b:value i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>NewYork3</b:value>",
                    "</a:KeyValuePairOfstringanyType>",
                  "</a:Attributes>",
                  "<a:EntityState i:nil='true'/>",
                  "<a:FormattedValues/>",
                  "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>",
                  "<a:LogicalName>account</a:LogicalName>",
                  "<a:RelatedEntities />",
                "</b:value>",
              "</a:KeyValuePairOfstringanyType>",
            "</a:Parameters>",
            "<a:RequestId i:nil='true' />",
            "<a:RequestName>Create</a:RequestName>",
          "</request>",
        "</Execute>",
      "</s:Body>",
    "</s:Envelope>"];
    
    
    var soapEndpoint = Xrm.Page.context.getServerUrl();
    if (soapEndpoint.match(/\/$/)) { soapEndpoint = soapEndpoint.substring(0, soapEndpoint.length - 1); } 
    soapEndpoint += "/XRMServices/2011/Organization.svc/web";
    
    var req = new XMLHttpRequest();
    req.open("POST", soapEndpoint, true);
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    var totalrequest = request.join('');
    req.send(totalrequest);

    the only thing different in my code is the soap endpoint url that is dynamic (and not static as in your code)


    My blog: www.crmanswers.net - Rockstar 365 Profile


    • Edited by Guido PreiteMVP Monday, December 15, 2014 2:07 PM
    • Marked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    • Unmarked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    • Marked as answer by Ppandu Tuesday, December 16, 2014 6:10 AM
    Monday, December 15, 2014 2:06 PM
  • change

    Xrm.Page.context.getServerUrl()

    to

    Xrm. Page.comtext.getClientUrl()


    Regards Faisal

    Monday, December 15, 2014 4:42 PM
  • change

    Xrm.Page.context.getServerUrl()

    to

    Xrm. Page.comtext.getClientUrl()


    Regards Faisal

    he has CRM 2011, getClientUrl has been introduced with rollup12

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, December 15, 2014 4:53 PM
  • its working fine.Thank you very much. I appreciate your answer. the problem is soapendpoint.

    hsk srinivas

    Tuesday, December 16, 2014 6:13 AM