It's all about the answers!

Ask a question

Set state of a work item programatically


Laurence Caraccio (9166) | asked Sep 22 '11, 8:25 a.m.
As the title say's I'm trying to set the state of a work item programatically so from new to in progress for example, I initially tried using
 workItem.setState2(Identifier<IState>); 

This did change the state but then the work item lost the other options so in the web ui there were no other states to choose from apart from the currently selected one also this method is marked as depreciated, the java doc says to use
 workingCopy.setWorkflowAction(String s)  

I've tried this but it is set to null before I change it so I'm not sure if this is actually what I want, I've been getting the work flow actions using
 ItemProfile<IWorkItem> profile = IWorkItem.DEFAULT_PROFILE;

IWorkItem work = workItemClient.findWorkItemById(349,profile, null);
IWorkflowInfo workFlowInfo = workItemClient.findWorkflowInfo(work, null);

Identifier<IWorkflowAction>[] states = workFlowInfo.getActionIds(work.getState2());

So I think the string I am passing in is correct, any ideas on what I'm doing wrong? Also I am saving the working copy of the work item after setting the work flow action.
Any help with this would be greatly appreciated. The full code I've been using to do it is below:
 ItemProfile<IWorkItem> profile = IWorkItem.DEFAULT_PROFILE;


IWorkItem w = workItemClient.findWorkItemById(349,profile, null);
if (w == null) {
System.out.println("Work Item not found.");
return false;
}

IWorkItemHandle handle1 = (IWorkItemHandle) w.getItemHandle();
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(handle1, IWorkItem.FULL_PROFILE, null);
WorkItemWorkingCopy wc = wcm.getWorkingCopy(handle1);

System.out.println(wc.getWorkflowAction());
wc.setWorkflowAction("com.ibm.team.workitem.common.workflow.IWorkflowAction:com.ibm.team.workitem.defectWorkflow.action.startWorking");
System.out.println(wc.getWorkflowAction());

wc.save(null);

One answer



permanent link
Laurence Caraccio (9166) | answered Sep 26 '11, 10:32 a.m.
If anyone is interested I've found out the correct way to do this courtesy of Luis Carlos Quintela:

"workItemAction" is the name of action to performed such as "start working"

 IWorkItemClient workItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);

IWorkItemWorkingCopyManager copyManager = workItemClient.getWorkItemWorkingCopyManager();

IWorkItem workItem = workItemClient.findWorkItemById(Integer.parseInt(workItemId), IWorkItem.FULL_PROFILE, monitor);
if (workItem == null)
throw new BuildException ("Work Item " + workItemId + " cannot be found");

copyManager.connect(workItem, IWorkItem.FULL_PROFILE, monitor);
WorkItemWorkingCopy wiCopy = copyManager.getWorkingCopy(workItem);

if (workItemAction != null) {
IWorkflowInfo wfInfo = workItemClient.findWorkflowInfo(workItem, monitor);
Identifier<IWorkflowAction>[] actionList = wfInfo.getActionIds(workItem.getState2());
String workItemActionIdentifier = null;

for (int i = 0; i < actionList.length; i++) {
Identifier<IWorkflowAction> action = actionList[i];
String actionName = wfInfo.getActionName(action);
if (actionName != null) {
if (wfInfo.getActionName(action).equals(workItemAction)) {
workItemActionIdentifier = action.getStringIdentifier();
break;
}
}
}
if (workItemActionIdentifier != null)
wiCopy.setWorkflowAction(workItemActionIdentifier);
}

wiCopy.save(monitor);
copyManager.disconnect(workItem);

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.