Forum Discussion

ToddGriffith's avatar
ToddGriffith
Qrew Cadet
7 years ago

I am having a problem with my code to toggle a checkbox.


I am having an issue with this code.  I am trying to create a URL button in a report to toggle a checkbox.  It does not seem to be working.  Can someone help?

var bool NewToggle = If([MD Approval]=false,true,false);


var text URL = URLRoot() & "db/" & Dbid()  & "?act=API_EditRecord&key=" &
"&rid=" & URLEncode ([Record ID#]) &
"&_fid_80= " & $NewToggle;

"javascript:" &
"$.get('" &
$URL &
"',function(){" &
"location.reload(true);" &
"});"
& "void(0);"

  • When  you use that Syntax the one disadvantage is that it will hide any error messages.

    The way to debug is to comment out or remove that javascript and end the formula with a like that says $URL

    But since you code looks perfect, odds are that in Advanced properties for your app you have the requirement for Application Tokens enabled, which is an extra layer of security.  So either just uncheck that requirement, or create an app token and add in a line to the URL like this


    var text URL = URLRoot() & "db/" & Dbid()  & "?act=API_EditRecord&key=" &
    "&rid=" & URLEncode ([Record ID#]) &
    "&_fid_80= " & $NewToggle 
    & "&apptoken=xxxxxxxxxxxx";

      




    • RobIV's avatar
      RobIV
      Qrew Cadet
      I have a question.  Why would you pass in an empty 'key=' to the URL?  It isn't listed in the docs for API_EditRecord when building the URL alternative to the method.  

      Example from the docs:

      https://target_domain/db/target_dbid?a=API_EditRecord&rid=154
      &_fnm_second_year=1776&_fid_8=changed&update_id=992017018414
      &ticket=auth_ticket&apptoken=app_token

      Link to the docs:
      https://help.quickbase.com/api-guide/edit_record.html

      I'm just curious.  I can't see the need for it.

      ~Rob
  • Also, just a note on brevity and clean code;

    You shouldn't have to URL Encode the Record ID if the Record ID is a number.  URL Encoding will do cool things to special characters and spaces in strings, but it doesn't do anything to numbers.  

    So, 

    123 = 123  //true
    URLEncode(123) = 123 //also true

    Feel free to play with things here to see what it does if you're interested --> https://www.w3schools.com/tags/ref_urlencode.asp

    ~Rob
  • Rob is correct, I missed that

    var text URL = URLRoot() & "db/" & Dbid() 
    & "?act=API_EditRecord&rid=" & [Record ID#] 
    & "&_fid_80=" & $NewToggle;

    also there was a space after the _fid_80= that I did not see.
  • I have one other thought, if the code still isn't working.  

    You are setting up the variable 'NewToggle' as type boolean, then building a URL string with it.  It may not make a difference, but you might have to instantiate that variable as a string first.  

    So, 

    var text NewToggle = If([MD Approval]=false,"true","false");

    ^^ That may be totally incorrect.  Just a thought.

    ~Rob