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

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


0 votes


Accepted answer

Permanent link
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

0 votes


4 other answers

Permanent link
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.

0 votes

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.


Permanent link
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;
          }     
     }


0 votes

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?


Permanent link
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.


0 votes


Permanent link
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!

0 votes

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

Question asked: Jun 18 '12, 4:35 p.m.

Question was seen: 4,103 times

Last updated: Jun 20 '12, 4:59 p.m.

Confirmation Cancel Confirm