11 Replies Latest reply on Mar 10, 2014 3:52 AM by Gabor Tolnai

    Changing Parameter through Javascript API

    Nay Lin Soe

      Hi all,

       

      Has anyone ever tried using Javascript API to change workbook parameter values? I've given it a go, however it is not setting successfully (somehow the reference is not right).

       

      I've created a simple version and attached with this message. The workbook needs to be published and the URL variable in the Javascript file needs to point to the published view. The idea is to set the parameter 'Measure' by clicking on either 'Sales' or 'Profit' buttons.

       

      Neither Javascript API documentation nor the tutorial provides an example of using changeParameterValueAsync() function. Any insight will be greatly appreciated.

       

      Regards,

      Nay

        • 1. Re: Changing Parameter through Javascript API
          Russell Christopher

          Try this:

           

          function changeParameterValueAsync() {

           

              mainWorkbook = mainViz.getWorkbook();

              // Set Top Customers parameter to 10

              mainWorkbook.changeParameterValueAsync('Top Customers', 10).then(function () {

                  alertOrConsole("'Top Customers' parameter set to 10");

              });

          }

          • 2. Re: Changing Parameter through Javascript API
            Nay Lin Soe

            Hi Russell,

             

            Thanks for looking. That is what I have done basically. In the js file:

            function changeParam(value)

            {

                workbook.changeParameterValueAsync('Measure', value)

                .then(function() {alert('success');})

                .otherwise(function(err) { alert('failed: ' + err);});

            }

             

            and in the html, it is:

            <button type="button" onclick="changeParam(1)">Sale</button>

            <button type="button" onclick="changeParam(2)">Profit</button>

             

            Every time I click on either button, the pop-up says:

            failed: Error: Invalid parameter: value

             

            I have double-checked the name and allowed values of the parameter and all are right. So I really can't think of anything else to change here.

             

            Nay

            • 3. Re: Changing Parameter through Javascript API
              Nay Lin Soe

              I haven't had any luck with this after all these days. I am still unable to make this simple page work. I contacted the support, however they said Javascript API is beyond their scope. If there is anyone who has cracked it, could I really use a hand here please?

              • 4. Re: Re: Changing Parameter through Javascript API
                Eric Howard

                Hello Nay,

                 

                If you do not click the "Show Params" button, does it succeed?  I was able to use your sample files to mock up a repro and if I do not hit Show Params button, it succeeds in changing the parameter.

                 

                The reason is due to a known issue that is expected to be fixed in a future release in which calling getParametersAsync() prevents subsequent calls of changeParameterValueAsync() from succeeding.

                 

                Please let me know if this is consistent with your experiences, and I will report your experience to our engineering team.

                 

                Eric

                1 of 1 people found this helpful
                • 5. Re: Re: Re: Changing Parameter through Javascript API
                  Nay Lin Soe

                  Hello Eric,

                   

                  I'm very glad you look into this. I've tried changing the parameters WITHOUT clicking "Show Params" button. In my case it is still not successful. Since you managed to make it work with the same file, I'm intrigued. Might that be something to do with our server (using 8.1.1) or the browser (Firefox, Chrome)?

                   

                  Interesting thing to note though is that the error messages returned from the two cases (without clicking "Show Params" and after clicking it) are different. (I have included the screenshots.) I suppose that is because of the known issue that you were referring to, involving the use of getParametersAsync().


                  Nay

                  • 6. Re: Re: Re: Re: Changing Parameter through Javascript API
                    Eric Howard

                    Hello Nay,

                     

                    Here is my example using a viz published on Tableau Public (attached). Let me know if you get both errors with this example. I know I was getting the error you received without clicking 'Show Params' when my parameter name didn't match in the workbook, so double check for "Measure" matching exactly in the workbook as it does in the javascript (trailing space?).

                     

                    The annotation in this view will show the parameter value change when the buttons are pushed if it is successful.

                    2 of 2 people found this helpful
                    • 7. Re: Re: Re: Re: Re: Changing Parameter through Javascript API
                      Nay Lin Soe

                      Hello Eric,

                       

                      Thanks for this. I've looked up your sample and did a few tests. Here is what I have found out:

                       

                      In calling changeParameterValueAsync('Measure', value), the argument value must be assigned to what is specified in 'Display As' of the parameter, not the 'Value'. In my original case, the parameter 'Measure' is defined as:

                      ValueDisplay As
                      1Sale
                      2Profit

                      And it wasn't working because I was using 1 and 2 in the JS function. I must actually use what's in 'Display As' (strings) to make it work although the underlying values are integers. (In your example, there is no such problem because 'Value' and 'Display As' are the same.

                       

                      Is it by design, in which case, it is different from the URL action arguments because there I can set parameters by their 'Value'?

                       

                      Nay

                      • 8. Re: Changing Parameter through Javascript API
                        Eric Howard

                        Hello Nay,

                         

                        That is good information, thank you very much!  I will pass this on to our engineering team and see if this behavior is by design.

                        • 9. Re: Changing Parameter through Javascript API
                          Eric Howard

                          Hello Nay,

                           

                          This is currently by design and our engineering team is looking at allowing either the alias or the value to be passed into the method in a future release.

                           

                          Thanks again!

                          1 of 1 people found this helpful
                          • 10. Re: Changing Parameter through Javascript API
                            Nay Lin Soe

                            Thanks Eric. I'll be making sure to use 'Display As' entries in setting parameter values in the meantime.

                            1 of 1 people found this helpful
                            • 11. Re: Changing Parameter through Javascript API
                              Gabor Tolnai

                              Even if the changeParameterValueAsync is sorted out, there is another problem. I'm trying to store the changed parameters, and restore them later.

                              I tried to subscribe to the parameter change event, and I get the display name in the event, but I need to use the other name to restore this parameter. If I hide the parameter name in the UI, it is even worse, in the parameter change event I just get an empty name, and I have no idea, which parameter was changed (if I have multiple ones)...

                               

                              So the event handling should be changed, that it should send not (just) the name on the UI.