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

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

0 votes


Accepted answer

Permanent link
the Owner field contains an object reference, not a string.

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
Rajiv Gandhi selected this answer as the correct answer

1 vote

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

 Lab 5 - in the beginning a lot of the limitations are explained.
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

Permanent link

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

  1. 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.
  2. 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.
  3. 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>
    		
    The owner 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.
  4. 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.
  5. 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.

1 vote


Permanent link
how to get Workflow state ID?

 in your program u mentioned
 workItem.getValue(WorkItemAttributes.STATE) === "4"

how to know ids of custom workflow state or default workflow state?

0 votes

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.


Permanent link

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

0 votes


Permanent link

 Can some one please tell the accurate place to put <ValueProvider> tag in process config?

I tried but getting the  Unsupported value - _wgiM4O5SEem3D5FEFVFywQ in logs. Need some assistance.

0 votes

Comments
It puts itself in there, if you configure one. Use a test environment to play around with it. Then look into the process configuration.


Also please ask your own questions and do not comment questions in other users questions answers.

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
× 10,938

Question asked: Jan 18 '16, 7:55 a.m.

Question was seen: 4,059 times

Last updated: Feb 04 '20, 10:08 a.m.

Confirmation Cancel Confirm