It's all about the answers!

Ask a question

work item customization: getting current user as logged in user


Anna Phan (112) | asked Jun 07 '16, 3:35 a.m.
converted to question Jun 07 '16, 3:44 a.m. by Donald Nong (14.5k614)
 Hello, 
Such a long time but my problem is still the same.
I am trying this code below to catch the current user id. This script only works when i change the dependent attributes. But when a work item is saved, the script is recalculated and it doesn't work, the return is null.
What could be a problem?

dojo.provide("testImport.setCurrentUser");
dojo.require("com.ibm.team.repository.web.client.session.Session");
(function() {
var WorkItemAttributes = com.ibm.team.workitem.api.common.WorkItemAttributes;

    dojo.declare("testImport.setCurrentUser", null, {
        getValue: function(attribute, workItem, configuration) {
  var creator = workItem.getValue(WorkItemAttributes.CREATOR);
var loggedInUser = com.ibm.team.repository.web.client.session.getAuthenticatedContributor().itemId; 
  console.log("Set current user\ncreator:"+creator+ "\nloggedInUser:"+loggedInUser);
if(creator==loggedInUser){
return loggedInUser + "____This is login user";
}
return "False"; 
        }
    });
})();

Comments
Donald Nong commented Jun 07 '16, 3:46 a.m.

I converted your comment to a new question, as few people would notice it in such an old post.

2 answers



permanent link
Donald Nong (14.5k614) | answered Jun 07 '16, 3:47 a.m.
Note that the script only works in a browser. If you are using a calculated value provider, it may be executed on the server side, which will not work.

Comments
Anna Phan commented Jun 07 '16, 4:04 a.m. | edited Jun 07 '16, 4:05 a.m.

Thank Donald.

I tested this script in a browser. This script only works when the dependent attributes changed. The console.log() function shows that the script is not called when a new a work item is created or when a work item is saved 


Ralph Schoon commented Jun 07 '16, 6:32 a.m. | edited Jun 07 '16, 6:34 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Yes, a calculated value provider only works under certain conditions. The most important is that it must detect a change. It is calculated from some attribute change.
For a default value (creation of the work item) create a default value. You can also try to depend your calculated value on the modification date, maybe that works better for you.

so basically your question is completely off. The script works, but not as you would expect. From that perspective, I would suggest to reword your question.


Ralph Schoon commented Jun 07 '16, 6:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Hmm, from https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values


  • When a work item is created, the script is executed
  • When a work item is saved, the script is executed to recalculate the value.
  • When an attribute which the current attribute depends on is changed, the value of the current attribute is recalculated.

Depending on the presentations of the two attributes this may not work in all cases.


Anna Phan commented Jun 07 '16, 9:17 p.m.

 Hi Ralph,

Information from your link https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Calculated_values, I understand that the script must be executed in all three cases, not one of them.

If you configure a Script-based calculated value, the script will be executed in three cases:



Ralph Schoon commented Jun 08 '16, 2:11 a.m. | edited Jun 08 '16, 2:12 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

And the scripts are executed in all these cases, except if you use the unsupported API you are using. So it is the usage of the unsupported API that breaks the scripts. Maybe surrounding the unsupported call in a try/catch block could help. I don't have the time to debug that. I found to my satisfaction that a correct script without using unsupported API gets called in the above scenarios. For example this script does:

dojo.provide("com.example.ValueProvider");
dojo.require("com.ibm.team.repository.web.client.session.Session");

(function() {
    dojo.declare("com.example.ValueProvider", null, {

        getValue: function(attribute, workItem, configuration) {
            var userId="Test";
             var out = "TestScriptRAS VP: " +(new Date()).getTime() + " " + userId;
console.log( out );
return out ;

        }
    });
})();


permanent link
Ralph Schoon (63.6k33646) | answered Jun 07 '16, 9:21 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
It is the (unsuported) session related code below that prevents the script to run on work item creation:

var Session = com.ibm.team.repository.web.client.session.Session;
var getAuthenticatedContributor = com.ibm.team.repository.web.client.session.getAuthenticatedContributor;
var userId = getAuthenticatedContributor().userId;

This code works:

dojo.provide("com.example.ValueProvider");
dojo.require("com.ibm.team.repository.web.client.session.Session");

(function() {
    dojo.declare("com.example.ValueProvider", null, {

        getValue: function(attribute, workItem, configuration) {
            var userId="Test";
             var out = "TestScriptRAS VP: " +(new Date()).getTime() + " " + userId;
console.log( out );
return out ;

        }
    });
})();

In addition the script above is Syntactical incorrect

The lines don't make sense. False is not a valid output you have to return a valid user ID:

		if(creator==loggedInUser){
			return loggedInUser + "____This is login user";
}
return "False"; 


Comments
Anna Phan commented Jun 07 '16, 9:25 p.m. | edited Jun 07 '16, 9:27 p.m.

Hi Ralph, I removed each part in my code to find out which one make my code fail, and i do know that this line below is the reason:

getAuthenticatedContributor().userId
But i do need to catch the Current UserID, so is there any other way?

PS: Don't care about my return value, it's just a test, i return a string to see the returned value on the web UI 



Donald Nong commented Jun 07 '16, 9:47 p.m.

If you look into the code more carefully, you will notice that the function getAuthenticatedContributor() is provided within com.ibm.team.repository.web.client.session. This is the whole reason why this method only works in a browser. As far as I know, this is the only way to get the logged-in user using JavaScript, with its obvious limitation.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.