It's all about the answers!

Ask a question

Set a new value to an attribute of a WorkItem with Java Plain API

Tiago Fernandez (5351619) | asked Aug 14 '13, 1:23 p.m.
edited Oct 15 '14, 9:33 a.m. by Ralph Schoon (63.1k33646)

Hello, I am brand new with Java Plain API and I am trying to set a new Value to an attibute of a workItem.
I am doing it in Groovy that is similar to Java.

Part of the code I have is (I already have the workItem and the iAttribute varibales):

def copyManager = workItemClient.getWorkItemWorkingCopyManager(); 
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
def workItemCopy = copyManager.getWorkingCopy(workItem);
def workItem_to_edit = (IWorkItem) workItemCopy.getWorkItem();
def inter = new Integer("4");
workItem_to_edit.setValue(iAttribute, inter)

but I get the following error:

Caught: org.eclipse.core.runtime.AssertionFailedException: assertion failed:
org.eclipse.core.runtime.AssertionFailedException: assertion failed:
        at org.eclipse.core.runtime.Assert.isTrue(
        at org.eclipse.core.runtime.Assert.isTrue(
        at$ Source)

What am I doing wrong?

Thanks for the help.

Philip Smith commented Aug 14 '13, 4:38 p.m.

 A failure with that stack trace means there is no attribute by checking hasAttribute() (yes, the error could be much better).
Where did the value of iAttribute come from? Maybe it is not applicable to that work item's type?

Tiago Fernandez commented Aug 14 '13, 4:47 p.m. | edited Aug 14 '13, 4:53 p.m.

I will show more details of my code, but as an answer since it doesn't fit as a comment....

4 answers

permanent link
Tiago Fernandez (5351619) | answered Aug 14 '13, 4:53 p.m.

More details of how my code is:

workItem = workItemClient.findWorkItemById(workItemNumber, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor())

println("Listing all attributes....")
def allAttributeHandles = workItemClient.findAttributes(workItem.getProjectArea(), new SysoutProgressMonitor());
for (iterator = allAttributeHandles.iterator(); iterator.hasNext();){

def iAttributeHandle = (IAttributeHandle)
def iAttribute = (IAttribute) repo.itemManager().fetchCompleteItem(iAttributeHandle, IItemManager.DEFAULT, new SysoutProgressMonitor())
println("Attribute Name: " + iAttribute.getDisplayName() + ", Type: " + iAttribute.getAttributeType())

if(iAttribute.getDisplayName() == "Actual Spent Hours"){
println("Setting a value to the attribute Actual Spent Hours....")
def copyManager = workItemClient.getWorkItemWorkingCopyManager();
//def workItemHandle = workItemClient.findWorkItemById(workItemNumber, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
def workItemCopy = copyManager.getWorkingCopy(workItem);
def workItem_to_edit = (IWorkItem) workItemCopy.getWorkItem();
def inter = new Integer("4");
workItem_to_edit.setValue(iAttribute, inter)


I want to set an Integer value to a custom field of a WorkItem "Actual Spent Hours", and the iAttribute is obtained by doing a for of the result of workItemClient.findAttributes.

Since I am parsing all attributes in this case, is it necessary to do an hasAttribute()?
Trying to set a value to a custom attribute, change the scenario is some way? or it is the same as the built-in attributes?
What could I be doing wrong to get that AssertionFailedException error?

As always, Thanks for the help :)

Ralph Schoon commented Oct 15 '14, 9:39 a.m.

If you don't check if the attribute is there with hasAttribute you can get an error. If all work item types you work with have the attribute, you don't have an issue. The attributes available to the type are not necessarily all there, especially on old items created before the attribute has been created. You would have to synchronize the attributes to make sure.

If you read the attribute you are trying to write. What type is the returned variable?

I am not sure in which line you get the Assertion Failed. I would suggest to debug this. See: for hints on debugging Plain Java API.

permanent link
Philip Smith (1862) | answered Aug 14 '13, 5:34 p.m.
edited Aug 14 '13, 5:36 p.m.
 Maybe you need to add something like this before calling setValue?
if (!workItem_to_edit.hasAttribute(iAttribute)) {
FYI, the type of allAttributeHandles should be List<IAttribute>, so you can cast to IAttribute and you won't need the next line.

Tiago Fernandez commented Aug 14 '13, 7:00 p.m. | edited Aug 14 '13, 7:02 p.m.

I did a println of the workItem_to_edit.hasAttribute(iAttribute) and it returned false.

The workingCopy seems it did get all attributes from the original workItem, I suspect that this line could be the key:
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor())
Maybe the FULL_PROFILE is not retrieving all attributes, could it be?
Should I try with other value of that? What profile could return all attributes of the original workItem?


PS: I am sure that the original workItem has the attribute I am trying to setValue, but the workCopy doesn't have it :(

Philip Smith commented Aug 14 '13, 7:25 p.m.

This has gone well beyond what I know. The best I can do right now is speculate.

I'd print out hasAttribute on the original item to be certain it was there. Maybe workitems don't start out with all of the attributes?

Tiago Fernandez commented Sep 14 '13, 12:12 p.m.

Hello again,
I could figure out what the problem is:
the for I did to list all the workItem attributes, is listing twice the attibute I want to set the new value, in the code example above the "Actual Spent Hours".
I fix it adding a counter just to try to set the new value the first time and not the second.

What can be happening that it lists twice some attibutes?
I am printing all attributes, that is not the only one that is listed twice.


permanent link
Surya Tripathi (65017) | answered Aug 15 '13, 8:14 p.m.
If you have a custom attribute, you probably want to try this -
List<IAttributeHandle> customAttributes = workItem.getCustomAttributes();
If this list of attributes contains your attribute, you can call
workItem.setValue(attribute, value); 
Otherwise, first add attribute and then set value
workItem.setValue(attribute, value); 

permanent link
Martin Dam Pedersen (1352814) | answered Oct 15 '14, 9:31 a.m.

There might be more than one attribute called "Actual Spent Hours". 
Have you checked to see if the attribute ID is the same on the "duplicates" by calling getIdentifier() ?

The identifier is unique within a project area, the name of the attribute is not.

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.