Create a cam-variable

I’m trying to create a cam-variable after the user clicks completely, the value of the variable I’m looking for in a console api see that the value of the api is coming normally but the cam variable is not created what is missing, follows the cam-script code

camForm.on('submit', function(){
	$.ajax({
	url: 'http://localhost:3000/users',
	method: 'GET'	
	}).done(function(res){
		camForm.variableManager.createVariable({
  			       name: 'userName',
  			      type: 'String',
  			      value: res[0].name
            })
})

Hi @MarceloCP,

You have to set CAM variable in your form.

For example, suppose below.

<div class="form-group col-md-4">
                    <label for="inputEmail">Email</label>
                       <div class="controls">
                      <input class="form-control"
                             required
                             type="email"
                             cam-variable-name="email"
                             cam-variable-type="String"
                             placeholder="john.doe@camunda.org"
                             ng-minlength="2"
                             ng-maxlength="20" readonly/>
                    </div>
                  </div>

<script cam-script type="text/form-script">
        inject(['$rootScope','$http','Uri', function($rootScope,$http,Uri) {
          $scope.userName = $rootScope.authentication.name;
        camForm.on('form-loaded', function() 
      {
        $http.get(Uri.appUri("engine://engine/:engine/user/" + $scope.userName + "/profile")).then(function (result){
    console.log(result)
        $scope.firstName = result.data.firstName;
        $scope.lastName=result.data.lastName;
        $scope.email=result.data.email;
       },function (error){
    
       });
    
     })
        
       }]);
      </script>

Regards,
Kedari

Hi @MarceloCP,

Try to create the variable only without the ajax call. To see if the issue is related to the ajax call or not.

hello kedary,
I used the function as you posted only replaces the URI, but when I click on complete I see that the value is put in the input but if I look at the cockpit the variables was created but without the value seems to me that the value is retrieved before creating the cam-variable variable

hello hassang,

The variable created normally what happens that the value retrieved from the api is not inserted in it, if I put a fixed string the value appears, I am finding that the variable is created after my call in the api is made if I put an input in the form. that the value is entered in it but in the cockpit that value does not appear in the variable.

I was able to create the variable with the value coming from api, but only in form-loaded, but I need to submit it when the user create in complete

<input type="text" ng-model="userName"	
           cam-variable-name="userName"
           cam-variable-type="String"/>

<script cam-script type="text/form-script">
inject(['$http', 'Uri', function($http, Uri) {
  camForm.on('form-loaded', function() {
    $http.get(Uri.appUri("http://localhost:3000/servidores")).then(function(res){
		$scope.userName = res.data[0].nome;
    });
  });
}]);

Hi @MarceloCP,

I think it is because your ajax call is async. You should make it synchronous.

Try setting async to false to make it synchronous.
$.ajax(... async: false ...);

Thank you very much hassang,

His tip was spectacular, I was in this function for days. Helped me a lot.