Logged in user-id for task-form-javascript


#1

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


#2

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


Get login user info in Task Form
#3

Where do I enter this code?


#4

You could use it in an embedded form for example.


#5

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?


#6

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


#7

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


#8

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


#9

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.


#10

Hi @e.belousov

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


#11

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