8 Replies Latest reply: Feb 9, 2016 12:35 PM by Alexander Karlsson RSS

    How to filter with Qlik Engine API

    Peter Cappelle

      Hi,

       

      About a month ago i asked on the community what to do best. Working with the Qlik Engine API or go the Capabilities way.

      Against all advices (sorry for that) i went the Qlik Engine API way and made an AngularJS enviroment that communicates with Sense through the Qlik Engine API.

       

      I pretty sure that what i have now is the way it has to be done, it's fast and works like a charm.

       

      The only thing i don't understand is how i can "filter" inside of my app.

       

      Imagine this request:

       

      {
                  "method": "CreateSessionObject",
                  "handle": 1,
                  "params": [
                      {
                          "qInfo": {
                              "qType": "Chart"
                          },
                          "qHyperCubeDef": {
                              "qDimensions": [
                                  {
                                      "qLibraryId": "",
                                      "qDef": {
                                          "qFieldDefs": [
                                  "Year"
                                ]
                                      }
                            }
                          ],
                              "qMeasures": [
                                  {
                                      "qLibraryId": "",
                                      "qDef": {
                                          "qLabel": "Saldo",
                                          "qDef": "Sum(Saldo)"
                                      }
                                  }
                          ],
                              "qSuppressZero": false,
                              "qSuppressMissing": false
                          }
                      }
                    ]
      }
      

       

      Then i send my get request:

       

      {
                  "method": "GetHyperCubeData",
                  "handle": 2,
                  "params": [
          "/qHyperCubeDef",
          [
                          {
                              "qTop": 0,
                              "qLeft": 0,
                              "qHeight": 99,
                              "qWidth": 99
            }
          ]
        ]
       }
      

       

      That gives me the data i want, Sum(Saldo)/Years

       

      But what if want to filter and i want to update that cube to just Year 2009 and 2010, Which request should i have to send?

       

      Also pretend i have multiple cubes all with Year as a dimension, can i update the Year dimension of every cube?

        • Re: How to filter with Qlik Engine API
          Alexander Karlsson

          That's awesome! Great to hear success stories around the Engine API

          There are a few different ways, either you can define a Generic Object with a qListObjectDef that contains the Year field and you make your selections in that. Or you can use the Field class and select through that, for example Select method

           

          Now the beauty with Qlik is that once you have selecting for example 2009 in the Year field all you would have to do is to call GetLayout on your other Generic Objects and they will automatically have filtered to reflect your selections in the app.

            • Re: How to filter with Qlik Engine API
              Peter Cappelle

              Hi Alexander en thx for your reply!

               

              I don't know if this is a good practice but i always try to send requests through the dev-hub > Engine API Explorer first to see if what i get back from Qlik is the data i want.

               

              There and with the Qlik dev help i found something i can use like this:

               

              1. Make just a random object:

               

              {
                "jsonrpc": "2.0",
                "method": "CreateObject",
                "handle": 1,
                "params": [
                {
                "qInfo": {
                "qId": "LB01",
                "qType": "ListObject"
                },
                "qListObjectDef": {
                "qDef": {
                "qFieldDefs": [
                "Country"
                ],
                "qFieldLabels": [
                "Country Label"
                ],
                "qSortCriterias": [
                {
                "qSortByLoadOrder": 1
                }
                ]
                },
                "qInitialDataFetch": [
                {
                "qTop": 0,
                "qLeft": 0,
                "qHeight": 3,
                "qWidth": 1
                }
                ]
                }
                }
                ]
              }
              
              
              
              
              


              It is clear for me that with this request i select the value on the second position of the previous response


              { 
                  "jsonrpc": "2.0", 
                  "id": 4, 
                  "method": 
                  "SelectListObjectValues", 
                  "handle": 2, 
                  "params": 
                      [ 
                          "/qListObjectDef", 
                          [ 1 ], 
                          false 
                      ] 
              }
              
              
              
              
              

               

               

              So now when i do getLayout i got something like this:

               

              "qMatrix": [ 
                  [ 
                      { 
                          "qText": "United States", 
                          "qNum": "NaN", 
                          "qElemNumber": 0, 
                          "qState": "X" 
                      }
                  ], 
                  [ 
                      { 
                          "qText": "Denmark", 
                          "qNum": "NaN", 
                          "qElemNumber": 1, 
                          "qState": "S" 
                      } 
                  ], ....
              
              
              
              
              

               

              In fact if i do getLayout on my other cubes i see that the selection i made previously is extended to there to! So this is what i want!

               

              Is this the way to go? Am i on the right track to succesfully filter?

               

              If i am, then where can i find the parameters that i can apply in here "params": [ ... ]?

              I have seem quiet some examples using:


                  "params":
                      [
                          "/qListObjectDef",
                          [ 1 ],
                          false
                      ]
              

               

              or

               

                  "params":
                      [
                          "/qListObjectDef",
                          [ 0 ],
                          [ 0 ],
                          false
                      ]
              

               

              But is it possible to do something like this?

              
              
                  "params":
                      [
                          "/qListObjectDef",
                          [ "Denmark" ],
                          false
                      ]
              

               

              Thanks again!

               

              Peter

              • Re: How to filter with Qlik Engine API
                Øystein Kolsrud

                I would probably go with akls suggestion of going through the field class select method, but I just want to mention that it is also possible to do selections based on your hypercube date. You do that through methods in the GenericObject class just like you can for list objects (either SelectHyperCubeCells or SelectHyperCubeValues). If your GetHyperCubeData-call returned the following table:

                 

                YearSaldo
                201140123
                200939123
                201245032
                201037856

                 

                then you could call SelectHyperCubeCells on your session object and select the 2009 and 2010 values like this:

                 

                {

                  "method": "SelectHyperCubeCells",

                  "handle": 2,

                  "params": [

                    "/qHyperCubeDef",

                    [ 0 ],

                    [ 1, 3 ],

                    True

                  ]

                }

                 

                This call will select row 1 and 3 from column 0, which means that the values 2009 and 2010 will be selected.

              • Re: How to filter with Qlik Engine API
                Peter Cappelle

                thx Øystein Kolsrud for this example, Now i know how to use these index numbers.

                 

                I have another question... I am having problems with my web socket connection, mostly because i never used it before. I don't know if there is someone who has ever made an angular enviroment with web sockets and Qlik Sense? My question is, if i have to make a new web socket connection every time when i make a listObject or hyperCubeObject? Or do i have to make a run block and put the "new websocket" inside of that so that the connection opens before the rest of my code and stays open the whole time?