Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

work item customization: getting current user as logged in user

 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"; 
        }
    });
})();

0 votes

Comments

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



2 answers

Permanent link
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.

0 votes

Comments

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 

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.

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.

 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:


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
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"; 

0 votes

Comments

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 


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 log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details

Question asked: Jun 07 '16, 3:35 a.m.

Question was seen: 2,131 times

Last updated: Jun 08 '16, 2:12 a.m.

Confirmation Cancel Confirm