Can't serialize jsonObject in camunda embedded form

Here is may java Delegate class which i use for getting output variable:
public class DocumentItem implements JavaDelegate{

	public void execute(DelegateExecution arg0) throws Exception {
		JSONObject jsonObject1;
	    
		JSONArray jsonArray = new JSONArray(arg0.getVariable("restResponse").toString());
	
        for(int i=0;i<jsonArray.length();i++)
        {
        	 jsonObject1 = jsonArray.getJSONObject(i);
        
            
        }
        
	}

}

and i want to create camVariable in my start form :
var documentData = $scope.documentData = {
};

    // hook into camunda SDK JS Form Lifecycle
    camForm.on('form-loaded', function() {

     
      camForm.variableManager.createVariable({
        name: 'jsonObject1',
        type: 'Object',
        value: jsonObject1,
        valueInfo: {
          // indicate that object is serialized as json
          serializationDataFormat: 'application/json',
          objectTypeName: 'org.camunda.bpm.engine.impl.util.json.JSONObject'
          
      
        }
      });

    });




</script>
<h2>My job List</h2>																							
	<div>
		<table style="width:100%;">
			<thead>
				<tr>
					<th style="width:140px;">id</th>
					<th style="width:305px;">organizationNameEN</th>
					<th style="width:250px;">organizationNameGe</th>
					<th style="width:75px;" >cardNumber</th>
					<th></th>
				</tr>			
			</thead>
			<tbody>
				<tr>
					<td><input style="width:140px;" type="long" id="id" class="control-group"  ng-model="jsonObject1.id" readonly /></td>
					<td><input style="width:305px;" type="text" id="cardNumber" class="control-groupl"  ng.model="jsonObject1.cardNumber"  /></td>
					<td><input style="width:250px;" type="text" id="OrganizationNameGE" class="control-group" ng.model="jsonObject1.OrganizationNameGE"/></td>			
					<td><input style="width:25px; height:25px;" type="checkbox" ng-model="chekselct"								 					 
							   								    cam-variable-name="isItemSelected"													
							   								    cam-variable-type="Boolean" /></td>				   								    
					<td ng-show="chekselct">extra Data: <input style="width:60px;" type="number"
																	   		    cam-variable-name="id" min="1"
																	   		    cam-variable-type="Integer" /></td>	
				</tr>
			</tbody>
		</table>
	</div>
</form>

But every time i deploy war file in camunda environment i got this error:Form failure: jsonObject1 is not defined
What should i change to make this code work?

Hi @Sally,

I’m not sure whether I understand it correct.

  1. Do you want to create a process variable jsonObject1 in the delegate?
  2. Do you want to get this process variable in the HTML form?

When you can answer both questions with YES , you missed something in the Java delegate code and you use the wrong JS code snippet in the HTML form.

You have to create the process variable in the Java delegate:

arg0.setVariable("jsonObject1", jsonObject1);

In the HTML form, you need the following code to get the process variable jsonObject1.

  <script cam-script type="text/form-script">
    camForm.on('form-loaded', function() {
      // tell the form SDK to fetch the variable named 'jsonObject1'
      camForm.variableManager.fetchVariable('jsonObject1');
    });
    camForm.on('variables-fetched', function() {
      // work with the variable (bind it to the current AngularJS $scope)
      $scope.jsonObject1 = camForm.variableManager.variableValue('jsonObject1');
    });
  </script>

Cheers
kristin

Hi @kristin
Thank you for your reply:
1.will i need to use code like this in html?
camForm.on(‘form-loaded’, function() {
// tell the form SDK to fetch the variable named ‘document’
camForm.variableManager.fetchVariable(‘jsonObject1’);
});
camForm.on(‘variables-fetched’, function() {
// work with the variable (bind it to the current AngularJS $scope)
$scope.jsonObject1 = camForm.variableManager.variableValue(‘jsonObject1’);
});

@Sally, that’s correct.

Sorry, I forgot to use the correct code formatting in my previous answer.

Cheers
kristin