How to change work item type programmatically when attribute value changes?
Accepted answer
I tried it out and this script seems to do it (This is an example and not fully tested and I am not liable if the script causes issues etc.). There are various considerations e.g. for which types to work and what to return and why that I leave to the reader.
This script only looks at items with a type defect and task and based on the priority it sets a type or returns the current type. Priority is looked for using the display value and not the literal ID.
/******************************************************************************* * Licensed Materials - Property of IBM * (c) Copyright IBM Corporation 2011. All Rights Reserved. * * Note to U.S. Government Users Restricted Rights: * Use, duplication or disclosure restricted by GSA ADP Schedule * Contract with IBM Corp. *******************************************************************************/ dojo.provide("com.example.ValueProvider"); dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes"); (function() { var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes; dojo.declare("com.example.ValueProvider", null, { getValue: function(attribute, workItem, configuration) { var enumerationDisplayValue = workItem.getLabel(WorkItemAttributes.PRIORITY); // var enumerationLiteral = workItem.getValue(WorkItemAttributes.PRIORITY); var currentType = workItem.getValue(attribute); if(currentType=="defect" || currentType=="task"){ if(enumerationDisplayValue=="High"){ return "defect"; } if(enumerationDisplayValue=="Medium"){ return "task"; } } return currentType; } }); })();
2 other answers
In general you set the ID of the type in the attribute.
I can think of the following approaches you can try:
1. A JavaScript based attribute customization. See https://jazz.net/library/article/1093 Lab 5 and https://jazz.net/wiki/bin/view/Main/AttributeCustomization. You would return the type ID based on the enumeration literal each time. I would assume the user has to refresh the editor if that happens.
2. A follow up action coded in Java - this is a lot more complex and has to deployed on the server. I would try the 1st approach on a test system
I would also - as always - ask myself the question, if this is a good approach. Why do we have this requirement? Is there a better way? Should a human do this change?
I can think of the following approaches you can try:
1. A JavaScript based attribute customization. See https://jazz.net/library/article/1093 Lab 5 and https://jazz.net/wiki/bin/view/Main/AttributeCustomization. You would return the type ID based on the enumeration literal each time. I would assume the user has to refresh the editor if that happens.
2. A follow up action coded in Java - this is a lot more complex and has to deployed on the server. I would try the 1st approach on a test system
I would also - as always - ask myself the question, if this is a good approach. Why do we have this requirement? Is there a better way? Should a human do this change?
Comments
aren't there potential impacts to other attributes in the target workitem type if they are not 100% symmetrical.
I haven't tried JavaScript, however, I used the API. I think it should work - as always test this on a test system, obviously.
If the other work item type has new attributes, they will be created. If the editor presentations are different, after reload attributes not in the presentations will not be shown, but they will still be there.
If there is validation or different required attributes then you would have to fix that in the UI.