JQuery not retrieving information

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

This code is not returning information.

The premise is to return the kRID of the record in the primary table based on the current user (user field) value in a separate table in the same application. The primary table has the email address of the user & s converted to a user field. Only the "active" record should be returned.

The primary table fields are fid_311 - formula user name, fig_94 - employee # fid_3 - RID & fid_112 - current status.

The sub table field is fid_17 - current user.

No error msg is returned and the xml shows undefined.

var status = "INACTIVE";
var url = "";
url += "../db/?????????";
url += "?act=API_DoQuery";
url += "&query={'311'.EX.'" + curruser + "'}AND{'112'.XEX.'" + status + "'}";
url += "&clist=94.3.112";
url += "&slist=94.3.112";
url += "&options=num-1.sortorder-D";
// url +="&fmt=structured";
url += "&apptoken=" + apptoken;
alert(" url = " + url) ;

$.get(url, function (xml) {
alert(" Errtext=" + $("errcode", xml).val() + " - " + $("errtext", xml).text());
alert("XML Info=" + xml.xml);
var hrrec = $("related_hr_record", xml).text();
var empnbr = $("employee#", xml).text();
alert("emp# - " + empnbr + "HR Rec -" + hrrec);
if (hrrrec > 0) {
$("#_fid_18").val(hrrec);
$("#_fid_19").val(empnbr);
} else if (hrrec = 0) {
$("#_fid_18").val(301);
$("#_fid_19").val("140925");
}
});

Photo of Gabe

Gabe

  • 0 Points

Posted 5 years ago

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

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
Debug the XML response with code similar to this:

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=310


Notes:

(0) You are using a old style of writing the script that does not take full advantage of jQuery in at least these areas:



(a) use $.ajaxSetup() as indicated to specify that all AJAX calls should sent the apptoken as data

(b) user a relative URL for the AJAX call which is simply the dbid

(c) Use AJAX promise methods rather than a callback to handle the AJAX response. You will avoid the "temple of doom" of highly nested code and decouple the code that deals with the AJAX request and the AJAX response.




(1) Use FireFox and the FireBug addon for debug

(2) Use console.log(), console.dir() and console.dirxml() to debug rather than alert().

(3) The string "_curuser_" is a special value that QuickBase interprets to mean the current user.

(4) You don't need the single quotes in your query criteria.
Photo of Gabe

Gabe

  • 0 Points
Thank you, I will review what you've recommended & begin utilizing. This is very helpful.
Photo of Gabe

Gabe

  • 0 Points
Firefox worked great but still have some work as the results did not yield any data


<qdbapi>
<action>API_DoQuery</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<dbinfo>
<name>Personnel Data</name>
<desc>Keep track of employee start dates, managers, departments, salary and contact information.</desc>
</dbinfo>
<variables> </variables>
<chdbids> </chdbids>
</qdbapi>
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
Now debug your query pasting commands into the FireBug console because this is faster than updating the user defined page holding your script.
Photo of Gabe

Gabe

  • 0 Points
This is great technique but still no luck getting the Query to work.
Is there a "trick" to pulling data from a formula field?
The Primary table has a formula that generates the User Name based on the email.
A simple Alert was placed in the Primary table to display the User Name & nothing is displayed which may explain why the original query you've provided excellent assistance is not working.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
>but still no luck getting the Query to work

This type of info is not very helpful for debugging. When debugging an AJAX call the most helpful information is what the request and and response are. Post the XML response of your AJAX call and it will likely reveal the problem.
Photo of Gabe

Gabe

  • 0 Points
Thanks again, too many interruptions & still a novice but this is the best advice i have received as of late!
I did resolve the issue.
Photo of Gabe

Gabe

  • 0 Points
i got past the big issues but now still can't find how to pull back the value that is in a user field that has the current user.
This is the JQuery (but i keyed in the User Name ?LastName", "FirstName" to get it to work).
{
          var apptoken = "q57nnbbquihr8baxv9xfm9hmmh";
        var status = "INACTIVE";
         var dbid = "bgb4px6ci";
          var cuser = $("#_fid_17").val();
          console.log(" cuser= " + cuser);

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

  var promise = $.get(dbid, {
    act: "API_DoQuery",
    query: "{311.EX.?LastName", ?FirstName?}AND{112.XEX." + status + "}",
    clist: "94.3.112.311.297",
    slist: "94.3.112",
  options: "num-1.sortorder-D"
});
      
$.when(promise).then(function(xml) {
    console.dirxml(xml);
    var hrrec = $("id_", xml).text();
  var empnbr = $("employee_", xml).text();
    console.log(hrrec);
    console.log(empnbr);
    $("#_fid_18").val(hrrec);
    //$("#_fid_19").val(empnbr);
  });

This is the Result: the negative number is the value being returned from the "var cuser = $("#_fid_17").val();"
I wonder if it is necessary to use JQuery LOAD or other.

 cuser= -57684664
GET https://???????????.quickbase.com/db/?????????ap...311.297&slist=94.3.112&options=num-1.sortorder-D

200 OK
959ms
jquery-....min.js (line 4)
undefined
<qdbapi>
<action>API_DoQuery</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<dbinfo>
<name>Personnel Data</name>
<desc>Keep track of employee start dates, managers, departments, salary and contact information.</desc>
</dbinfo>
<variables> </variables>
<chdbids> </chdbids>
<record>
<employee_>140925</employee_>
<id_>301</id_>
<current_status>Active</current_status>
<hr_user>???_QB_Admin</hr_user>
<email>?.????????@???????????.com</email>
<update_id>1397580334837</update_id>
</record>
</qdbapi>
301       <-- this is the RID on the Personnel Data
140925 <-- this is the employee number

the _curuser_ did not retrieve any data:
undefined
<qdbapi>
<action>API_DoQuery</action>
<errcode>0</errcode>
<errtext>No error</errtext>
<dbinfo>
<name>Personnel Data</name>
<desc>Keep track of employee start dates, managers, departments, salary and contact information.</desc>
</dbinfo>
<variables> </variables>
<chdbids> </chdbids>
</qdbapi>
2
(an empty string)
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
I am not sure I understand what you are trying to accomplish at this point and I don't understand the appearance of the question marks in you last message:

query: "{311.EX.?LastName", ?FirstName?}AND{112.XEX." + status + "}",

