Can I access system variables through javascript?

  • 0
  • 1
  • Question
  • Updated 4 weeks ago
  • Answered
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?
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb

Posted 1 month ago

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

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,754 Points 20k badge 2x thumb
>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) { ...}
(Edited)
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
Thank you!  I appreciate your help!
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
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
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,694 Points 20k badge 2x thumb
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}`);
  });
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
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.
Photo of Ken Casser

Ken Casser

  • 612 Points 500 badge 2x thumb
It's all working now!  Thank you so much for your help.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 27,694 Points 20k badge 2x thumb
Go Script!