# Create a variable

Create a variable by using the [CreateVariableEx method](https://help.qlik.com/en-US/sense-developer/May2025/Subsystems/EngineJSONAPI/Content/service-doc-createvariableex.htm).

You can create a transient variable by using the [CreateSessionVariable method](https://help.qlik.com/en-US/sense-developer/May2025/Subsystems/EngineJSONAPI/Content/service-doc-createsessionvariable.htm).

Create a script-defined variable using the [SetScript method](https://help.qlik.com/en-US/sense-developer/May2025/Subsystems/EngineJSONAPI/Content/service-doc-setscript.htm). By default, the type of a script-defined variable is variable.

Information noteIn a published app, only transient variables can be created.

## Examples

Example 1:

[](https://help.qlik.com/en-US/sense-developer/Subsystems/EngineAPI/Content/Sense_EngineAPI/WorkingWithAppsAndVisualizations/CreateVariables/create-variable.htm)

#### The goal is to create a variable. The app is not published.

1. Create the variable Variable01. The identifier of the variable is VB01. A comment (in qComment) and a definition (in qDefinition) are set.

The client sends:

```
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "CreateVariableEx",
  "handle": 1,
  "params": [
    {
      "qInfo": {
        "qId": "VB01",
        "qType": "Variable"
      },
      "qName": "Variable01",
      "qComment": "My first variable",
      "qDefinition": "=Count(Holes)"
    }
  ]
}
```

The engine returns:

```

 {
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "qReturn": {
      "qType": "GenericVariable",
      "qHandle": 2
    },
    "qInfo": {
      "qId": "VB01",
      "qType": "Variable"
    }
  },
  "change": [
    1,
    2
  ]
}
```

The variable is created and has 2 as a handle.

Example 2:

[](https://help.qlik.com/en-US/sense-developer/Subsystems/EngineAPI/Content/Sense_EngineAPI/WorkingWithAppsAndVisualizations/CreateVariables/create-variable.htm)

#### The goal is to create a transient hypercube with one dimension and one measure. A variable is used in the expression of the measure.

In this example, the hypercube contains one dimension: City and one measure: Sum(Quantity) \* Sum(UnitPrice) \* $(CurrencyConverter).

CurrencyConverter is a numeric variable whose initial value is 0.9.

This example shows how the data in the hypercube evolve when the value of the variable is changed.

1. Create the variableCurrencyConverter. The initial value is 0.9 and the identifier of the variable is VB01.

The client sends:

```
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "CreateVariableEx",
  "handle": 1,
  "params": [
    {
      "qInfo": {
        "qId": "VB01",
        "qType": "Variable"
      },
      "qName": "CurrencyConverter",
      "qDefinition": "0.90"
    }
  ]
}
```

The engine returns:

```
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "qReturn": {
      "qType": "GenericVariable",
      "qHandle": 2
    },
    "qInfo": {
      "qId": "VB01",
      "qType": "Variable"
    }
  },
  "change": [
    1,
    2
  ]
}
```

The variable is created and has 2 as a handle.

2.Create a chart with one dimension (qFieldDefs is City) and one measure (qDef is "Quantity \* UnitPrice \* $(CurrencyConverter)").

The client sends:

```
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "CreateSessionObject",
  "handle": 1,
  "params": [
    {
      "qInfo": {
        "qId": "BC01",
        "qType": "Chart"
      },
      "qHyperCubeDef": {
        "qDimensions": [
          {
            "qLibraryId": "",
            "qDef": {
              "qFieldDefs": [
                "City"
              ],
              "qFieldLabels": [
                "Label"
              ],
              "qSortCriterias": [
                {
                  "qSortByLoadOrder": 1
                }
              ],
              "qReverseSort": false
            }
          }
        ],
        "qMeasures": [
          {
            "qLibraryId": "",
            "qDef": {
              "qLabel": "Chart HC Properties measures label",
              "qDef": "Sum(UnitPrice)*Sum(Quantity)*$(CurrencyConverter)"
            }
          }
        ],
        "qSuppressZero": true,
        "qSuppressMissing": true,
        "qInitialDataFetch": [
          {
            "qTop": 0,
            "qHeight": 4,
            "qLeft": 0,
            "qWidth": 2
          }
        ]
      }
    }
  ],
  "cont": false,
  "delta": false
}
```

The engine returns:

{ "jsonrpc": "2.0", "id": 3, "result": { "qReturn": { "qType": "GenericObject", "qHandle": 3 } }, "change": [ 3 ] }

The chart is created. The handle of the chart is 3.

3. Get the layout of the chart. The handle of the request is 3 because the handle of the chart is 3.

The client sends:

{ "jsonrpc": "2.0", "id": 4, "method": "GetLayout", "handle": 3, "params": [] }

The engine returns:

{ "jsonrpc": "2.0", "id": 4, "result": { "qLayout": { "qInfo": { "qId": "BC01", "qType": "Chart" }, "qSelectionInfo": {}, "qHyperCube": { "qSize": { "qcx": 2, "qcy": 11 }, "qDimensionInfo": [ { "qFallbackTitle": "Label", "qApprMaxGlyphCount": 14, "qCardinal": 11, "qSortIndicator": "A", "qGroupFallbackTitles": [ "Label" ], "qGroupPos": 0, "qStateCounts": { "qLocked": 0, "qSelected": 0, "qOption": 11, "qDeselected": 0, "qAlternative": 0, "qExcluded": 0, "qSelectedExcluded": 0, "qLockedExcluded": 0 }, "qTags": [ "$text" ], "qDimensionType": "D", "qGrouping": "N", "qNumFormat": { "qType": "R", "qnDec": 14, "qUseThou": 1, "qFmt": "##############", "qDec": "," }, "qIsAutoFormat": true, "qGroupFieldDefs": [ "City" ] } ], "qMeasureInfo": [ { "qFallbackTitle": "Chart HC Properties measures label", "qApprMaxGlyphCount": 15, "qCardinal": 0, "qSortIndicator": "N", "qNumFormat": { "qType": "R", "qnDec": 0, "qUseThou": 0, "qFmt": "##############", "qDec": ",", "qThou": " " }, "qMin": 23430848494.625214, "qMax": 3061107211344.6104, "qIsAutoFormat": true, "qAttrExprInfo": [] } ], "qEffectiveInterColumnSortOrder": [ 0, 1 ], "qGrandTotalRow": [ { "qText": "26638214144658", "qNum": 26638214144657.777, "qElemNumber": -1, "qState": "X", "qIsTotalCell": true } ], "qDataPages": [ { "qMatrix": [ [ { "qText": "Berlin", "qNum": "NaN", "qElemNumber": 0, "qState": "O" }, { "qText": "24383185575,125", "qNum": 24383185575.125027, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "Mannheim", "qNum": "NaN", "qElemNumber": 1, "qState": "O" }, { "qText": "27182641851,718", "qNum": 27182641851.718, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "Aachen", "qNum": "NaN", "qElemNumber": 2, "qState": "O" }, { "qText": "26113833067,574", "qNum": 26113833067.574455, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "München", "qNum": "NaN", "qElemNumber": 3, "qState": "O" }, { "qText": "332427570656,48", "qNum": 332427570656.47638, "qElemNumber": 0, "qState": "L" } ] ], "qTails": [ { "qUp": 0, "qDown": 0 } ], "qArea": { "qLeft": 0, "qTop": 0, "qWidth": 2, "qHeight": 4 } } ], "qPivotDataPages": [], "qStackedDataPages": [], "qMode": "S", "qNoOfLeftDims": -1 } } } }

The first fourth values are returned for both the dimension and the measure (qHeight is 4 and qWidth is 2). When the dimension value is Berlin, the value of the measure is 24383185575.125027.

4. Update the value of the variable, so that it is 2. The handle of the request is 2 because the handle of the variable is 2.

The client sends:

{ "jsonrpc": "2.0", "id": 5, "method": "SetNumValue", "handle": 2, "params": [ 2 ] }

The engine returns:

{ "jsonrpc": "2.0", "id": 5, "result": {}, "change": [ 3 ] }

The variable CurrencyConverter has now for value 2.

5. Get the layout of the chart and check that the data are updated according to the new value of the variable.

The client sends:

{ "jsonrpc": "2.0", "id": 6, "method": "GetLayout", "handle": 3, "params": [] }

The engine returns:

{ "jsonrpc": "2.0", "id": 6, "result": { "qLayout": { "qInfo": { "qId": "BC01", "qType": "Chart" }, "qSelectionInfo": {}, "qHyperCube": { "qSize": { "qcx": 2, "qcy": 11 }, "qDimensionInfo": [ { "qFallbackTitle": "Label", "qApprMaxGlyphCount": 14, "qCardinal": 11, "qSortIndicator": "A", "qGroupFallbackTitles": [ "Label" ], "qGroupPos": 0, "qStateCounts": { "qLocked": 0, "qSelected": 0, "qOption": 11, "qDeselected": 0, "qAlternative": 0, "qExcluded": 0, "qSelectedExcluded": 0, "qLockedExcluded": 0 }, "qTags": [ "$text" ], "qDimensionType": "D", "qGrouping": "N", "qNumFormat": { "qType": "R", "qnDec": 14, "qUseThou": 1, "qFmt": "##############", "qDec": "," }, "qIsAutoFormat": true, "qGroupFieldDefs": [ "City" ] } ], "qMeasureInfo": [ { "qFallbackTitle": "Chart HC Properties measures label", "qApprMaxGlyphCount": 15, "qCardinal": 0, "qSortIndicator": "N", "qNumFormat": { "qType": "R", "qnDec": 0, "qUseThou": 0, "qFmt": "##############", "qDec": ",", "qThou": " " }, "qMin": 52068552210.278252, "qMax": 6802460469654.6895, "qIsAutoFormat": true, "qAttrExprInfo": [] } ], "qEffectiveInterColumnSortOrder": [ 0, 1 ], "qGrandTotalRow": [ { "qText": "59196031432573", "qNum": 59196031432572.836, "qElemNumber": -1, "qState": "X", "qIsTotalCell": true } ], "qDataPages": [ { "qMatrix": [ [ { "qText": "Berlin", "qNum": "NaN", "qElemNumber": 0, "qState": "O" }, { "qText": "54184856833,611", "qNum": 54184856833.611168, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "Mannheim", "qNum": "NaN", "qElemNumber": 1, "qState": "O" }, { "qText": "60405870781,596", "qNum": 60405870781.595551, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "Aachen", "qNum": "NaN", "qElemNumber": 2, "qState": "O" }, { "qText": "58030740150,165", "qNum": 58030740150.165459, "qElemNumber": 0, "qState": "L" } ], [ { "qText": "München", "qNum": "NaN", "qElemNumber": 3, "qState": "O" }, { "qText": "738727934792,17", "qNum": 738727934792.16968, "qElemNumber": 0, "qState": "L" } ] ], "qTails": [ { "qUp": 0, "qDown": 0 } ], "qArea": { "qLeft": 0, "qTop": 0, "qWidth": 2, "qHeight": 4 } } ], "qPivotDataPages": [], "qStackedDataPages": [], "qMode": "S", "qNoOfLeftDims": -1 } } } }

The data in the chart have changed. When the dimension value is Berlin, the value of the measure is now 54184856833.611168.

Example 3:

[](https://help.qlik.com/en-US/sense-developer/Subsystems/EngineAPI/Content/Sense_EngineAPI/WorkingWithAppsAndVisualizations/CreateVariables/create-variable.htm)

#### The goal is to define some variables in the script and check that these variables are seen as script-defined variables by the engine.

Prerequisite:

A session object that lists all the variables in the app with the type variable has been previously created. The identifier of this list is VL01. Two user variables (i.e variables not created through script) VB01 and VB02 have also been created.

1. Create two script-defined variables: vSales and vSales2.

The client sends:

{ "jsonrpc": "2.0", "id": 8, "method": "SetScript", "handle": 1, "params": [ "\r\nTRACE script set from file: E:\\GIT\\engine-sense-fork\\prod\\data\\Engine\\test\\qlikviewTests\\ProtocolTester4Net\\Resources\\\\CustomScript\\VariableScript\_DOC.txt;\r\nLOAD \* INLINE [\r\n Sales\r\n 150\r\n 200\r\n 240\r\n ];\r\nLet vSales = '=Sum(Sales)' ;\r\nLet vSales2 = '=Avg(Sales)' ;" ] }

The engine returns:

{ "jsonrpc": "2.0", "id": 8, "result": {} }

The script-defined variables are created. Per default, these variables have variable as type.

[SetScript method](https://help.qlik.com/en-US/sense-developer/May2025/Subsystems/EngineJSONAPI/Content/service-doc-setscript.htm)

2. Load the script.

{ "jsonrpc": "2.0", "id": 9, "method": "DoReload", "handle": 1, "params": [] }

The engine returns:

{ "jsonrpc": "2.0", "id": 9, "result": { "qReturn": true }, "change": [ 1, 2, 3, 4 ] }

The script is reloaded.

[DoReload method](https://help.qlik.com/en-US/sense-developer/May2025/Subsystems/EngineJSONAPI/Content/service-doc-doreload.htm)

3. Check the content of the list of variables VL01. The handle of the request is 2 because the handle of the list of variables is 2.

The client sends:

{ "jsonrpc": "2.0", "id": 10, "method": "GetLayout", "handle": 2, "params": [] }

The engine returns:

{ "jsonrpc": "2.0", "id": 10, "result": { "qLayout": { "qInfo": { "qId": "VL01", "qType": "VariableList" }, "qSelectionInfo": {}, "qVariableList": { "qItems": [ { "qName": "Variable01", "qDescription": "My first variable", "qDefinition": "=Count(Sales)", "qInfo": { "qId": "VB01", "qType": "variable" }, "qData": {} }, { "qName": "Variable02", "qDescription": "My second variable", "qDefinition": "=Sum(Sales)", "qInfo": { "qId": "VB02", "qType": "variable" }, "qData": {} }, { "qName": "vSales", "qDefinition": "=Sum(Sales)", "qInfo": { "qId": "ff28e996-20bc-4ba5-8915-32e7b4a9523c", "qType": "variable" }, "qData": {}, "qIsScriptCreated": true }, { "qName": "vSales2", "qDefinition": "=Avg(Sales)", "qInfo": { "qId": "796003fa-9958-415e-a515-f1be240276dd", "qType": "variable" }, "qData": {}, "qIsScriptCreated": true } ] } } } }

This list displays all user variables and all script-defined variables with the type variable in the app. The property qIsScriptCreated is set to true for the variables vSales and vSales2 because these variables have been defined through script.