Discussions

Expand all | Collapse all

case statement in api_addrecord javascript

  • 1.  case statement in api_addrecord javascript

    Posted 02-09-2018 05:49
    Hi Quickbase Community,

    I am having an issue using a switch statement in Javascript while adding a record. I have defined a variable in the URL button called F9 that provides the number of site visits, the code runs perfectly without the switch statement - can someone please let me know if I am doing something wrong and how i can correct it? (there are 10 visits, i have just shortened for testing)

    $.get(dbidWR, {
    act: "API_AddRecord",
    _fid_48: F72,
    _fid_132: F77,
    _fid_150: F98,
    _fid_147: F84,
    _fid_77: F100,
    switch(F9){
    case '1':
    _fid_137: F21;
    _fid_251: F22;
    _fid_442: F23;
    _fid_164: F999;
    break;
    case '2':
    _fid_137: F21;
    _fid_251: F22;
    _fid_442: F23;
    _fid_164: F999;
    _fid_138: F25;
    _fid_252: F26;
    _fid_443: F27;
    _fid_165: F999;
    break;
    }


  • 2.  RE: case statement in api_addrecord javascript

    Top
    Contributor
    Posted 02-09-2018 14:08
    Your code is a mess. You need to merge the different sets of options objects together using native JavaScript Object.assign() or jQuery.extend().

    I don't have your F* variables you so I just converted them to string values in the following code.
    var options1 = {
      act: "API_AddRecord",
      _fid_48: "F72",
      _fid_132: "F77",
      _fid_150: "F98",
      _fid_147: "F84",
      _fid_77: "F100",
    }
    var options2 = {
      _fid_137: "F21",
      _fid_251: "F22",
      _fid_442: "F23",
      _fid_164: "F999",
    }
    var options3 = {
      _fid_137: "F21",
      _fid_251: "F22",
      _fid_442: "F23",
      _fid_164: "F999",
      _fid_138: "F25",
      _fid_252: "F26",
      _fid_443: "F27",
      _fid_165: "F999"
    }
    var F9 = "1";
    var options;
    switch (F9) {
      case '1':
        options = Object.assign({}, options1, options2)
        break;
      case '2':
        option = Object.assign({}, options1, options3)
        break;
    }
    console.log(JSON.stringify(options, null, "  "));
    var F9 = "2";
    var options;
    switch (F9) {
      case '1':
        options = Object.assign({}, options1, options2)
        break;
      case '2':
        options = Object.assign({}, options1, options3)
        break;
    }
    console.log(JSON.stringify(options, null, "  "));

    $.get(dbidWR, options)
      .then(function(xml) {
        console.dirxml(xml)
      });


    The first and second console.logs will display as follows depending on for F9 equal to "1" or "2" as follows:
    {
      "act": "API_AddRecord",
      "_fid_48": "F72",
      "_fid_132": "F77",
      "_fid_150": "F98",
      "_fid_147": "F84",
      "_fid_77": "F100",
      "_fid_137": "F21",
      "_fid_251": "F22",
      "_fid_442": "F23",
      "_fid_164": "F999"
    }
     {
      "act": "API_AddRecord",
      "_fid_48": "F72",
      "_fid_132": "F77",
      "_fid_150": "F98",
      "_fid_147": "F84",
      "_fid_77": "F100",
      "_fid_137": "F21",
      "_fid_251": "F22",
      "_fid_442": "F23",
      "_fid_164": "F999",
      "_fid_138": "F25",
      "_fid_252": "F26",
      "_fid_443": "F27",
      "_fid_165": "F999"
    }


  • 3.  RE: case statement in api_addrecord javascript

    Posted 02-10-2018 23:24
    Thank you so much, For anyone else needing to use this

    /*
    The below code is to be loaded against a URL Button
    */
    "javascript:" &
    "var F3= " & URLEncode([Record ID#]) & ";" &
      "var F9= '" & [Number of Store Visits Required ? ] & "';" &
      "var F72= '" & [Related Project] & "';" &
      "var F77= '" & [Related Site] & "';" &
      "var F98= '" & [Type of Scope] & "';" &
      "var F84= '" & [Project - Related customer] & "';" &
      "var F100= '" & [Date of Issue] & "';" &
      "var F999= '" & "Installation" & "';" &
      "var F21= '" & [Visit 1 Date] & "';" &
      "var F22= '" & [Visit 1 Time] & "';" &
      "var F23= '" & [Visit 1 Labour Qty] & "';" &
      "var F25= '" & [Visit 2 Date] & "';" &
      "var F26= '" & [Visit 2 Time] & "';" &
      "var F27= '" & [Visit 2 Labour Qty] & "';" &
      "var F29= '" & [Visit 3 Date] & "';" &
      "var F30= '" & [Visit 3 Time] & "';" &
      "var F31= '" & [Visit 3 Labour Qty] & "';" &
      "var F33= '" & [Visit 4 Date] & "';" &
      "var F34= '" & [Visit 4 Time] & "';" &
      "var F35= '" & [Visit 4 Labour Qty] & "';" &
      "var F37= '" & [Visit 5 Date] & "';" &
      "var F38= '" & [Visit 5 Time] & "';" &
      "var F39= '" & [Visit 5 Labour Qty] & "';" &
      "var F41= '" & [Visit 6 Date] & "';" &
      "var F42= '" & [Visit 6 Time] & "';" &
      "var F43= '" & [Visit 6 Labour Qty] & "';" &
      "var F45= '" & [Visit 7 Date] & "';" &
      "var F46= '" & [Visit 7 Time] & "';" &
      "var F47= '" & [Visit 7 Labour Qty] & "';" &
      "var F49= '" & [Visit 8 Date] & "';" &
      "var F50= '" & [Visit 8 Time] & "';" &
      "var F51= '" & [Visit 8 Labour Qty] & "';" &
      "var F53= '" & [Visit 9 Date] & "';" &
      "var F54= '" & [Visit 9 Time] & "';" &
      "var F55= '" & [Visit 9 Labour Qty] & "';" &
      "var F57= '" & [Visit 10 Date] & "';" &
      "var F58= '" & [Visit 10 Time] & "';" &
      "var F59= '" & [Visit 10 Labour Qty] & "';" &
      "$.getScript('" & URLRoot() & "db/[DBID]?act=dbpage&pagename=AddWR.js');void(0);"
    /*
    The below code is to be loaded onto .js codepage
    */
    // Connecting to SOW Table:
    var dbidSOW = "xxxxxxx";
    // Connecting to Work Package Table:
    var dbidWR = "xxxxxx";
    // Providing the App Token below:
    var apptoken = "xxxxxx";
    // Set Variable for options no null
    var options;
    //Setting up an AJAX call to tell the script we are expecting to receive back XML later in the script
    $.ajaxSetup({
      data: {
        apptoken: apptoken
      }
    });
    // Setup the options, What fields for each option. 
    var optionsone = {
      act: "API_AddRecord",
      _fid_48: F72,
      _fid_132: F77,
      _fid_150: F98,
      _fid_147: F84,
      _fid_77: F100,
    }
    var optionstwo = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999
    }
    var optionsthree = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999
    }
    var optionsfour = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999
    }
    var optionsfive = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999
    }
    var optionssix = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999
    }
    var optionsseven = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999,
      _fid_142: F41,
      _fid_256: F42,
      _fid_447: F43,
      _fid_169: F999
    }
    var optionseight = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999,
      _fid_142: F41,
      _fid_256: F42,
      _fid_447: F43,
      _fid_169: F999,
      _fid_143: F45,
      _fid_257: F46,
      _fid_448: F47,
      _fid_170: F999
    }
    var optionsnine = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999,
      _fid_142: F41,
      _fid_256: F42,
      _fid_447: F43,
      _fid_169: F999,
      _fid_143: F45,
      _fid_257: F46,
      _fid_448: F47,
      _fid_170: F999,
      _fid_144: F49,
      _fid_258: F50,
      _fid_449: F51,
      _fid_171: F999
    }
    var optionsten = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999,
      _fid_142: F41,
      _fid_256: F42,
      _fid_447: F43,
      _fid_169: F999,
      _fid_143: F45,
      _fid_257: F46,
      _fid_448: F47,
      _fid_170: F999,
      _fid_144: F49,
      _fid_258: F50,
      _fid_449: F51,
      _fid_171: F999,
      _fid_145: F53,
      _fid_259: F54,
      _fid_450: F55,
      _fid_172: F999
    }
    var optionseleven = {
      _fid_137: F21,
      _fid_251: F22,
      _fid_442: F23,
      _fid_164: F999,
      _fid_138: F25,
      _fid_252: F26,
      _fid_443: F27,
      _fid_165: F999,
      _fid_139: F29,
      _fid_253: F30,
      _fid_444: F31,
      _fid_166: F999,
      _fid_140: F33,
      _fid_254: F34,
      _fid_445: F35,
      _fid_167: F999,
      _fid_141: F37,
      _fid_255: F38,
      _fid_446: F39,
      _fid_168: F999,
      _fid_142: F41,
      _fid_256: F42,
      _fid_447: F43,
      _fid_169: F999,
      _fid_143: F45,
      _fid_257: F46,
      _fid_448: F47,
      _fid_170: F999,
      _fid_144: F49,
      _fid_258: F50,
      _fid_449: F51,
      _fid_171: F999,
      _fid_145: F53,
      _fid_259: F54,
      _fid_450: F55,
      _fid_172: F999,
      _fid_146: F57,
      _fid_260: F58,
      _fid_451: F59,
      _fid_173: F999
    }
    // setup switch to assign options defined against an object under a case statement so only data relevant to the switch condition will be returned, Get function is called and new Work Package is created
    switch (F9) {
      case '1':
        options = Object.assign({}, optionsone, optionstwo)
        break;
      case '2':
        options = Object.assign({}, optionsone, optionsthree)
        break;
      case '3':
        options = Object.assign({}, optionsone, optionsfour)
        break;
      case '4':
        options = Object.assign({}, optionsone, optionsfive)
        break;
      case '5':
        options = Object.assign({}, optionsone, optionssix)
        break;
      case '6':
        options = Object.assign({}, optionsone, optionsseven)
        break;
      case '7':
        options = Object.assign({}, optionsone, optionseight)
        break;
      case '8':
        options = Object.assign({}, optionsone, optionsnine)
        break;
      case '9':
        options = Object.assign({}, optionsone, optionsten)
        break;
      case '10':
        options = Object.assign({}, optionsone, optionseleven)
        break;
    }
    // Test to display XML returned from the above switch case and get call
    console.log(JSON.stringify(options, null, "  "));
    // returns the Work Package Record ID from the newly created record above then pushes it to a Edit Record function against the SOW Table to input against the related SOW.
    $.get(dbidWR, options).then(function(xml) {
      console.dirxml(xml);
      var WRrid = $("rid", xml).text();
      console.log(WRrid);
      // I'm using the URL Editrecord method below, could easily do a second AJAX call but no need with this use case
      document.location.href = "https://[realm].quickbase.com/db/[dbidsow]?a=API_EditRecord&apptoken=[xxxxxxxxxxxxxxxxxxxxx]&...; + F3 + "&_fid_114=" + WRrid + "&disprec=13"
    });


  • 4.  RE: case statement in api_addrecord javascript

    Top
    Contributor
    Posted 02-11-2018 00:20


    Supersize Me! 

    It that whole thing a formula URL?

    It is over 8000 characters long?

    Does it even work?

    Which Browser?


  • 5.  RE: case statement in api_addrecord javascript

    Posted 02-11-2018 00:25
    Works perfectly, i use Chrome, also used it in IE as well. Only the below code is put into a URL Button, the rest comes from the AddWR.js.

    "javascript:" &
    "var F3= " & URLEncode([Record ID#]) & ";" &
      "var F9= '" & [Number of Store Visits Required ? ] & "';" &
      "var F72= '" & [Related Project] & "';" &
      "var F77= '" & [Related Site] & "';" &
      "var F98= '" & [Type of Scope] & "';" &
      "var F84= '" & [Project - Related customer] & "';" &
      "var F100= '" & [Date of Issue] & "';" &
      "var F999= '" & "Installation" & "';" &
      "var F21= '" & [Visit 1 Date] & "';" &
      "var F22= '" & [Visit 1 Time] & "';" &
      "var F23= '" & [Visit 1 Labour Qty] & "';" &
      "var F25= '" & [Visit 2 Date] & "';" &
      "var F26= '" & [Visit 2 Time] & "';" &
      "var F27= '" & [Visit 2 Labour Qty] & "';" &
      "var F29= '" & [Visit 3 Date] & "';" &
      "var F30= '" & [Visit 3 Time] & "';" &
      "var F31= '" & [Visit 3 Labour Qty] & "';" &
      "var F33= '" & [Visit 4 Date] & "';" &
      "var F34= '" & [Visit 4 Time] & "';" &
      "var F35= '" & [Visit 4 Labour Qty] & "';" &
      "var F37= '" & [Visit 5 Date] & "';" &
      "var F38= '" & [Visit 5 Time] & "';" &
      "var F39= '" & [Visit 5 Labour Qty] & "';" &
      "var F41= '" & [Visit 6 Date] & "';" &
      "var F42= '" & [Visit 6 Time] & "';" &
      "var F43= '" & [Visit 6 Labour Qty] & "';" &
      "var F45= '" & [Visit 7 Date] & "';" &
      "var F46= '" & [Visit 7 Time] & "';" &
      "var F47= '" & [Visit 7 Labour Qty] & "';" &
      "var F49= '" & [Visit 8 Date] & "';" &
      "var F50= '" & [Visit 8 Time] & "';" &
      "var F51= '" & [Visit 8 Labour Qty] & "';" &
      "var F53= '" & [Visit 9 Date] & "';" &
      "var F54= '" & [Visit 9 Time] & "';" &
      "var F55= '" & [Visit 9 Labour Qty] & "';" &
      "var F57= '" & [Visit 10 Date] & "';" &
      "var F58= '" & [Visit 10 Time] & "';" &
      "var F59= '" & [Visit 10 Labour Qty] & "';" &
      "$.getScript('" & URLRoot() & "db/[DBID]?act=dbpage&pagename=AddWR.js');void(0);"


  • 6.  RE: case statement in api_addrecord javascript

    Top
    Contributor
    Posted 02-11-2018 00:37
    That makes more sense. 

    Here is another mechanism I sometimes use to pass field values to script.

    Here is a Rich Text field styled as a button: that places two fields [Record ID#] and [Name] into attributes of the <a> element:
    [iol] & "module.js" & [/iol]
    &
    "<a class='QBU_Button Vibrant Success' " &
    "  data-rid='" & [Record ID#] & "'" &
    "  data-name='" & [Name] & "'" &
    ">Button</a>"
    The <a> portion of the generated field looks like this:
    <a 
      class="QBU_Button Vibrant Success" 
      data-rid="1" 
      data-name="Jane Doe" >Button</a>
    Within the module.js file you can gain access to the field values like this:
    $("a.QBU_Button").on("click", function(event) {
      var rid = this.dataset.rid;
      var name = this.dataset.name;
      console.log("rid=", rid);
      console.log("name=", name);
    });
    This allows you to style the button anyway you want and provides nice isolation between the formula and script.


  • 7.  RE: case statement in api_addrecord javascript

    Top
    Contributor
    Posted 02-11-2018 00:40
    Also, I sometimes pass just  the [Record ID#] and within the module.js make an AJAX call for any of the other fields values you need to access. This makes every thing dead simple with very good isolation between formula and script.


  • 8.  RE: case statement in api_addrecord javascript

    Posted 02-11-2018 00:42
    Brilliant thank you so much - I am in the process of building out my application, no doubt the IOL technique will be used in spades. 

    I look forward to our future correspondence. My plan is as i work through my different use cases that i post on here for anyone looking to do similar functions where i cant see a solution already posted.


  • 9.  RE: case statement in api_addrecord javascript

    Posted 02-11-2018 00:44
    As well as asking for help when I just can't work something out. I have only 8 years VBA experience (with specific use cases as I am self-taught) so interesting learning curve moving to Javascript.


  • 10.  RE: case statement in api_addrecord javascript

    Top
    Contributor
    Posted 02-11-2018 00:47
    Keep going.

    We have to levelup the other QuickBase users to use script so we can get into fun stuff like QuickBase Machine Learning and stop complaining about trifles like the color scheme.