API_DOQUERY Javascript is not working

  • 0
  • 1
  • Question
  • Updated 12 months ago
  • Answered

Hi,

I'm testing the API_DoQuery with the following code and I'm not getting any result. Could you help me?

var promise = $.get(dbidcalendar, {

act: "API_DoQuery",

          query: "{3.EX.178}",

          clist: "3"

});

$.when(promise).then(function(xml){

var recordid = $("rid", xml).text();

});


I use something similar for API_AddRecord and API_EditRecord and it works very well. The APP does not have APPTOKEN

Photo of Alvaro Guerrero

Alvaro Guerrero

  • 0 Points

Posted 3 years ago

  • 0
  • 1
Photo of Shankar Ravi

Shankar Ravi

  • 280 Points 250 badge 2x thumb
Hi,
https://target_domain/db/target_dbid?a=API_DoQuery&includeRids=1 &ticket=auth_ticket&apptoken=app_token&udata=mydata &query={'5'.CT.'Ragnar Lodbrok'}AND{'5'.CT.'Acquisitions'}&clist=5.6.7.22.3 &slist=3&options=num-4.nosort.skp-10.onlynew&fmt=structured
Try to understand above URL alternative method.
Photo of Shankar Ravi

Shankar Ravi

  • 280 Points 250 badge 2x thumb
I use API_DoQuery as below : 

var jRecordId = "";
$.ajaxSetup({async: false});
url2 = "https://target_domain/db/target_dbid?a=API_Doquery&clist=3...;
console.log(url);
$.get(url2,function(xml){
$("record",xml).each(function(){
jRecordId = $(this).find("record_id_").text() + "\n";
});
});
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
Try this and note the areas where I highlighted the code:

var promise = $.get(dbidcalendar, {
  act: "API_DoQuery",
  query: "{3.EX.178}",
  clist: "3"
});

$.when(promise).then(function(xml){
  console.dirxml(xml);
  var recordid = $("record_id_", xml).text();
  console.log(recordid);
});

or chained version:

$.get(dbidcalendar, {
  act: "API_DoQuery",
  query: "{3.EX.178}",
  clist: "3"
}).then(function(xml){
  console.dirxml(xml);
  var recordid = $("record_id_", xml).text();
  console.log(recordid);
});
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
This will make all requests synchronous including those QuickBase makes through their own use of jQuery:

$.ajaxSetup({async: false});

Everything on the internet is asynchronous so you should learn to use asynchronous methods.
Photo of Shankar Ravi

Shankar Ravi

  • 280 Points 250 badge 2x thumb
Ok Then should I use chained version? Is that asynchronous method?
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
Both of my versions are asynchronous. I have not written a synchronous script is a decade!
Photo of Shankar Ravi

Shankar Ravi

  • 280 Points 250 badge 2x thumb
Thanks for your suggestion.
Photo of Shankar Ravi

Shankar Ravi

  • 280 Points 250 badge 2x thumb
If I just remove $.ajaxSetup({async: false}); from my script then will my code become asynchronous?
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
Yes
Photo of Carlos

Carlos

  • 796 Points 500 badge 2x thumb
Hi,
I'd really appreciate some help.. Trying to implement Dan's solution but the value I'm getting from the Do_Query is always 0..
fid_109 is called f_montominimo, and it should be a numeric value. I?m trying to get it into a variable and display on an alert (to verify it) and I use it later on in the code for other calculations.
var promise1 = $.get(gReqDBID, {
  act: "API_DoQuery",
  query: "{3.EX.QBU_key}",
  clist: "3.109"
});
$.when(promise1).then(function(xml){
  console.dirxml(xml);
  var montominimo = $("f_montominimo", xml).text();
montomin = Number(montominimo);
  console.log(montominimo);
});
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
It is better to ask a new question with your specific details.

The first thing I noticed is that you have QBU_key as a literal substing:
query: "{3.EX.QBU_key}",
You should be string concatenation like so:
query: "{3.EX." + QBU_key+ "}",
Or string interpolation:
query: `{3.EX.${QBU_key}}`,
But in any case you don't say where the variable QBU_key is coming from.
Photo of Carlos

Carlos

  • 796 Points 500 badge 2x thumb
The js code executes on a button click... But that first thing you noticed actually solved it! Thanks!
"javascript:" &
        "var QBU_key = '" & [Record ID#] & "';" &
        "$.getScript(gReqAppDBID + '?a=dbpage&pagename=ADDBID.js');" &
        "void(0);"
(Edited)
Photo of Chad Brandmire

Chad Brandmire

  • 1,030 Points 1k badge 2x thumb
I'm trying to get the rid of another table to populate into a field. I tried this with DoQueryCount to get numMatches and as it should returns "1" and puts it in fid 74. But, when i try the DoQuery below I get nothing. 
What am I missing?




var dbid="myDBID";
var apptoken = "oooolooktokens";
var pro = kRid;$.ajaxSetup({async: false}); 
var ntit = $("#_fid_9").val();
var nupc = $("#_fid_75").val();

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

var promise = $.get(dbid,{
  act: "API_DoQuery",
query: "{'35'.CT."+ntit+"}AND{'34'.CT."+nupc+"}",
  clist: "3"
});

$.when(promise).then(function(xml){
    console.dirxml(xml);
  var recordid = $("ItemID", xml).text();
 console.log(recordid);
$("#_fid_74").val(recordid);
});
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,074 Points 20k badge 2x thumb
More than likely this is the statement that needs to be corrected from:
var recordid = $("ItemID", xml).text();
to this:
var recordid = $("itemid", xml).text();
The XML tag is derived from the field label by lower-casing all letters and substituting an underscore for non-alphabetic characters. You should expand the xml variable in the console to see the full XML response:
console.dirxml(xml);
For what is is worth, I would make further improvements to your code such as the following:
(function() {
  var dbid = "myDBID";
  var apptoken = "oooolooktokens";
  var pro = kRid;
  var ntit = _fid_9").value;
  var nupc = _fid_75.value;
  $.ajaxSetup({data: {apptoken: apptoken}});
  var promise = $.get(dbid, {
    act: "API_DoQuery",
    query: `{35.CT.${ntit}}AND{34.CT.${nupc}}`,
    clist: "3"
  }).then(function(xml) {
    console.dirxml(xml);
    var recordid = $("itemid", xml).text();
    console.log(recordid);
    _fid_74.value = recordid;
  });
})();
Also, you probably need to add decoding logic to determine what type of age you are on as I think you code is only going to work for add and edit pages.