It's all about the answers!

Ask a question

Scripting - compare value of enumeration to text string


Terry Kemp (351012) | asked Jun 18 '12, 4:35 p.m.
edited Jun 18 '12, 5:31 p.m.
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


Accepted answer


permanent link
Gabriel Enriquez (3463) | answered Jun 18 '12, 6:41 p.m.
JAZZ DEVELOPER
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
Terry Kemp selected this answer as the correct answer

4 other answers



permanent link
Terry Kemp (351012) | answered Jun 19 '12, 12:20 p.m.
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.

Comments
Gabriel Enriquez commented Jun 19 '12, 1:49 p.m.
JAZZ DEVELOPER

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".


Terry Kemp commented Jun 19 '12, 2:59 p.m.

Thanks for checking back. I am on RTC 3.0.1.1. Getting no return from getLabel


Gabriel Enriquez commented Jun 19 '12, 5:40 p.m.
JAZZ DEVELOPER

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.


permanent link
Kevin Ramer (4.5k8183200) | answered Jun 19 '12, 12:22 p.m.
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;
          }     
     }



Comments
Terry Kemp commented Jun 19 '12, 3:05 p.m.

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?


permanent link
Kevin Ramer (4.5k8183200) | answered Jun 19 '12, 3:24 p.m.
Well, what I posted is Java that uses the RTC Plain Java client libraries.   So my answer may not apply, unless you could work this in as a "bean" somehow.



permanent link
Terry Kemp (351012) | answered Jun 20 '12, 4:59 p.m.
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!

Your answer


Register or 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.