How to make invalid the child work items of a parent using server api?
![](http://jazz.net/_images/myphoto/13218ea1eb60334c4a68d2fc649306f5.jpg)
I had created a parent workitem and created children based on the custom attribute(ChildCount). Now if I change the childcount the remaining which are last created need to move to invalid state. I am able to Invalidate child also but it is invalidating randomly. I below is code suggest any modifications.
Collections.reverse(references);
for (IReference reference : references) {
IAuditableHandle childHandle = (IWorkItemHandle) ((IItemReference) reference)
.getReferencedItem();
IWorkItem child = (IWorkItem) workItemServer.getAuditableCommon()
.resolveAuditable(childHandle, IWorkItem.FULL_PROFILE, monitor)
.getWorkingCopy();
child.getWorkItemType();
IWorkflowInfo workFlowInfo = workItemServer.findWorkflowInfo(child, monitor);
Identifier<IState>[] availableActions = workFlowInfo.getAllStateIds();
for (Identifier<IState> stateIds : availableActions) {
if (workFlowInfo.getStateName(stateIds).equalsIgnoreCase(INVALID_STATE)) {
child.setState2(stateIds);
Set additionalParams = new HashSet();
additionalParams.add("Saved111");
child = (IWorkItem) child.getWorkingCopy();
IStatus saveStatus = workItemServer.saveWorkItem3(child, null, null,
additionalParams);
}
}
childCount1--;
if (childCount1 == 0) {
break;
}
}
for (IReference reference : references) {
IAuditableHandle childHandle = (IWorkItemHandle) ((IItemReference) reference)
.getReferencedItem();
IWorkItem child = (IWorkItem) workItemServer.getAuditableCommon()
.resolveAuditable(childHandle, IWorkItem.FULL_PROFILE, monitor)
.getWorkingCopy();
child.getWorkItemType();
IWorkflowInfo workFlowInfo = workItemServer.findWorkflowInfo(child, monitor);
Identifier<IState>[] availableActions = workFlowInfo.getAllStateIds();
for (Identifier<IState> stateIds : availableActions) {
if (workFlowInfo.getStateName(stateIds).equalsIgnoreCase(INVALID_STATE)) {
child.setState2(stateIds);
Set additionalParams = new HashSet();
additionalParams.add("Saved111");
child = (IWorkItem) child.getWorkingCopy();
IStatus saveStatus = workItemServer.saveWorkItem3(child, null, null,
additionalParams);
}
}
childCount1--;
if (childCount1 == 0) {
break;
}
}
One answer
![](http://jazz.net/_images/myphoto/13218ea1eb60334c4a68d2fc649306f5.jpg)
You just need to order the children work items based on their ID (or creation date), did I get it right?
Your code seems good, you can get the ID this way:
String childID = child.getId();
I suggest that you write two "for" cycles, the first to retrieve the IDs and order the children, the second to actually invalidate the last n children you've previously ordered.