Trying to pass a value derived from javascript code to a field in quickbase

  • 0
  • 1
  • Question
  • Updated 7 months ago
  • Answered
I've written a lovely piece of javascript which creates a unique quote number and resets every night, but I can't figure out how to pass that value from my javascript file (which is a code page in quickbase) to a field in my quotations table.  When generating a New Quote, I want quickbase to take the Quote Number from the javascript file and insert it into the Quote # field.  Can someone point me in the right direction?  I could get the javascript result to print on a blank browser page, and got it to print in the console.log(), but that's it.  Thank you!
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb

Posted 7 months ago

  • 0
  • 1
Photo of Chris

Chris, Champion

  • 4,390 Points 4k badge 2x thumb
Can you embed the js in a HTML form element with a name="_FID_##" ?
Photo of Brett

Brett

  • 110 Points 100 badge 2x thumb
The easiest would be as Chris suggested and simply use a hidden form to submit with API_AddRecord or you can use an AJAX post method to do the same.  To find the field ids to reference you can look at the fields in your QB and, if I remember correctly, you can find an advanced button and have it show you the field ids in the listing as well. 

Once you do that, you'll be able to find your field ids to associate correctly and use a form or the api to add the record.

https://help.quickbase.com/api-guide/index.html#add_record.html%3FTocPath%3DQuick%2520Base%2520API%2...
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
Thank you both for your help!

Chris - I can "embed the js in a HTML", but I don't yet exactly know what a "form element" is.  I'm presuming you're not referring to renaming the code page I created, but putting the HTML in its own Field named "_FID_##" (without the quotes, and with the field number of the place where I want the result to appear as ##).  But, that can't be.  If I paste my HTML file with the js imbeded in it to a Formula - Rich Text field, I get stopped at every line.  so I guess I don't know where the "form element"s are.

Brett - I guess you are saying I need to create my own button to do an API_AddRecord rather than use the native "+New Quote" green button that is on my page, which is fine if I can figure out a) how to do it, and b) how to get rid of the "+New Quote" green button so our people don't get confused.  I don't know anything about "AJAX", but I'll look into it, and I'll read up on where an API_AddRecord gets its data.  I think I know how to get the field ids (Advanced Options button in 'Settings, Fields').

So, I'll muddle through this today and hope I come up with the right moves!  I appreciate your help.

Ken
Photo of Chris

Chris, Champion

  • 4,390 Points 4k badge 2x thumb

Ken,

Sorry for the confusion. When I say form element, I mean like the following, and this is a pseudo code effort:

<form id="thisForm" name="thisForm">

<input type="text" &

{insert js code here}

&\"name="_fid_38" />

</form>

So the inserted js code takes on a value and is inserted into the form input element with a name of _fid_38 ( the 38 is just a number I picked out of the air, it would actually be the fid of the field you intent the value to be applied to.

The syntax also may be off, but I think you get the gist of my suggestion.

Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
Chris - you're not confusing me, I started out that way (lol).  I now know where the Form Elements are, and I know where the form id is, so I'll play around with this today as time allows.  I'll get back to you when I get more confused!  Thank you.
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
I'm missing something basic.  To me, "Form Elements" are either Fields, or 'Text, Section Heading or Tab'.  If I put this code into a Field, when choosing Formula - Rich Text, I get bounced.  What other type of Form Element would this code go into?
Photo of Brett

Brett

  • 110 Points 100 badge 2x thumb
Ah, based on that reply it sounds like the JS that you added is in QB itself rather than in an external website, correct?  If that's the case, it's probably best to relate the tables and use something native in the QB to create the new records in your separate table.
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
Correct.  It's just a file I wrote that I thought would work as a formula but it's getting to be quite a bit more involved than that.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,522 Points 20k badge 2x thumb
I assume from all the comments that your "lovely piece of javascript" is loaded into a edit or add record page. From this point there are two ways to "pass that value from my javascript file (which is a code page in quickbase) to a field in my quotations table."

(1) Set Field Value Via Form Field

_fid_6.value = calculated_value;

(2) Set Field Value Via API
$(dbidTable, {
  act: "API_EditRecord",
  rid: kRid,
  _fid_6: calculated_value
});
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
The choices of Pages were Home Page, Rich Text Page and Code Page.  Not "edit or add record page".  As I said, I'm missing something very basic.  I have tables with fields, forms and pages, but I can't figure out where the code goes...
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,372 Points 20k badge 2x thumb
JavaScript always goes in code pages. I assume you are intending to inject this JavaScript into an ?a=er or ?a=nwr page. If not post the code so we can see your lovely piece of code.
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
You're way ahead of me.  I have to read up on ?a=er and ?a=nwr.  I'm not quite comfortable with those things yet, but I appreciate you pointing me in the right direction.  The lovely code is:

var date = new Date();
var yr = date.toDateString().substr(-2);
var mo = date.getMonth()+1;
var d = date.getDate();
var da = d.toString();
var a;

if (localStorage.getItem("c") !== da) {
localStorage.setItem("b", 0);
localStorage.setItem("c", da);
}

if (localStorage.getItem("b")) {
a = parseInt(localStorage.getItem("b"))+1;
localStorage.setItem("b", a);
} else {
localStorage.setItem("b", 0);
a = parseInt(localStorage.getItem("b"));
}

function leadingZero(mo) {
if (mo <= 9) {
mo = "0" + mo;
}
return mo;
}

I'm sure there are more elegant ways to do what I'm trying to do, but considering my experience level with QB & JavaScript (maybe a month in QB and a week learning js), I'm quite happy.  The point is to establish a numbering system for generating our Quotation Numbers.  We want every quote number to be yymmdd## where the ## for the first quote of the day is 01, and increments as quotes are generated that day.  The next day we reset back to 01.  My problem with doing a native QB formula is that I couldn't figure out how to increment the numbers and I couldn't figure out how to do the reset.  i.e., I don't know how to store a value and retrieve it later in QB.
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
When I copied my code above, it missed the last line which gave the result.  It should have ended with:

q=(yr+leadingZero(mo)+leadingZero(da)+leadingZero(a));

In the meantime, I've figured out how to access my javascript page with a URL button, and am doing an API_AddRecord using ajax (modified from the QB API Boot Camp).  I've successfully generated new records and am able to pass literals using:

  var request="<qdbapi>";
  request += " <field fid='7'>Kenny</field>";
  request += "<field fid='6'>q</field>";
  request += "</qdbapi>";

Field 7 says "Kenny", and field 6 says "q".  I want field 6 to have the result of the variable.  I've tried:

request += "<field fid='6'>$q</field>";

and a bunch of other iterations, and I've searched and read and searched and read, but can't for the life of me figure out how to get the value of q into fid 6.  (Yup, I tried value.q, value(q), etc)

I also tried adding:

$(bnfynvsjt, {
  act: "API_EditRecord",
  rid: kRid,
  _fid_6:q
});

to it, but that didn't work either.  Maybe I put it in the wrong place or maybe I can't do two API calls in the same script, but I can play with that some more tomorrow.  But if you can save me some time I'd sure appreciate it!

Oh, and I know the calculation is working because after the 'request' info I do a console.log(q), and a correct result appears in the console when a new record is generated.

Any ideas?  Thank you!
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
In case you're wondering...the answer is:

request += "<field fid='6'>"+q+"</field>";