Logged in user-id for task-form-javascript

Hi,

I want to user the user-Id of the logged in user on a start form to generate a business key.

How can I get it to make this script running:

  <script cam-script type="text/form-script">
    camForm.on('submit', function() {
      camForm.businessKey = 'tweet wanted by user ' + HowDoIGetTheUserIdHere?;
    });  
  </script>

Cheers, Ingo

1 Like

Hi Ingo,

the id of the user is stored in the authentication object in the angular rootScope. You can get it by

inject(['$rootScope', function($rootScope) {
  var userId = $rootScope.authentication.name;
  //...
}]);

Does this help you?

Cheers
Sebastian

5 Likes

Where do I enter this code?

You could use it in an embedded form for example.

Hello, I tried the code as written in the google group

  var variableManager = camForm.variableManager;
  inject([ '$rootScope', function($rootScope) { 

  // current authentication to get the current logged in user
  var user = $rootScope.authentication.name;

  // a new variable will add it to the form submit
  variableManager.createVariable({
    name: 'customVariable',
    type: 'String',
    value: user,
    isDirty: true
  });
}]);

And entered it into a form in the start node.
in the task i tried to retrieve the user with following:

camForm.on('form-loaded', function() {
console.log("please log here");
  // this callback is executed *before* the variables are loaded from the server.
  // if we declare a variable here, its value will be fetched as well
  variableManager.fetchVariable('customVariable');
console.log('customVariable');
});

camForm.on('variables-fetched', function() {
  // this callback is executed *after* the variables have been fetched from the server
  var variableValue = variableManager.variableValue('customVariable');
  console.log(variableValue);
});

neither of those work. I start my process with the same user that is in the task…
the logs dont return anything. how can i get it to work?

Hi,

which logs are you looking at? The server logs or the browser logs? The embedded forms are evaluated by the browser and the console.log statements will not be visible in the server log but rather in the browser console. You can access it by pressing F12. You will also see potential error messages there.

To be extra sure you can also put a logging statement outside of the camform.on callbacks.

Cheers
Sebastian

i tried the browser logs still empty :frowning: , furthermore, if i wanted the full name of the user, how would i manage that?

Okay, that’s weird. But you do see the html of the form in the tasklist, right? Could you make the complete html file of the form available somewhere - maybe on Github? I think it would be easier to see where things go wrong.

You can get the full name of the user via the User Profile endpoint of the REST API

Hello.
This returns admin’s account for me for some reason, not the current assignee’s account.

Why can that be?

This is what I do:
inject([ ‘$rootScope’, function($rootScope) {
camForm.variableManager.createVariable({
name: ‘currentUserID’,
type: ‘String’,
value: $rootScope.authentication.name,
isDirty: true
});
}]);

Even if I log in as “kermit”, i still get “demo” in the variable.

Hi @e.belousov

Do you mean the value of the process variable?
Have you tried to print out the value of $rootScope.authentication.name?

Indeed, the variable of $rootScope.authentication.name is fine.
The problem may be somewhere in my code.
Sorry for that :slight_smile:

1 Like

Hello to everybody, I have the same issue in this post How to pass variable value in a form. Could I use {{userName}} in the embedded form, to have my form pre-populated ? Sorry for my english

Hi everybody,

I summed up the solution from here: How to pass variable value in a form

<form name="startProcess">
    <script cam-script type="text/form-script">
	inject([ '$rootScope', '$scope', function($rootScope, $scope) {
        camForm.on('variables-fetched', function() {
		  $scope.userName = $rootScope.authentication.name;
		});
	}]);
</script>
    <div class="form-group">
        <label for="client">User name</label>
        <input cam-variable-name="client"
               cam-variable-type="String"
               type="text"
               name="client"
               required
               class="form-control"
               ng-model="userName"
               readonly="true"
        />
    </div>
</form>