Set a new value to an attribute of a WorkItem with Java Plain API
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(Assert.java:110)
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2909)
at com.ibm.team.workitem.common.model.IWorkItem$setValue.call(Unknown Source)
at script.run(script.groovy:124)
What am I doing wrong?
Thanks for the help.
4 answers
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.addCustomAttribute(attribute); workItem.setValue(attribute, value);
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) iterator.next()
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 :)
Comments
if (!workItem_to_edit.hasAttribute(iAttribute)) {
workItem_to_edit.addCustomAttribute(iAttribute)
}
Comments
Philip Smith
JAZZ DEVELOPER Aug 14 '13, 4:38 p.m.Tiago Fernandez
Aug 14 '13, 4:53 p.m.