Ajax API_AddRecord for parent and children: children returning error [fixed and answered]

  • 0
  • 1
  • Question
  • Updated 2 years ago
  • Answered

*note I am not using javascript:void(copyMasterDetailButtonHandler('&relfids=192,194,250&copyFid=327&destrid=0&sourcerid=" & [Quote ID#] & "','"& Dbid()&"')) because we having a looping relationship that I am simply not adding to this ajax.

this will be a lot of code, in a nut shell I made a button on the parent record that calls a script when clicked and changes the text of the button to its current state as it goes through the process of get/post/get/post 
however I am having an error that does not make any sense 
"<?xml version="1.0" ?> <qdbapi> <action>API_AddRecord</action> <errcode>11</errcode> <errtext>Could not parse XML input</errtext> <errdetail>XML Parsing Error. mismatched tag at line 1 column 21 (which is byte 21)</errdetail> </qdbapi> "

this does not make sense to me as "/" is the 21st from 
<qdbapi><f id='6'>1</field>...</qdbapi>


the kicker is the parent shares the same format but does not throw any error AND goes through for the copy

Sanitized code:

(function() {

  //change color

  var process

  $('#userButtonCopy').off('click');

  $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");

  $('#userButtonCopy').removeClass('Primary').addClass('Alert');

  var dots = window.setInterval(function() {

    var wait = document.getElementById("wait");

    if (wait.innerHTML.length > 3)

      wait.innerHTML = "";

    else

      wait.innerHTML += ".";

  }, 400);

  //get xml

  var rid = kRid || ""; //kRid is the qb current record id for the one you are viewing

  var url = "";

  var childrid = ""; //technically is related parent 

  var children = "";

  var rdr = "";

  var count = 0;

  url += "-Removed-";

  url += "?act=API_DoQuery";

  url += "&clist=233.168.272.260.247.192.194.162.250.18.20.80.24.83.7.244.16.9.248.237.232.21.286.13.245.191.74.17.261.52.6.3.11.12.204.186.155.200.202.14.81.94.19.15.167.25.10.246.249.85.255&opts=nos.ned.";

  url += "&dvqid=66&query=({%273%27.EX.%27" + rid + "%27})";

  url += "&apptoken=-Removed-";

  url += "&fmt=structured";


  $.get(url, function(xml) {

    process = "getting data";

    $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");

    var promises = [];

    $("original", xml).each(function() {

      rdr = $("next_record_id", this).text();

    });

    $("record", xml).each(function() {


      childrid = $("f#3", this).text(); 

      children = $("f#255", this).text();



      var xhr = new XMLHttpRequest();

      xhr.open("GET", url, true);

      xhr.onreadystatechange = function() {

        if (xhr.readyState == 4 && xhr.status == 200) {

          // defensive check

          if (typeof callback == "function") {

            // apply() sets the meaning of "this" in the callback

            callback.apply(xhr);

          }

        }

      }


      var req = "";

      req += "<qdbapi>";

      req += `<field fid='233'>` + $("f#233", this).text() + `</field>`;

      req += `<field fid='168'>` + $("f#168", this).text() + `</field>`;

      req += `<field fid='272'>` + $("f#272", this).text() + `</field>`;

      req += `<field fid='260'>` + $("f#260", this).text() + `</field>`;

      req += `<field fid='247'>` + $("f#247", this).text() + `</field>`;

      req += `<field fid='162'>` + $("f#162", this).text() + `</field>`;

      req += `<field fid='18'>` + $("f#18", this).text() + `</field>`;

      req += `<field fid='20'>` + $("f#20", this).text() + `</field>`;

      req += `<field fid='80'>` + $("f#80", this).text() + `</field>`;

      req += `<field fid='24'>` + $("f#24", this).text() + `</field>`;

      req += `<field fid='83'>` + $("f#83", this).text() + `</field>`;

      req += `<field fid='7'>` + $("f#7", this).text() + `</field>`;

      req += `<field fid='244'>` + $("f#244", this).text() + `</field>`;

      req += `<field fid='16'>` + $("f#16", this).text() + `</field>`;

      req += `<field fid='9'>` + $("f#9", this).text() + `</field>`;

      req += `<field fid='248'>` + $("f#248", this).text() + `</field>`;

      req += `<field fid='237'>` + $("f#237", this).text() + `</field>`;

      req += `<field fid='232'>` + $("f#232", this).text() + `</field>`;

      req += `<field fid='21'>` + $("f#21", this).text() + `</field>`;

      req += `<field fid='286'>` + $("f#286", this).text() + `</field>`;

      req += `<field fid='13'>` + $("f#13", this).text() + `</field>`;

      req += `<field fid='245'>` + $("f#245", this).text() + `</field>`;

      req += `<field fid='191'>` + $("f#191", this).text() + `</field>`;

      req += `<field fid='74'>` + $("f#74", this).text() + `</field>`;

      req += `<field fid='17'>` + $("f#17", this).text() + `</field>`;

      req += `<field fid='261'>` + $("f#261", this).text() + `</field>`;

      req += `<field fid='52'>` + $("f#52", this).text() + `</field>`;

      req += `<field fid='6'>` + $("f#6", this).text() + `</field>`;

      req += `<field fid='11'>` + $("f#11", this).text() + `</field>`;

      req += `<field fid='12'>` + $("f#12", this).text() + `</field>`;

      req += `<field fid='204'>` + $("f#204", this).text() + `</field>`;

      req += `<field fid='186'>` + $("f#186", this).text() + `</field>`;

      req += `<field fid='155'>` + $("f#155", this).text() + `</field>`;

      req += `<field fid='200'>` + $("f#200", this).text() + `</field>`;

      req += `<field fid='202'>` + $("f#202", this).text() + `</field>`;

      req += `<field fid='14'>` + $("f#14", this).text() + `</field>`;

      req += `<field fid='81'>` + $("f#81", this).text() + `</field>`;

      req += `<field fid='94'>` + $("f#94", this).text() + `</field>`;

      req += `<field fid='19'>` + $("f#19", this).text() + `</field>`;

      req += `<field fid='15'>` + $("f#15", this).text() + `</field>`;

      req += `<field fid='167'>` + $("f#167", this).text() + `</field>`;

      req += `<field fid='25'>` + $("f#25", this).text() + `</field>`;

      req += `<field fid='10'>` + $("f#10", this).text() + `</field>`;

      req += `<field fid='246'>` + $("f#246", this).text() + `</field>`;

      req += `<field fid='249'>` + $("f#249", this).text() + `</field>`;

      req += `<field fid='85'>` + $("f#85", this).text() + `</field>`;

      req += "<field fid='327'>copied</field>";

      req += "</qdbapi>";

      console.log(req);


      promises.push(

        $.ajax({

          type: "POST",

          contentType: "text/xml",

          dataType: "xml",

          processData: false,

          url: "-Removed-",

          data: req,

          success: function(responce) {

            $('#userButtonCopy').html("Please wait(" + responce + ")<span id='wait'>.</span>");

          }

        })

      );

      console.log(promises);

    });

    if (children > 0) {

      process = "getting build (" + children + ") data";

      $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");


      //get xml


      var url = "";

      url += "-Removed-";

      url += "?act=API_DoQuery";

      url += "&clist=108.6.7.8.9.10.11.111.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.136.31.32.34.37.62.52.42.48.49.83.84.85.86.87.88.89.90.92.93.95.96.98.99.137&opts=nos.";

      url += "&dvqid=30&query=({%27108%27.EX.%27" + childrid + "%27})";

      url += "&apptoken=-Removed-";

      url += "&fmt=structured";


      $.get(url, function(xml) {

        var promises = [];

        $("record", xml).each(function() {

          process = "Getting build (" + children + ") data";

          $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");

          var xhr = new XMLHttpRequest();

          xhr.open("GET", url, true);

          xhr.onreadystatechange = function() {

            if (xhr.readyState == 4 && xhr.status == 200) {

              // defensive check

              if (typeof callback == "function") {

                // apply() sets the meaning of "this" in the callback

                callback.apply(xhr);

              }

            }

          }


          var req = "";

          req += "<qdbapi>";

          req += `<f id='6'>` + $("f#6", this).text() + `</field>`;

          req += `<f id='7'>` + $("f#7", this).text() + `</field>`;

          req += `<f id='8'>` + $("f#8", this).text() + `</field>`;

          req += `<f id='9'>` + $("f#9", this).text() + `</field>`;

          req += `<f id='10'>` + $("f#10", this).text() + `</field>`;

          req += `<f id='11'>` + $("f#11", this).text() + `</field>`;

          req += `<f id='111'>` + $("f#111", this).text() + `</field>`;

          req += `<f id='12'>` + $("f#12", this).text() + `</field>`;

          req += `<f id='13'>` + $("f#13", this).text() + `</field>`;

          req += `<f id='14'>` + $("f#14", this).text() + `</field>`;

          req += `<f id='15'>` + $("f#15", this).text() + `</field>`;

          req += `<f id='16'>` + $("f#16", this).text() + `</field>`;

          req += `<f id='17'>` + $("f#17", this).text() + `</field>`;

          req += `<f id='18'>` + $("f#18", this).text() + `</field>`;

          req += `<f id='19'>` + $("f#19", this).text() + `</field>`;

          req += `<f id='20'>` + $("f#20", this).text() + `</field>`;

          req += `<f id='21'>` + $("f#21", this).text() + `</field>`;

          req += `<f id='22'>` + $("f#22", this).text() + `</field>`;

          req += `<f id='23'>` + $("f#23", this).text() + `</field>`;

          req += `<f id='24'>` + $("f#24", this).text() + `</field>`;

          req += `<f id='25'>` + $("f#25", this).text() + `</field>`;

          req += `<f id='26'>` + $("f#26", this).text() + `</field>`;

          req += `<f id='27'>` + $("f#27", this).text() + `</field>`;

          req += `<f id='28'>` + $("f#1", this).text() + `</field>`;

          req += `<f id='136'>` + $("f#136", this).text() + `</field>`;

          req += `<f id='31'>` + $("f#31", this).text() + `</field>`;

          req += `<f id='32'>` + $("f#32", this).text() + `</field>`;

          req += `<f id='34'>` + $("f#34", this).text() + `</field>`;

          req += `<f id='37'>` + $("f#37", this).text() + `</field>`;

          req += `<f id='62'>` + $("f#62", this).text() + `</field>`;

          req += `<f id='52'>` + $("f#52", this).text() + `</field>`;

          req += `<f id='42'>` + $("f#42", this).text() + `</field>`;

          req += `<f id='48'>` + $("f#48", this).text() + `</field>`;

          req += `<f id='49'>` + $("f#49", this).text() + `</field>`;

          req += `<f id='83'>` + $("f#83", this).text() + `</field>`;

&nbsp         req += `<f id='84'>` + $("f#84", this).text() + `</field>`;

          req += `<f id='85'>` + $("f#85", this).text() + `</field>`;

          req += `<f id='86'>` + $("f#86", this).text() + `</field>`;

          req += `<f id='87'>` + $("f#87", this).text() + `</field>`;

          req += `<f id='88'>` + $("f#88", this).text() + `</field>`;

          req += `<f id='89'>` + $("f#89", this).text() + `</field>`;

          req += `<f id='90'>` + $("f#90", this).text() + `</field>`;

          req += `<f id='92'>` + $("f#92", this).text() + `</field>`;

          req += `<f id='93'>` + $("f#93", this).text() + `</field>`;

          req += `<f id='95'>` + $("f#95", this).text() + `</field>`;

          req += `<f id='96'>` + $("f#96", this).text() + `</field>`;

          req += `<f id='98'>` + $("f#98", this).text() + `</field>`;

          req += `<f id='99'>` + $("f#99", this).text() + `</field>`;

          req += `<f id='137'>` + $("f#137", this).text() + `</field>`;

          req += `<f id='108'>` + rdr + `</field>`;

          req += "</qdbapi>";

          console.log(req);


          setTimeout(function() {

            promises.push(


              $.ajax({

                type: "POST",

                contentType: "text/xml",

                dataType: "xml",

                processData: false,

                url: "-Removed-,

                data: req,

                success: function(responce) {

                  console.log(promises);

                  $('#userButtonCopy').html("Please wait(" + responce + ")<span id='wait'>.</span>");

                }

              })


            );


            children--;

            console.log(count);

          }, 2000);


        });

        count = promises.length;

        console.log(count);


      });

      process = "Posting build" + children;

      $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");

    } else {

      process = "Waiting on post to finish ";

      $('#userButtonCopy').html("Please wait(" + process + ")<span id='wait'>.</span>");

      //redirect

      $('#userButtonCopy').html("Go to and edit Cloned Record");

      var str = window.location.href;

      str = str.split('?')[0];

      str += "?a=er&rid=" + rdr;

      $('#userButtonCopy').attr('href', str);

      setTimeout(function() {

        window.location.href = str;

      }, 20000);

    }

  });

})();




Photo of Robert

Robert

  • 30 Points

Posted 2 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,004 Points 20k badge 2x thumb
tl;dr

The most likely explanation is that you have unescaped characters in some of your filelds which when concatenated into the XML body of the request produce invalid XML.

Don't take offense but other than that you code is a mess of different paradigms and I am not sure which to comment on first.
Photo of Robert

Robert

  • 30 Points
Yeah I was too involved with the ajax responses that I failed to see that I missed a step when getting the proper format. code is updated to what works now.