Skip to main content
Announcements
Customer Spotlight: Discover what’s possible with embedded analytics Oct. 16 at 10:00 AM ET: REGISTER NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Dan01
Contributor II
Contributor II

Qlik Engine API - Access and Select Field Values

Problem Description: By using the Engine API we are trying to build a script that dynamically cycles through the values of 3 fields and updates the contents of a sheet accordingly. Getting the cardinal for each field works, however for getting the actual values fails (theses are needed in another script, not discussed here)

Attempted Solutions:

  • connect to the Engine API on port 4747

  • establish a synchronous socket connection

  • open the required app by using the “OpenDoc” method. From the json response the application_handle is extracted

  • using the application_handle a Session Object is created

From here 2 approaches have been tested:

  1. Access and update the fields directly from the Application level

        • use the app_handle and the “GetField” method to obtain the field_handle

        • using the field Handle and the “GetCardinal” method we get the cardinal

        • using the app_handle we get responses on other methods like “GetFieldDescription”

        • However trying to use the “GetFieldValues” fails with “Method not found” error

  1. Access and update the fields directly from the Sheet level

        • essentially an additional step is added where a specific sheet is selected and its handle used in tandem with the “GetFieldValues” method, with the same error as the result

Blocking Points:

- getting the actual values of any particular Field

- updating the sheet and consequently its visualizations, after the values for the 3 Fields have been updated

Questions:

Is it possible to get a json response containing an array with the Values of a particular Field? Or is such a method missing from the backend Engine API?

If the answer is yes to the first question can anyone provide a working example for the proper chain of the Requests and their syntax?

Thanks a lot in advance for your time and effort

(if these questions have already been answered please provide a link)

Labels (3)
1 Solution

Accepted Solutions
alex_colombo
Employee
Employee

Hey @Dan01 if you want to get all values for a specific field, you can create a ListBox and get values. Here how to do it.

View solution in original post

5 Replies
alex_colombo
Employee
Employee

Hey @Dan01 if you want to get all values for a specific field, you can create a ListBox and get values. Here how to do it.

Dan01
Contributor II
Contributor II
Author

Hello @alex_colombo  and thanks for the suggestion

However for my use case the provided example does not work as expected.

- Firstly I use Python instead of JavaScript (that was my mistake, I forgot to mention the language)

- Secondly I get a "Method not found" when I try to use the provided methods in a Python script (probably the params I use are off, especially the handle)

So far, the approach that functions for me is: once I get to the application level to use a sequence of synchronous requests to the following methods: "GetField" => "GetFieldAndColumnSamples" => "Select" 

Of course when doing that one has to pay attention to the handle used to make the requests

The problem I have is that if I use the "GetCardinal" method, there is no link between the numbers and the values of that particular field. As you probably may have guessed I need this in order to make selections for several fields at once, and based on those update a number of sheets in a particular application. Should I open a new thread or continue on this one?

alex_colombo
Employee
Employee

@Dan01 could you please share your Python code here? I should be able to translate it from JS.

About my code shared in this thread, once the Qlik app is opened in your code, please use all methods I'm using in my code after enigmaModel object, such as createSessionObject.

Dan01
Contributor II
Contributor II
Author

Hi @alex_colombo 

Below you can find the code I am using in Python to get all the unlocked combinations of 3 fields inside an Application. The problem is that I get 60% more that the actual number of valid combinations.

Would appreciate your guidance on what are the methods and what the proper sequence of requests is to get all the combinations of unlocked values. I should mention that the number of valid options for one field changes according to a selection made in the previous one.

To get the field values, inside the get_field_values() function, I use 2 Methods:  "GetField" and  "GetFieldAndColumnSamples" which correctly return all the possible options for a specific field (locked and unlocked).

Thanks a lot for your help

-------------------------------------------------------------------------------------------

 

def send_request(ws, request_id, method, params=None, handle=-1):
    if params is None:
        params = {}
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": method,
        "handle": handle,
        "params": params
    }
    ws.send(json.dumps(request))
    response = ws.recv()
    return json.loads(response)

ws = websocket.create_connection(url, 
                                 sslopt=certs, 
                                 header={"X-Qlik-User": "UserDirectory=...; UserId=..."})

#define the fields to be selected
selection_fields = ['field_name_01', 'field_name_02', 'field_name_03']

# get the list of applications
send_request(ws, request_id, "GetDocList")
app_list_response = ws.recv()
request_id += 1

# sift through the list of apps to find the target app
doc_list = output_doc_list(app_list_response)
app_id = [doc[1] for doc in doc_list if compare_strings(doc[0], "Application Name")]

# Open the app
app_handle_response = send_request(ws, request_id, 'OpenDoc', {'qDocName': app_id[0]})
app_handle = app_handle_response['result']['qReturn']['qHandle']
request_id += 1

# create a list containing a tuple made of the name, handle and available values for each field
fields_list = []
for field_name in selection_fields:
    field_handle, field_values = get_field_values(field_name, request_id)
    fields_list.append((field_name, field_handle, field_values))

field_name_01, field_handle_01, field_values_01 = fields_list[0]
field_name_02, field_handle_02, field_values_02 = fields_list[1]
field_name_03, field_handle_03, field_values_03 = fields_list[2]
      
# find out all valid combinations of the 3 selection fields
for field_value_01 in field_values_01:
    clear_field = send_request(ws,
                              request_id,
                              "Clear",
                              [],
                              field_handle_01)
    request_id += 1
    select_field_response_01 = send_request(ws,
                                       request_id,
                                       "Select",
                                       {"qMatch": field_value_01,
                                        "qSoftLock": False
                                       },
                                       field_handle_01
                                    )
    request_id += 1
    select_valid_01 = select_field_response_01['result']['qReturn']        
    if select_valid_01:
        combination.append(field_value_01)
        for field_value_02 in field_values_02:
            clear_field = send_request(ws,
                                      request_id,
                                      "Clear",
                                      [],
                                      field_handle_02)
            request_id += 1
            select_field_response_02 = send_request(ws,
                                               request_id,
                                               "Select",
                                               {"qMatch": field_value_02,
                                                "qSoftLock": False
                                               },
                                               field_handle_02
                                            )
            request_id += 1
            select_valid_02 = select_field_response_02['result']['qReturn']
            if select_valid_02:
                combination.append(field_value_02)
                for field_value_03 in field_values_03:
                    clear_field = send_request(ws,
                                              request_id,
                                              "Clear",
                                              [],
                                              field_handle_03)
                    request_id += 1
                    select_field_response_03 = send_request(ws,
                                                       request_id,
                                                       "Select",
                                                       {"qMatch": field_value_03,
                                                        "qSoftLock": False
                                                       },
                                                       field_handle_03
                                                    )
                    request_id += 1
                    select_valid_03 = select_field_response_03['result']['qReturn']
                    if select_valid_03:
                        combination.append(field_value_03)
                        unlocked_combinations.append(combination.copy())
                        combination.pop()
                        request_id += 1
                combination.pop()
                request_id += 1
        combination.pop()

[print(f"{comb}\n") for comb in unlocked_combinations]

ws.close()

 

 

 

alex_colombo
Employee
Employee

Hey @Dan01 , not sure if I'm understanding correctly, but if you want to get all unlocked field values from a Qlik field, then you have to follow steps mentioned in above link (using ListBox). This is the only way for reading all values from a fields, including if values are selected, not selected, or locked.