Qlik Community

Knowledge

Search or browse our knowledge base to find answers to your questions ranging from account questions to troubleshooting error messages. The content is curated and updated by our global Support team

Announcements
Support Case Portal has moved to Qlik Community! Read the FAQs to start exploring Support resources.

How to: Loop and reduce with Qlik Application Automation (advanced)

Emile_Koslowski
Support
Support

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

Covered topics:

  1. Loop and reduce to one space
  2. 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:

  1. Go to the Qlik Cloud Services connector and search for the block "Loop And Reduce". Drag this block inside the automation editor.
  2. 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.
    Emile_Koslowski_1-1631100768236.png

     

  3. 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.
  4. 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:
    Emile_Koslowski_2-1631100991141.png
  5. 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.
  6. 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.
    Emile_Koslowski_3-1631101069904.png

     

  7. 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.
    Emile_Koslowski_4-1631101148856.png

     

  8. 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.
  9. 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".
    Emile_Koslowski_1-1633102363604.png
  10. 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:

Emile_Koslowski_0-1633102328191.png

 

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:

Emile_Koslowski_1-1631102067047.png

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.

Example:

 

[
	{
		"reduction_value": "Scotland",
		"space_id": "6138a3062c1054d8158c189a"
	},
	{
		"reduction_value": "Northern Ireland",
		"space_id": "6138a318faed485d36ae911f"
	},
	{
		"reduction_value": "England",
		"space_id": "6138a337faed485d36ae9126"
	},
	{
		"reduction_value": "Wales",
		"space_id": "6138a33f98b0d0bf7e719dfb"
	},
	{
		"reduction_value": "Guernsey",
		"space_id": "6138a349faed485d36ae912b"
	},
	{
		"reduction_value": "Isle of Man",
		"space_id": "6138a35aba392246d331e611"
	},
	{
		"reduction_value": "Jersey",
		"space_id": "6138a3653ec592fe53a8d55b"
	}
]

 

The next step is to store the mapping in the automation. Execute the following steps to do this:

  1. Search for the Variable block and add this to the automation.
  2. Go to the block's settings, click "Manage variables" and add a new variable of type list.
  3. Go to the operations section of the variable and set the first operation to empty the variable.
  4. 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".
  5. 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.
  6. Add the block to the beginning of the automation from the first part of this article:
    Emile_Koslowski_0-1631106827220.png

     

  7. 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.
  8. 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:
    Emile_Koslowski_0-1633102611369.png

     

  9. 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.
    Emile_Koslowski_1-1633102642378.png

     

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.

Labels (2)
Attachments
Version history
Revision #:
18 of 18
Last update:
3 weeks ago
Updated by:
 
Contributors