[Embedded Form] How to initialize JSON variable if it does not exist

Hi,

I run on Camunda Run 7.15.0

I have to modify var1 and var2 in an embedded form. Both variables are retrieved from a json variable myjson.

My challenge is that under circumstances, myjson does not exist in the process instance.

How can I create myjson and its inner variables conditionally if they are not present in process instance

Snippet:

   <script cam-script type="text/form-script">
  camForm.on('form-loaded', function() {
    camForm.variableManager.fetchVariable('myjson');
  });
  camForm.on('variables-fetched', function() {
    $scope.myjson = camForm.variableManager.variableValue('myjson');
  });
</script>

    <h3>Data</h3>
    <table class="table table-striped">
      <tr>
         <td>Var 1</td>
        <td><input type="text" id="input_var1"
           ng-model="myjson['var1']"/></td>
         <td>Var 2</td>
        <td><input type="text" id="input_var2"
           ng-model="myjson['var2']"/></td>
      </tr>

      </tr>
    </table>

Thank you,
Markus

Hi @Noordsestern,

Try below code

<form role="form" name="frmTest">
	  
<script cam-script type="text/form-script">
inject([ '$rootScope', '$scope', function($rootScope, $scope) {

  var variableManager = camForm.variableManager;

  camForm.on('form-loaded', function() {

    variableManager.fetchVariable('myjson');
  });

  camForm.on('variables-fetched', function() {

	if (variableManager.variableValue('myjson')) {
	
		$scope.myjson = variableManager.variableValue('myjson');

	} else {
		
		$scope.myjson = {};
		$scope.myjson.var1 = "initVar1";
		$scope.myjson.var2 = "initVar2";

		variableManager.variable('myjson').type = 'json'
		variableManager.variable('myjson').value = $scope.myjson;
	}
  });
}]);
</script>

<h3>Data</h3>
<table class="table table-striped">
	<tr>
		<td>Var 1</td>
		<td><input type="text" id="input_var1" ng-model="myjson['var1']" /></td>
		<td>Var 2</td>
		<td><input type="text" id="input_var2" ng-model="myjson['var2']" /></td>
	</tr>
	</tr>
</table>
</form>
1 Like

Great! Thank you @hassang Only had a small typi missing camForm in front of these lines:

		variableManager.variable('myjson').type = 'json'
		variableManager.variable('myjson').value = $scope.myjson;

The final script was:

inject([ '$rootScope', '$scope', function($rootScope, $scope) {

  var variableManager = camForm.variableManager;

  camForm.on('form-loaded', function() {

    variableManager.fetchVariable('myjson');
  });

  camForm.on('variables-fetched', function() {
        $scope.myjson = variableManager.variableValue('myjson');

	if ($scope.myjson) {

	} else {
		
		$scope.myjson = {};
		$scope.myjson.var1 = "initVar1";
		$scope.myjson.var2 = "initVar2";

		camForm.variableManager.variable('myjson').type = 'json'
		camForm.variableManager.variable('myjson').value = $scope.myjson;
	}
  });
}]);

Hi @Noordsestern,

As long as camForm.variableManager is assigned to the javascript variable “variableManager” then using only javascript variable “variableManager” should work