Adding approvals to work item in precondition (server side)
Hi, I'm trying to create a plugin which will automatically add an approval to a work item, if the plugin detects a certain status change.
Right now the code is experimental, and should add an approval to ALL status changes, but I can't get it to work. The entire operation runs fine, but no approvals are saved in the work item: Object obj = operation.getOperationData(); SaveParameter data = (SaveParameter) obj; // getting standard utilities IWorkItemServer ws = getService(IWorkItemServer.class); IAuditableCommon wiCommon = ws.getAuditableCommon(); IRepositoryItemService rs = getService(IRepositoryItemService.class); IAuditableServer as = getService(IAuditableServer.class); // getting work item info IWorkItem wi_old = (IWorkItem) data.getOldState(); IWorkflowInfo wfInfo_old = ws.getWorkflow(wi_old.getWorkItemType(), wi_old.getProjectArea(), monitor); String workItemState_old = wfInfo_old.getStateName(wi_old.getState2()); IWorkItem wi = (IWorkItem) data.getNewState(); IWorkflowInfo wfInfo = ws.getWorkflow(wi.getWorkItemType(), wi.getProjectArea(), monitor); String workItemState = wfInfo.getStateName(wi.getState2()); // if no state change occurred -> exit this advisor if (workItemState.equals(workItemState_old)) { return; } wi_old = (IWorkItem) wi_old.getWorkingCopy(); IApprovals approvals= wi_old.getApprovals(); IApprovalDescriptor descriptor= approvals.createDescriptor(WorkItemApprovals.REVIEW_TYPE.getIdentifier(), "My approval"); IApproval approval= approvals.createApproval(descriptor, wi_old.getOwner()); approvals.add(approval); ws.saveWorkItem2(wi_old, ws.resolveWorkItemReferences(wi_old, monitor), null); IAdvisorInfo info = collector.createProblemInfo("WorkItem ("+wi.getId()+") - new state ["+workItemState+"]", "bla bla bla", this.getClass().getName()); collector.addInfo(info); return;Any ideas why this won't work? Thanks a lot /Morten. |
Accepted answer
you are saving the OLD workitem contents, which then get overlayed by the NEW workitem contents.
also, watch out, your 'save' SHOULD cause your plugin to get called again. you should use saveWorkitem3 with the ability to pass a parameter Morten Madsen selected this answer as the correct answer
Comments Thanks for your answer. You are right in both cases. Yes, I save the old workitem because I will not allow the status change. But I add approvals to this old state and execute another save operation which will in turn invoke my advisor again.
1
sam detweiler
commented Aug 15 '13, 8:02 a.m.
good question.
Morten Madsen
commented Aug 15 '13, 8:30 a.m.
Thanks for trying anyway :).
1
It doesn't sound like saving a work item during a work item precondition is a good idea. The precondition runs before the work item save and you save the work item again in the precondition. This would cause the precondition to run again, which causes another work item save.
Morten Madsen
commented Aug 15 '13, 10:00 a.m.
I agree. I've changed my process now.
|
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.