Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
rbartley
Specialist II
Specialist II

Qlik Sense Mashup applyPatches replace all measures

Hi everyone,

I have built a mashup where I start with a pivot table with two measures then depending on the option selected from a list, I either remove the second measure and/or replace the first measure with another value as follows:

 

if(intVisMeasures==2)
			{
					console.log('removing second measure');
					
					patches.push(
						{
						'qPath': '/qHyperCubeDef/qMeasures/1',
						"qOp": "remove"
						}
					);
				
			}

			patches.push( 
				{
					'qPath': '/qHyperCubeDef/qMeasures/0/qDef/qDef',
					"qOp": "Replace",
					'qValue':   strMeasure
				}
				,
				{
					'qPath': '/subtitle/qStringExpression/qExpr',
					"qOp": "Replace",
					'qValue':   strSubtitle
				}
			);

 

This works fine, but now I want to add the second measure back when the first option in the list is selected and there I am having an issue.  

error:

code:-32602

message:"Invalid method parameter(s)"

parameter:"JSON type error"

 

Here's what I'm passing:

 

strMeasures = JSON.stringify(strMeasures);

patches.push(

				{
					'qPath': '/qHyperCubeDef/qMeasures',
					"qOp": "replace",
					'qValue':   strMeasures
				}
				);

 

 

 

Any help gratefully appreciated.

1 Solution

Accepted Solutions
rbartley
Specialist II
Specialist II
Author

While I will continue to investigate the impact of patches being applied to multiple visualizations asynchronously, chaining the applyPatches calls has allowed me to progress with my current project and so I will close this discussion now.

View solution in original post

6 Replies
rbartley
Specialist II
Specialist II
Author

I notice that this works intermittently.  It seems that the applyPatches method sometimes applies app the patches, but on other occasions, it only applies one or two.  Has anyone else experienced the same problem?

 

Any ideas, @ErikWetterberg ?  

ErikWetterberg

Hi,

This looks a bit strange:

strMeasures = JSON.stringify(strMeasures)

for you applyPatch to work you should provide an array of measures (possibly with only one measure, but still an array) and then do JSON.stringify on it. 

rbartley
Specialist II
Specialist II
Author

Hi Erik,

Thanks for responding.

This is the definition of strMeasures (I got this from extracting the properties from the original pivot table using options/developer in Qlik Sense).

strMeasures = [
				{
					"qLibraryId": "",
					"qDef": {
					"qLabel": "Import",
					"qDescription": "",
					"qTags": [],
					"qGrouping": "N",
					"qDef": "Sum({<FLOW={'1'}>}[Product Tons])",
					"qNumFormat": {
						"qType": "F",
						"qnDec": 2,
						"qUseThou": 0,
						"qFmt": "#.##0,00",
						"qDec": ",",
						"qThou": "."
					},
					"qRelative": false,
					"qBrutalSum": false,
					"qAggrFunc": "",
					"qAccumulate": 0,
					"qReverseSort": false,
					"qActiveExpression": 0,
					"qExpressions": [],
					"qLabelExpression": "",
					"autoSort": true,
					"cId": "GaXKUM",
					"numFormatFromTemplate": true
					},
					"qSortBy": {
					"qSortByState": 0,
					"qSortByFrequency": 0,
					"qSortByNumeric": -1,
					"qSortByAscii": 0,
					"qSortByLoadOrder": 1,
					"qSortByExpression": 0,
					"qExpression": {
						"qv": ""
					},
					"qSortByGreyness": 0
					},
					"qAttributeExpressions": [
					{
						"qExpression": "",
						"qLibraryId": ""
					},
					{
						"qExpression": "",
						"qLibraryId": ""
					}
					],
					"qAttributeDimensions": [],
					"qCalcCond": {
					"qv": ""
					},
					"qCalcCondition": {
					"qCond": {
						"qv": ""
					},
					"qMsg": {
						"qv": ""
					}
					}
				},
				{
					"qLibraryId": "",
					"qDef": {
					"qLabel": "Export",
					"qDescription": "",
					"qTags": [],
					"qGrouping": "N",
					"qDef": "Sum({<FLOW={'2'}>}[Product Tons])",
					"qNumFormat": {
						"qType": "F",
						"qnDec": 2,
						"qUseThou": 0,
						"qFmt": "#.##0,00",
						"qDec": ",",
						"qThou": "."
					},
					"qRelative": false,
					"qBrutalSum": false,
					"qAggrFunc": "",
					"qAccumulate": 0,
					"qReverseSort": false,
					"qActiveExpression": 0,
					"qExpressions": [],
					"qLabelExpression": "",
					"autoSort": true,
					"cId": "AYQhg",
					"numFormatFromTemplate": true
					},
					"qSortBy": {
					"qSortByState": 0,
					"qSortByFrequency": 0,
					"qSortByNumeric": -1,
					"qSortByAscii": 0,
					"qSortByLoadOrder": 1,
					"qSortByExpression": 0,
					"qExpression": {
						"qv": ""
					},
					"qSortByGreyness": 0
					},
					"qAttributeExpressions": [
					{
						"qExpression": "",
						"qLibraryId": ""
					},
					{
						"qExpression": "",
						"qLibraryId": ""
					}
					],
					"qAttributeDimensions": [],
					"qCalcCond": {
					"qv": ""
					},
					"qCalcCondition": {
					"qCond": {
						"qv": ""
					},
					"qMsg": {
						"qv": ""
					}
					}
				}
			];

strMeasures = JSON.stringify(strMeasures);

As I say, this seems to work intermitently and sometimes, the remove patch is not executed, but this patch is, so it seems to me to be a sequencing issue, but I'm happy to hear if you have any suggestions. 

If my  definition above is incorrect, could you please provide an example of what it should look like?

In the meantime, I notice that appyPatches returns a promise, so if it is a sequencing issue, I suppose I could break the changes down into individual patches and chain the calls to applyPatches. 

 

rbartley
Specialist II
Specialist II
Author

@ErikWetterberg

OK, so I think I've figured out what's happening.  The applyPatches code is called when the selected value of either of two lists is changed and this is called for two visualizations.  It seems that somehow these calls are interfering with each other and preventing the code from completing.  When I only call the applyPatches code on one of the visualizations, it works perfectly and consistently.

rbartley
Specialist II
Specialist II
Author

@erik_wetterberg

By forcing the script to wait until the the selection (field.selectMatch()) has been applied (using the deferred object) and by using the same approach to chain the calls to the applyPatches method, so that it calls on one visualization and then waits for it to complete before calling it on another , this has increased the reliability significantly and now no errors are returned.  However, very occasionally, it seems that some of the patches have not finished being applied. 

Since I am already using the following to wait for the function to complete I cannot see a way around this other than applying the patches individually.

vis.applyPatches( patches, false ).then(function()
{
	deferredObject.resolve();
});

 

Can you see anything wrong with my approach? Do you have any suggestions?

 

rbartley
Specialist II
Specialist II
Author

While I will continue to investigate the impact of patches being applied to multiple visualizations asynchronously, chaining the applyPatches calls has allowed me to progress with my current project and so I will close this discussion now.