Scripting - compare value of enumeration to text string
var product = workItem.getValue(WorkItemAttributes.products);
I found an old thread that touches on this same issue but it is not clear enough for me to know what to do:
https://jazz.net/forum/questions/5972/converting-enumeration-attribute-values-to-consumable-string
Accepted answer
workItem.getValue(<attribute_id>);
or for getting its label:
workItem.getLabel(<attribute_id>);
Note: replace <attribute_id> with the attribute id within quotes and without "WorkItemAttributes.".
--
Gabriel Enriquez, IBM Rational, Tracking & Planning
4 other answers
Just to clarify what I am trying to do, let me provide an example. Suppose I have an enumeration with the following literals:
Product A
Product B
Product C
When a user creates a work item and selects one of the values in the enumeration, I want to be able to extract the text for the value selected, such as "Product B", so I can compare it to another string. So what I'd like to have is something like this:
var product = workItem.getName("products");
if (product == "Product B" ) {
do some stuff;
}
So the question is, what is the proper syntax to extract the string that represents the currently selected value of the enumeration.
Comments
Which RTC/Jazz version are you using? Use getLabel instead of getValue to retrieve its UI label (just tested in RTC 4.0). Also, the script (therefore the extracted value) will be executed initially when the editor opens, and further "on save".
Thanks for checking back. I am on RTC 3.0.1.1. Getting no return from getLabel
In RTC 4.0 you have the getLabel option. For your version, please refer to https://jazz.net/wiki/bin/view/Main/AttributeCustomization#API_for_Javascript.
private String getAttributeDisplayName(IAttribute attribute,String lit_value) {
IEnumeration<ILiteral> enumeration;
try {
String my_literal;
enumeration = (IEnumeration<ILiteral>)getWorkItemClient().resolveEnumeration(attribute, null);
List<ILiteral> enumerationLiterals = enumeration.getEnumerationLiterals();
for (ILiteral literal : enumerationLiterals) {
my_literal=literal.getIdentifier2().getStringIdentifier();
if (lit_value.equals(my_literal)) {
return literal.getName();
}
}
return null;
} catch (TeamRepositoryException e) {
return null;
}
}
Comments
Thanks - that looks promising but will need to get some help incorporating it into my calculated value script as I'm not that familiar with java scripting and learning RTC as I go. One question - is "<iliteral>" a placeholder I am supposed to replace with some other string or is that the way it should appear in the function?