Forum Discussion

WilliamMarrone's avatar
WilliamMarrone
Qrew Cadet
8 years ago

API_Edit Record in a Code Page

Hello, 

I am having trouble with my code page in regards to editing a record. 

    for(var i=0; i<response.records.length; i++){

if(response.records["Include in Deposit"] == 1){
var newAmountApplied = (response.records["Amount Applied"]-response.records["Amount Applied"]);
var RecID = response.records["Record ID#"];
console.log(response.records["Record ID#"]);
console.log(newAmountApplied);
var url5 = "Domain";
url5    += "/db/";
url5    += "bnae46589";
url5    += "?a=API_EditRecord";
url5   += "&rid=" + RecID;
url5    += "&apptoken=d4vg82pmsvv9dceu22djsquwtj";
url5    += "&_fid_14=" & newAmountApplied ;
url5    += "&_fid_28="&1;
httpGet(url5);
}



}

All I am trying to do with my code is to clear out a record and check a checkbox if another checkbox is checked. The console is logging the correct record Id and values it is however not editing the records. In the same codepage I am purging and adding records as well and those work fine. Any Ideas?
  • Are you using the QuickBaseClient.js library? If so that is your problem.

    In any event, the your code is not posted is not complete. Post the complete code you are using after formatting it using jsbeautifier.org using 2 space indent and placing it in a <pre> section using the forum toolbar.
  • var src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js";

    require(['moment'], function() {
      moment();
    });

    function httpGet(theUrl) //enables us to 'get' a queried table from quickbase to show on the page
    {
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.open("GET", theUrl, false); // false for synchronous request
      xmlHttp.send(null);
      return xmlHttp.responseText;
    };

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();

    if (dd < 10) {
      dd = '0' + dd
    }

    if (mm < 10) {
      mm = '0' + mm
    }

    today = mm + '/' + dd + '/' + yyyy; //Concatenates the current date.

    var url2 = "Domain";
    url2 += "/db/";
    url2 += "bnae466wz";
    url2 += "?a=API_AddRecord";
    url2 += "&apptoken=d4vg82pmsvv9dceu22djsquwtj";
    url2 += "&_fid_6=" + today + "";

    httpGet(url2); //Creates new Deposit                            

    var url3 = "Domain";
    url3 += "/db/";
    url3 += "bnae466wz";
    url3 += "?a=API_GenResultsTable";
    url3 += "&apptoken=d4vg82pmsvv9dceu22djsquwtj";
    url3 += "&clist=3";
    url3 += "&slist=3";
    url3 += "&options=num-1.sortorder-D";
    url3 += "&jsa=1";

    $.getScript(url3, function() {
      console.log(qdb_data);
    });


    //--------------------------------------------------------------------------------------------------

    var dbid = "bnae47dct";
    var apptoken = "d4vg82pmsvv9dceu22djsquwtj";

    $.ajaxSetup({
      data: {
        apptoken: apptoken
      }
    });

    var XMLStructuredToObj = function(xml, type) {
      var fidToLabel = {};
      $("field", xml).each(function() {
        var id = $(this).attr("id");
        var label = $("label", this).text();
        //console.log(id, label);
        fidToLabel[id] = label;
      });

      var data = [];
      var record = {};
      $("record", xml).each(function() {
        record = {};
        $("f", this).each(function() {
          record[fidToLabel[$(this).attr("id")]] = $(this).text();
        });
        data.push(record);
      });
      return {
        records: data
      };
    };

    var csv_blob = "";
    var clist = "6.7.8.9.10.11.12.13.14.15"; //mapping for import

    var promise = $.ajax({

      url: dbid,
      data: {
        act: "API_DoQuery",
        query: "{'21'.EX.true}",
        clist: "6.7.8.9.10.11.12.13.14.3.21",
        fmt: "structured"
      },
      dataFilter: XMLStructuredToObj
    });

    promise.then(function(response) {

      var newdeposit = qdb_data[0][0]; //This is our new deposit ID   

      for (var i = 0; i < response.records.length; i++) {
        response.records["Related Deposit"] = newdeposit;
        response.records["Agent Name"] = response.records["Agent Name"].replace(/,/g, "");
        response.records["Client Name"] = response.records["Client Name"].replace(/,/g, "");
        response.records["Reference Number"] = response.records["Reference Number"].replace(/,/g, "");
      };

      var importcsv = response.records;

      csv_blob = importcsv.map(function(obj) {
        var items = [];
        items.push(obj["Agent Name"]);
        items.push(obj["Client Name"]);
        items.push(obj["Policy Number"]);
        items.push(obj["Reference Number"]);
        items.push(obj["CTC Deposit ID"]);
        items.push(obj["Payment ID"]);
        items.push(obj["Invoice ID"]);
        items.push(obj["CTC Date Posted"]);
        items.push(obj["Amount Applied"]);
        items.push(obj["Related Deposit"]);
        return items.join(",");
      }).join("\n");

      csv_blob = csv_blob.replace(/&/g, "&amp;");


      console.log(csv_blob);


      for (var i = 0; i < response.records.length; i++) {

        if (response.records["Include in Deposit"] == 1) {
          var newAmountApplied = (response.records["Amount Applied"] - response.records["Amount Applied"]);
          var RecID = response.records["Record ID#"];
          console.log(response.records["Record ID#"]);
          console.log(response.records["Amount Applied"]);
          var url5 = "Domain";
          url5 += "/db/";
          url5 += "bnae46589";
          url5 += "?a=API_EditRecord";
          url5 += "&rid=" + RecID + "";
          url5 += "&apptoken=d4vg82pmsvv9dceu22djsquwtj";
          url5 += "&_fid_21=" + 0 + "";
          url5 += "&_fid_14=" + newAmountApplied + "";
          url5 += "&_fid_28=" + 1 + "";
          httpGet(url5);
        }


      }


      // ----------------------------------------------------------------
      // ---------------------- IMPORT FROM CSV -------------------------
      // ----------------------------------------------------------------


      var url = "Domain"; // Remember to put in YOUR baseURL
      url += "/db/";
      url += "bnae47cy5"; // Remember to put in YOUR tableDBID
      url += "?a=API_ImportFromCSV";

      var request = "<qdbapi>";
      request += "<apptoken>d4vg82pmsvv9dceu22djsquwtj</apptoken>"; // Remember to put in YOUR appToken
      request += "<records_csv>" + csv_blob + "</records_csv>";
      request += "<clist>" + clist + "</clist>"
      request += "</qdbapi>";

      $.ajax({
        type: "POST",
        contentType: "text/xml",
        async: false,
        url: url,
        dataType: "xml",
        processData: false,
        data: request,
        timeout: 30000,
        success: function(response) {

          var xml = $(response);
          if (xml.find('qdbapi').find('errcode').text() === "0") {

            var numberOfTotalRecs = xml.find('qdbapi').find('num_recs_input').text();
            console.log("Total Number of Records in CSV = " + numberOfTotalRecs);

            var numberOfRecsAdded = xml.find('qdbapi').find('num_recs_added').text();
            console.log("Number of Records Added = " + numberOfRecsAdded);

            var numberOfRecsUpdated = xml.find('qdbapi').find('num_recs_updated').text();
            console.log("Number of Records Updated = " + numberOfRecsUpdated);

          } else {
            console.log("Quickbase returned an error.");
            console.log(response);
          }
        },
        error: function(e) {
          console.log(e);
        }
      });

      // var url4 = "Domain";
      // url4    += "/db/";
      // url4    += "bnae47dct";
      // url4    += "?a=API_PurgeRecords";
      // url4    += "&apptoken=d4vg82pmsvv9dceu22djsquwtj";
      // url4    += "&query={'21'.EX.true}";
      // httpGet(url4);   

    });
    //console.log(importcsv);   

    console.log(JSON.stringify(importcsv, null, "  "));
    //});



    This is the full code. I have not written the whole thing I am just trying to add the API Edit_Record call before the script imports into the other app. Everything else is functioning correctly and the values I need are being pulled no values are being edited however. Also I guess I am using the QuickBaseClient.js if that is what this was originally
  • >I have not written the whole thing ...

    This looks to be a cut and paste job from found code as there are various inconsistent idioms being used. I will put it on my list to look at but unfortunately I don't have time now to debug it.

    >I guess I am using the QuickBaseClient.js

    In fact you are not using it - which is good.
  • The basis of the code works for what we need, they tasked me with adding an edit records in the middle of the process to change some values that will not be moved the edit will not work properly and I wasn't sure if there was anything wrong with the Edit Records URL as I could not find much in the community on proper usage of it in a code page.