Set user name automatically in “Owned by” field while changing the status of workitem in RTC
Dears,
We are planning to set user name automatically in “Owned by” field while changing the status of specific workitem(Change Request) in RTC. So I have created the below “calculated values” script to set RTC user name automatically in “Owned by” field but it is not working for me. Could you please correct me on this script? and let us know the right script for my requirement. Thanks in advance!
FYI,
dojo.provide("DefaultOwner");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); // To access the work item attributes
dojo.require("com.ibm.team.workitem.api.common.Status"); // To create a status object to return
dojo.require("com.ibm.team.workitem.api.common.Severity"); // To create a severity for the status
(function() {
dojo.declare("DefaultOwner", null, {
getValue: function(attributeId, workItem, configuration) {
var state = workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.STATE);
// var ManagerName = workItem.setValue(Rajiv Gandhi);
// var SetName = "Rajiv Gandhi";
var ManagerName = workItem.setOwner("Rajiv Gandhi");
if((workItem.getValue(com.ibm.team.workitem.api.common.WorkItemAttributes.TYPE)=="change_request") && (state == "ChangeRequestWorkFlow.state.s45")){
return ManagerName;
}
}
});
})();
Thanks,
Rajivgandhi
Accepted answer
somehow you have to lookup the object from the users name string.
I haven't done this.. the javascript functions are limited in what they can do.
see this https://jazz.net/wiki/bin/view/Main/AttributeCustomizationExamples#Get_an_item_s_UUID_users_iterati
Comments
Also note, that your calculation has to ALWAYS return a value. It is a function, a function has to always return a value to be syntactically correct. So you have to return the current value, if you don't want to change it.
This is a popular question for attribute customization and has been answered many times already, JavaScript is very limited. Carefully look into
I think it would be better to create a follow up action. That would have a lot more API access as well and could e.g. find the manager by role, which a JavaScript calculated value can't.
Hi Ralph, Thanks for the valuable information.
4 other answers
I have followed the below steps from wiki page as mentioned Sam and Ralph. Now my requirement is working fine. Thanks Sam and Ralph for the valuable information.
Automatically set the value of an attribute based on state
Scenario: You need to automatically set the value of a work item attribute (e.g. Owner) when it is in a particular state (e.g. Verified)Solution: Use a calculated value customization
-
Note: The presented solution is experimental and should be used with caution:
- This technique may not work with all attribute types.
-
The behavior of the Eclipse UI and the Web UI differs.
- When using Eclipse, the value of an attribute is updated as soon as an item is saved.
- In the Web UI, the value of an attribute stored on the server is updated as soon as an item is saved. However the value visible on screen is not updated in that case. If you need to see the new value you should refresh the web page. Note that the data on the server contains the correct value and this is just a UI issue.
- It is recommended to combine this method with Owner must be a predefined user to assure data integrity.
-
Create a new script-based calculated value provider. In the following we set the Owned By attribute of an item to a specific user when the item is in a particular state.
dojo.provide("org.example.CalculatedOwner"); dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); (function() { var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes; var CalculatedOwner= dojo.declare("org.example.CalculatedOwner", null, { getValue: function(attributeId, workItem, configuration) { if (workItem.getValue(WorkItemAttributes.TYPE) === "defect" && workItem.getValue(WorkItemAttributes.STATE) === "4") { return configuration.getChild("parameters").getStringDefault("owner", ""); } else { return workItem.getValue(WorkItemAttributes.OWNER); } } }); })();
Note that you must always return a value that will be assigned to the Owned By field. In case the item is in the Verified state (index 4) we use a value that is configured in the project specification. Otherwise we use the current value of the Owned By field. -
This script requires a configuration entry in the process specification:
<valueProvider id="calculatedowner" name="Calculated owner" providerId="com.ibm.team.workitem.shared.common.internal.valueProviders.ScriptAttributeValueProvider"> <script class="org.example.CalculatedOwner" path="/workitem/scripts/common/CalculatedOwner.js"/> <parameters owner="_r4ct4I2vEeCCZ-jvhw8VFA" /> </valueProvider>
Theowner
attribute defines which user needs to be set as owner. To get the user id for a particular user see Get an item's UUID. - Finally assign this customization to the Owned By attribute in Work Items > Types and Attributes. Make sure to add dependencies on the Type and Status attributes since these are required for the calculation to work properly.
- This customization will allow any user to be selected as the owner of a defect as long as the defect is not in the Verified state. Once the defect is put into the Verified state the system will automatically set the owner of the item to the predefined user in the process specification.
Comments
See https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Accessing_an_item_s_Status_attri the state ID's can be found in the process configuration source and some historical information is mentioned in the documentation.
Debugging or console.log are obviously always an option as well.
Hi,
This solution is working for me. How can I set the owner for multiple workitem types through single script? for eg.
if (workItem.getValue(WorkItemAttributes.TYPE) === "defect") {
return configuration.getChild("parameters").getStringDefault("owner", "owner should be the scrum master");
} else if (workItem.getValue(WorkItemAttributes.TYPE) === "task") {
return configuration.getChild("parameters").getStringDefault("owner", "owner should be team member");
} else if (workItem.getValue(WorkItemAttributes.TYPE) === "story") {
return configuration.getChild("parameters").getStringDefault("owner", "owner should be project lead");
}
else {
return workItem.getValue(WorkItemAttributes.OWNER); }
Can some one please tell the accurate place to put <ValueProvider> tag in process config?