How to: Loop and reduce with Qlik Application Automation (advanced)
This article explains how a loop and reduce operation can be performed on a Qlik Sense app by using Qlik Application Automation. It covers two examples, in the first one, the reloaded apps will be (re)published to one space. In the second example, each reduced app will be (re)published to a separate space.
This article covers two advanced examples, a more basic example can be found here
Loop and reduce to one space
Loop and reduce to multiple spaces
The source app used in this article can be downloaded here
Attached to this article are 2 files of the exported automations used in this article. More information on importing automations can be found here.
1. Loop and reduce to one space
In this example, all reduced apps will be (re)published to the same space.
Create a new automation and follow these steps to perform a loop and reduce action on a QCS app:
Go to the Qlik Cloud Services connector and search for the block "Loop And Reduce". Drag this block inside the automation editor.
Specify the id for the app that should be used as a source app. The reduction will be performed on this app. Next, specify the field name over which the loop should run, for each unique value of the specified field name, a reduced app will be created. In this example, CountryName will be used as the reduction value.
The reduced apps are stored on the Temporary File services. The Loop And Reduce block will return a list of file ids together with the unique value of the reduction field and a list of sheet ids for the sheets that were public on the source app.
The next step is to import the apps from the Temporary File services. To do this search for the block "Import App From Export Id" and drag this block in the loop that's created by the Loop And Reduce block. This block will import one app from the temporary file services into your personal space. Tip: specify the reduced value that's outputted by the Loop and Reduce block in the Name parameter of the Import block. See the below image for an example:
When an app is imported, all sheets will be made private, this is the default behavior of the import app block. The next step is to publish the sheets that were public in the source app.
Because each reduced app will have a list of sheets that were public, a Loop block is necessary to loop over the list. Search for a "Loop" block in the block menu or find it under Basic and connect it right below the "Import App From Export Id" block. Specify the "Public Sheets" list returned by the Loop And Reduce block as the value for the "Loop over items of list" input parameter of the Loop block.
Now to publish each sheet of the imported app, search for the block "Publish Sheet" in the Qlik Cloud Services connector and drag that block in the loop generated by the "Loop" block. In this block, specify the id that's returned by the Import App block as App Id and use the current item in the loop as Sheet Id.
Nex step is to publish or republish the imported app to a managed space. This can be done with the block "Publish App To Managed Space". Search for this block in the Qlik Cloud Services connector and connect it below the loop block. Set the Run Mode to "(Re)publish by name", if the managed space already contains an app with the same name as the imported app, the imported app will be republished, else it will be published.
In the configuration of the Publish App To Managed Space block, set the App Id equal to the id returned by the Import App From Export Id block. For the Space Id, perform a lookup or manually enter an id, make sure you're specifying a managed space. The Data parameter should be set to "source".
Finally, when your app is published to a managed space, the imported app will still exist in your personal space. To remove this app, search for the block "Delete App" in the Qlik Cloud Services connector and connect it to the Publish App To Managed Space block. Specify the id returned by Import App From Export Id as the App Id in the block's settings.
Below is an image of the completed automation:
Attached example file: loop_and_reduce_to_same_space.json
2. Loop and reduce to multiple spaces
In this example, each reduced app will be published to a different space. We'll use the Insurance Claims app and the automation for the first example again. Since this app reduces on the field CountryName, we'll start by creating a managed space for each unique value that's found for CountryName. This results in the following list of spaces:
Next is to create a mapping for each unique reduction value and the corresponding space's id. There are multiple ways to achieve this. In this example, this mapping is stored in a JSON list of objects with the keys reduction_value and space_id. Feel free to use an automation to create the managed spaces and build this list.
The next step is to store the mapping in the automation. Execute the following steps to do this:
Search for the Variable block and add this to the automation.
Go to the block's settings, click "Manage variables" and add a new variable of type list.
Go to the operations section of the variable and set the first operation to empty the variable.
Add a second operation "Merge other list into..." and click the List input field of that operation, in the dropdown that appears, select "Add Formula".
From the formula picker, search for the formula "Object" and select it. Copy the JSON mapping to the json input field of the Object formula and click Save.
Add the block to the beginning of the automation from the first part of this article:
The next step is to find the right space id for each reduced app. To do that, add the block "Lookup Item In List" in the automation right before the block Publish App To Managed Space.
Go to the settings of the Lookup Item In List block and specify the variable that contains space_id/reduction_value mapping as input list. The condition inside the lookup should compare the mapping's reduction_value to the Reduced Value of the current item in the loop of Loop And Reduce. See the below image for an example:
Finally, specify the space_id from the output of the lookup block as Space Id in the settings of the Publish App To Managed Space.
Attached example file: loop_and_reduce_to_mulitple_spaces.json
The information in this article is provided as-is and to be used at own discretion. Depending on tool(s) used, customization(s), and/or other factors ongoing support on the solution below may not be provided by Qlik Support.