How to update workitem custom attributes?
One answer
Object data = operation.getOperationData(); if(data instanceof ISaveParameter) { ISaveParameter saveParameter = (ISaveParameter) data; IAuditable newState = saveParameter.getNewState(); if(newState instanceof IAuditable) { IWorkItem workItemNewState = (IWorkItem) newState;You then need to get a reference to the attribute(s) you want to update:
IWorkItemServer workItemServer = getService(IWorkItemServer.class); IAttribute customAttribute = workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttributeId", null);You then need to get a working copy of the work item you want to update:
IWorkItem workingCopy = (IWorkItem) workItemNewState.getWorkingCopy();Lastly you need to update the value of the attribute and save the working copy:
workingCopy.setValue(customAttribute, "my new value");
workItemServer.saveWorkItem2(workingCopy, null, null);
Full code looks like this:
public void run(AdvisableOperation operation, IProcessConfigurationElement participantConfig, IParticipantInfoCollector collector, IProgressMonitor monitor) throws TeamRepositoryException { Object data = operation.getOperationData(); if(data instanceof ISaveParameter) { ISaveParameter saveParameter = (ISaveParameter) data; IAuditable newState = saveParameter.getNewState(); if(newState instanceof IAuditable) { IWorkItem workItemNewState = (IWorkItem) newState; IWorkItem workingCopy = (IWorkItem) workItemNewState.getWorkingCopy(); IWorkItemServer workItemServer = getService(IWorkItemServer.class); IAttribute customAttribute = workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttribute", null); workingCopy.setValue(customAttribute, "my new value"); workItemServer.saveWorkItem2(workingCopy, null, null); } } }
Comments
Thanks for reply Jared,
I tried your code but I was getting the following exception:
Exception running followup action.
An unhandled exception occurred during "testing participant".
Stale Data.
any help???
Remove the workItemServer.saveWorkItem2(workingCopy, null, null); line from the code. As the operation advisor is running in the current context so this method will be called automatically. Saving work item twice is causing error.
If I am not wrong this code you are running inside the server then I thin working copy is also not needed.
First remove the line above if not working then do something like this
IWorkItem workItemNewState = (IWorkItem) newState;
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IAttribute customAttribute =
workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttribute", null);
workItemNewState.setValue(customAttribute, "my new value");
Hi praveen, as you said , I tried but it was not working , it wont give exception but the workitem was also not save custom attribute value .
any other help please ..........