Also, there seems to be some confusion about the "current user" Normally the "current user" is the user who is presently using your application and the use of the token "_curuser_" is used to build a query referring to this current user. But you appear to be extracting what you are calling the "current user" from a field on a new or edit record form with this statement:

var cuser = $("#_fid_17").val();

This isn't really the "current user" but simply the user that has been selected on the form.

QuickBase has a bit of a confusing way of identifying user. While a human might like to refer to a user by name, email or handle, QuickBase refers to users with an identifier you normally do not see until you start using the API or peering into the construction of QuickBase's forms. For example my UserID in QuickBase is this:

51084879.bdbt

But sometimes QuickBase uses 51084879 to refer to me and other times it uses -51084879 to identify me in user fields. This is why you are seeing the negative number - it is the negative of the first part of the full User ID.

My suggestion is that since you have much of your code working that you post a new question dealing solely with your remaining problem using the most concise description of the problem and remove all the other details. When these message threads become long and wander too much around people including me stop reading them.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
Hello Dan,  I have this integrated, but I still need to do about 3 more api calls.  How do I avoid the nesting the queries?
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
lets say the first query retireved data for the RID.  Now I need to get more data from a couple more tables, using the data retrieved from the primary table, then do an insert.  I know how to do the api calls, I just don't know how to avoid the nesting... which is a real pain.

Thanks,
Dean
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
All jQuery's AJAX calls return a promise and since promises are "thenable" they have a them() method which will be called when the AJAX call completes. Chaining three API calls together and waiting till the prior AJAX call is complete would be done like this:

$.get(dbidTable1, {
  act: "API_Something1",
  //... more params
}).then(function(xml) {
  //extract parameters from xml response from calling API_Something1
  $.get(dbidTable2, {
    act: "API_Something2",
    //... more params
}).then(function(xml) {
  //extract parameters from xml response from calling API_Something2
  $.get(dbidTable3, {
    act: "API_Something3",
    //... more params
}).then(function(xml) {
  //extract parameters from xml response from calling API_Something3
  alert("all done");
});
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
When I try this it doesn't work... It looks like there are 6 open braces and only 4 close braces...there are opens for the $get and for the then... but only one close for each of the pairs???
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
$.get(dbidTable1, {
  act: "API_Something1",
  //... more params
}).then(function(xml) {
  //extract parameters from xml response
  $.get(dbidTable2, {
    act: "API_Something2",
    //... more params
  }).then(function(xml) {
    //extract parameters from xml response
    $.get(dbidTable3, {
      act: "API_Something3",
      //... more params
    }).then(function(xml) {
      //extract parameters from xml response
      alert("all done");
    });
  });
});
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
Thanks Dan... that makes sense.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,434 Points 20k badge 2x thumb
Actually I think this is what you want to avoid deep nesting (always return a promise):

$.get(dbidTable1, {
  act: "API_Something1",
  //... more params
}).then(function(xml) {
  //extract parameters from xml response
  return $.get(dbidTable2, {
    act: "API_Something2",
    //... more params
  });
}).then(function(xml) {
  //extract parameters from xml response
  return $.get(dbidTable3, {
    act: "API_Something3",
    //... more params
  });
}).then(function(xml) {
  //extract parameters from xml response
  alert("all done");
});

I should add that this is the preferred method of chaining promises. When developing code it is natural to debug the first API, console log some extracted data from the XML response and then form the next API call. This is normally done with cascading (nesting) code because the primary objective is to get the logic and data extraction working. You often don't conform the code to the prefer version of returning promises by chaining until you refactor the code after everything is working.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
I am trying this method, but when I try to use the retrieved parameters from the first api_DoQuery in the third API_DoQuery, I get "undefined" messages.  thoughts?