7 Replies Latest reply on Dec 8, 2014 8:44 AM by Wesley Cloven

    Cannot read property getWorkbook of undefined

    Wesley Cloven

      I'm getting the following error when attempting to apply filters using the JavaScript API.

       

      Uncaught TypeError: Cannot read property 'getWorkbook' of undefined

       

      I initialize the report with the following.  It works and does display the initial unfiltered report.

       

      var viz, workbook, activeSheet;

      window.onload=function() {

        var vizDiv = document.getElementById('viz');

        var vizURL = 'http://myserver/trusted/yMNF_ZLCRpDtyKQ987cTZyP-/views/NLN_Participant_ExamScores/Participant_ExamScoreDashboard?:embed=yes&:toolbar=yes';

        var options = {

          onFirstInteractive: function () {

            workbook = viz.getWorkbook();

            activeSheet = workbook.getActiveSheet();

          }

        };

        viz = new tableauSoftware.Viz(vizDiv, vizURL, options);

      };

       

      I then attempt to use the following code to apply a filter.

        • 1. Re: Cannot read property getWorkbook of undefined
          Jeff D

          Hi Wesley, it sounds like there's an error when loading the viz.  Check the debug console window for error messages.  Also, if you haven't already done so, try using a URL without a trusted ticket and without the parameters.

          • 2. Re: Cannot read property getWorkbook of undefined
            Russell Christopher

            You have a trusted ticket in your URL path (.../trusted/<ticket is here>/views/....). I assume you are replacing it each time you request the viz? A ticket can only be used once.

            • 3. Re: Cannot read property getWorkbook of undefined
              Wesley Cloven

              Thanks for the reply.  I do replace the ticket every time.  The initial report shows, but applying filters fail generating the error.

              • 4. Re: Cannot read property getWorkbook of undefined
                Wesley Cloven

                Jeff, The error I listed is the only one showing in the console.  I can use viz.hide() and viz.show() without error, but that is about it.

                • 5. Re: Cannot read property getWorkbook of undefined
                  Wesley Cloven

                  It looks like my original post cut off the filter code I was using.

                   

                  var worksheet;

                   

                  viz.getWorkbook().activateSheetAsync('Participant_ExamScoreDashboard').then(function (sheet) {

                    worksheet = sheet;

                  })

                  .then(function () {

                    return worksheet.applyFilterAsync('Participant Name', 'EB5C721F258546E6814FE3533616113D', tableauSoftware.FilterUpdateType.REPLACE);

                  })

                  .then(function () {

                    return worksheet.applyFilterAsync('Schedule Name', '177 - End of Term Exam 2014-01', tableauSoftware.FilterUpdateType.REPLACE);

                  })

                  .otherwise(function (err) {

                    console.log(err);

                  });

                   

                  I also tried the following:

                   

                  activeSheet.applyFilterAsync('Participant Name', 'EB5C721F258546E6814FE3533616113D', tableauSoftware.FilterUpdateType.REPLACE);

                  activeSheet.applyFilterAsync('Schedule Name', '177 - End of Term Exam 2014-01', tableauSoftware.FilterUpdateType.REPLACE);

                   

                  That generated the console error:
                  Uncaught TypeError: Cannot read property 'applyFilterAsync' of undefinedreports:151 (anonymous function)

                  • 6. Re: Cannot read property getWorkbook of undefined
                    Russell Christopher

                    You're applying the filter to a Dashboard, it appears ('Participant_ExamScoreDashboard'). Filters are associated with sheets. Try grabbing the sheet itself inside the dashboard and applying your filter to that.

                    • 7. Re: Cannot read property getWorkbook of undefined
                      Wesley Cloven

                      Thanks Russell, you got me in the right direction.

                       

                      I ended up updating my options onFirstInteractive: to:

                        var options = {

                          onFirstInteractive: function () {

                            workbook = viz.getWorkbook();

                            activeSheet = workbook.getActiveSheet();

                            setParticipant();    }

                        };

                        viz = new tableauSoftware.Viz(vizDiv, vizURL, options);

                       

                      then adding the following function to set initial filters:

                      function setParticipant() {

                          var worksheets = activeSheet.getWorksheets();

                          for(var i = 0; i < worksheets.length; i++) {

                            if (worksheets[i].getName() == 'Participant_ExamScore') {

                              worksheets[i].applyFilterAsync('Participant Name', participantName, tableauSoftware.FilterUpdateType.REPLACE);

                            }

                          }

                      }

                       

                      I had a form submit to update filtering which I think was messing things up.  I switched to using an onChange event on my select calling:

                       

                      function assessmentChange() {

                            var scheduleName = document.getElementById('assessment').selectedOptions[0].value;

                       

                            if (scheduleName != '_none') {

                                  var worksheets = activeSheet.getWorksheets();

                                  for(var i = 0; i < worksheets.length; i++) {

                                    if (worksheets[i].getName() == 'Participant_ExamScore') {

                                      worksheets[i].applyFilterAsync('Schedule Name', scheduleName, tableauSoftware.FilterUpdateType.REPLACE);

                                    }

                                  }

                                  viz.show();

                            } else {

                                viz.hide();

                            }

                      }

                       

                      This got me up and running.  Thanks everyone for all of your suggestions!

                       

                      Wes