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:
Thanks a lot
/Morten.
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
ws.saveWorkItem2(wi_old, ws.resolveWorkItemReferences(wi_old, monitor), null);
also, watch out, your 'save' SHOULD cause your plugin to get called again. you should use saveWorkitem3 with the ability to pass a parameter
Comments
1 vote
1 vote