Forum Discussion

KenCasser's avatar
KenCasser
Qrew Cadet
6 years ago

Can I access system variables through javascript?

I have a couple of Variables set up under Home/Settings/Variables and I would like to use their values in creating a new record using a button on the home page.  The home page buttons offer limited choices (New Record, Report, Page & URL), so I don't know how to write successive API calls in any of those formats to accomplish my goal.  How can I do API_SetDBVar & API_GetDBVar calls from the home page?

7 Replies

  • >Can I access system variables through javascript?

    Of course you can! JavaScript can access everything and is your portal to a happy, bountiful and successful life.

    In the pastie below I define a function with the following signature:
    getDBVar(dbid, varName) { ...}
    When you call it a promise is returned which when it resolves returns the value of the varName.

    In the Pastie Database I created a new application variable foo and gave it the value bar. So if you visit the Pastie Database and paste the code into the console you will get the following output:
    foo = bar
    Pastie Database
    https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=680

    Notes:

    (1) This script is written in a very specific style to make the code as short as possible and uses some advanced JavaScript features including ES6 Promises.

    (2) The script converts the jQuery promise to an ES6 promise to get around a bug in jQuery 1.71 which QuickBase uses which prevents proper chaining.

    (3) The coding for setDBVar would be very similar but would use a function with the following signature:
    setDBVar(dbid, varName, varValue) { ...}
  • Dan - I have succeeded with 'GetDBVar', but my limited knowledge is preventing me from translating that to 'SetDBVar'.  Here's what I've got.  Where am I going wrong?

    function setDBVar(dbid, varDaName, 12) {
    return Promise.resolve(
    $.set(dbid, {
    act: "API_SetDBVar",
    varname: varDaName
    })
    ).then(function(xml) {
    return $("value", xml).text();
    });
    }

    getDBVar(dbid, varDaName)
    .then(function(varValue) {
    console.log('${varDaName} = ${varValue}');
    });

    Thank you!

    Ken
    • _anomDiebolt_'s avatar
      _anomDiebolt_
      Qrew Elite
      function setDBVar(dbid, dbvName, dbvValue) {
        return Promise.resolve(
          $.get(dbid, {
            act: "API_SetDBVar",
            varname: dbvName,
            value: dbvValue
          })
        ).then(function(xml) {
          return $("value", xml).text();
        });
      }
      var dbid = "";
      var dbvName = "";
      var dbvValue = "";
      setDBVar(dbid, dbvName, dbvValue)
        .then(function(dbvValue) {
          console.log('${dbvName} = ${dbvValue}');
        });
    • KenCasser's avatar
      KenCasser
      Qrew Cadet
      Thank you!  I got it to work, and tried getting the 'Set' and a 'Get' to work in sequence and then nothing worked!  I'll have to play with it more tomorrow, but thank you for your help.
    • KenCasser's avatar
      KenCasser
      Qrew Cadet
      It's all working now!  Thank you so much for your help.