Discussions

 View Only
  • 1.  Pass Field Values to Javascript

    Posted 09-27-2017 17:34
    I have a button which takes in a bunch of field values and uses them to print a label. My only solution right now to pass variables from that page to the script is to put them as part of the formula as seen below. Is there a better way to do this?

    Formula:
    "javascript: "
    &"var dbid = '" & [_DBID_Example] & "';"
    &"var recordID = '" & [Record ID#] & "';"
    &"var appToken = '" & [AppToken] & "';"
    &"var mfgDate = '" & [Date Created] & "';"
    &"var serialNum = '" & [Serial Number] & "';"
    &"var recordID = '" & [Record ID#] & "';"
    &"var name = 'Example';"
    & [script] & "PrintLabel.js" & [/script]


  • 2.  RE: Pass Field Values to Javascript

    Posted 09-27-2017 17:36

    Allan,

    I believe you have to use the FID_##_ field identifiers.



  • 3.  RE: Pass Field Values to Javascript

    Posted 09-27-2017 17:50
    You can just create those variables in your PrintLabel.js .
    You just use the field ID like Chris is mentioned. If in edit mode you can open up console(F12) and type _fid_3.value and it should return the value for that field on the form. 3 usually is Record ID I think but you get the idea.

    In console you can type gDBID from a table and get the DBID. You just need the formula text field to load the PrintLabel.js then just create another formula text thats an html button and wrap the print label function on the click like this
    $("#qbu_yourBtn").on("click", printLabel())

    Good luck!
    -Chuck@Chuck.Support


  • 4.  RE: Pass Field Values to Javascript

    Posted 03-12-2019 17:55
    This doesn't seem to work anymore, I've attempted to type _fid_3.value in the console and it states that _fid_3 is not defined. I've also tested with a field that was actively on the form.


  • 5.  RE: Pass Field Values to Javascript

    Posted 09-27-2017 17:51
    You could pass just the [Record ID#] and within the script perform an AJAX call using API_DoQuery or API_GetRecordInfo using just one "passed" variable. 

    A second technique is to augment the IOL variable [-] with some HTML element (typically <a>, <div>, <span>) that has data attributes. Here is a button that passes [Record ID#] and [Name] through a data attribute:

    [iol] & "moduleTable5.js" & [/iol]
    &
    "<a class='QBU_Button Vibrant Success' " &
    "  data-rid='" & [Record ID#] & "'" &
    "  data-name='" & [Name] & "'" &
    ">Button</a>"
    And then read off the data attributes within  the script using this.dataset:
    (function(){
      var dbid = "bj5c9b9nb";
      var dbidTable1 = "bj5c9b9pr";
      var dbidTable2 = "bj5dnm78i";
      var dbidTable3 = "bj5dnnsgm";
      var dbidTable4 = "bj5dnpb46";
      var dbidTable5 = "bj5dwp4f5";
      var dbidTable6 = "bj5dwq9dx";
      var apptoken = "c7fsvsnb7x6327d7an2ncd2k7vq";
      $.ajaxSetup({data: {apptoken: apptoken}});
      $("a.QBU_Button").on("click", function(event) {
        console.log("moduleTable5.js");
        var rid = this.dataset.rid;
        var name = this.dataset.name;
        console.log("rid=", rid);
        console.log("name=", name);
        console.log("this", this);
      });
    })();

    Note that you can't use data attributes with fields that contain text that cannot be included in an attribute so use this technique for simple text, number date fields but not multi-line text fields.

    A third technique it to pass absolutely no parameters but within the script scrape the rid out of the HTML or reference the global kRid and then perform an AJAX query for the rest of the fields as above.

    The point of all of these approaches is to make your formulas as clean and readable as possible and avoid placing script into the QuickBase Formula. You will have endless problems debugging your formulas if you insist on placing JavaScript in them because you have to escape all sorts of characters. Get out of the formula language and into script as fast as possible.


  • 6.  RE: Pass Field Values to Javascript

    Posted 03-12-2019 18:37
    kRid is exactly what I was looking for


  • 7.  RE: Pass Field Values to Javascript

    Posted 03-12-2019 19:02
    Do you happen to know how I can refer to the kRid from a JS referenced in an iframe?


  • 8.  RE: Pass Field Values to Javascript

    Posted 09-27-2017 18:01
    I should also add that your existing formula will create global variables that could conflict with QuickBase's global variables :
    &"var dbid = '" & [_DBID_Example] & "';"
    &"var recordID = '" & [Record ID#] & "';"
    &"var appToken = '" & [AppToken] & "';"
    &"var mfgDate = '" & [Date Created] & "';"
    &"var serialNum = '" & [Serial Number] & "';"
    &"var recordID = '" & [Record ID#] & "';"
    &"var name = 'Example';"
    You can reduce the chance of this but prefixing wtih QBU:
    &"var QBU_dbid = '" & [_DBID_Example] & "';"
    &"var QBU_recordID = '" & [Record ID#] & "';"
    &"var QBU_appToken = '" & [AppToken] & "';"
    &"var QBU_mfgDate = '" & [Date Created] & "';"
    &"var QBU_serialNum = '" & [Serial Number] & "';"
    &"var QBU_recordID = '" & [Record ID#] & "';"
    &"var QBU_name = 'Example';"
    I sent out a factitious memo years ago informing all earth based QuickBase staff to never use a variable, id or class name etc that starts with QBU to avoid namespace collisions. Maybe I should resend my memo as they hired a lot of new developers in the last decade.


  • 9.  RE: Pass Field Values to Javascript

    Posted 09-27-2017 21:06
    That's a great point. I haven't run into this problem yet but definitely can see it happening in the future._