Scripting - compare value of enumeration to text string
I am writing a script to calculate the value of an attribute based on the value selected in an enumeration attribute. Specifically, the enumeration is a list of product names. I want to compare the value selected in the enumeration to a text string. I am trying to extract the value in the enumeration using the following statement, but it doesn't seem to work. Is there a different way to access the selected value in the enumeration attribute?
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
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
To retrieve the id from your custom enumeration-based attribute, try:
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
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
Thanks for the input. I did get a result from workItem.getValue(<attribute_id>); however it returned the id of the first literal in the enumeration, namely "products.literal.l4". I did not get a return from workItem.getLabel(<attribute_id>);.
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.
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
I find this useful:
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;
}
}
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
Well, turns out in 3.x, I had to use the literal IDs out of the process configuration source to do my comparisons, not directly against the name of the literals. So the getValue was the way to go in 3.x. The 4.0 option to use the new getLabel API would result in much cleaner code. Thanks for the help!