It's all about the answers!

Ask a question

Change value of workitem in Follow-up action not apply!!!

silencehill wu (5022532) | asked Nov 14 '13, 4:04 a.m.

I am writting a Follow-up action.I want to modified value of two field after the change of any field value of workitem saved. But after changing state of the workitem, the follow action did run, and set setAttribute statement did passed. After follow up action finished. In UI of RTC client, The two fields were not changed at all. The type of two fields both are small string. Why no change? how can I change workitem values in follow up action.

Please do not let me try pre condition. Pre-condition is ok, I know. But I want to change value of workitem just after workitem have been saved. I need do some other work after workitem saved not before workitem saved.

The source like below:

public class StreamAutoCreateParticipant extends AbstractScmService implements IOperationParticipant {
 private static Logger logger = Logger.getLogger(StreamAutoCreateParticipant.class);
 private static IWorkItemServer workItemService;
 private static IProjectAreaHandle projectAreaHandle;
 private static IProcessService processService;

 public void run(AdvisableOperation operation,
   IProcessConfigurationElement participantConfig,
   IParticipantInfoCollector collector, IProgressMonitor progress)
   throws TeamRepositoryException {
  IRepositoryItemService repoItemService = getService(IRepositoryItemService.class);
  workItemService = getService(IWorkItemServer.class);
  Object operationData = operation.getOperationData();
  if (!(operationData instanceof ISaveParameter)) {
  IProcessArea processArea = operation.getProcessArea();
  projectAreaHandle = processArea.getProjectArea();
  IProjectArea projectArea = (IProjectArea)                  repoItemService.fetchItem(processArea.getProjectArea(),IRepositoryItemService.COMPLETE);

  IWorkItem newWorkItem = (IWorkItem) saveParameter.getNewState();
  IAttribute attr = workItemService.findAttribute(projectArea, "SupplementalProduct", null);
  workItem.setValue(attr,  "abcd,ddd,tttt");

  IAttribute attr = workItemService.findAttribute(projectArea, "MainProduct", null);
  workItem.setValue(attr,  "kkkk");"########StreamAutoCreateParticipant结束,vpproject工作项状态修改之后自动创建流###############"+new Date());

after the source running, the value of SupplementalProduct and MainProduct still not changed.

then  I have ever change coding like below, but cycling happened, loop saving.

IWorkItem wk = workItemService.findWorkItemById(newWorkItem.getId(), IWorkItem.FULL_PROFILE, null);
IWorkItem workingCopy= (IWorkItem)wk.getWorkingCopy();
IAttribute attr = workItemService.findAttribute(projectArea, "MainProduct", null);
workingCopy.setValue(attr,  "kkkk");
IStatus status = workItemService.saveWorkItem2(workingCopy, null, null);
if(! status.isOK()) {

then I try below code,but error occured,

IWorkItem newWorkItem = (IWorkItem) saveParameter.getNewState();
IWorkItem workingCopy= (IWorkItem)newWorkItem.getWorkingCopy();
IAttribute attr = workItemService.findAttribute(projectArea, "MainProduct", null);
workingCopy.setValue(attr,  "kkkk");

error info :
14:15:26,421 [30252940@qtp-21488974-59] ERROR                         - Stale Data Stale Data
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(

Please give me an advise.

Accepted answer

permanent link
Ralph Schoon (61.2k33643) | answered Nov 15 '13, 4:21 a.m.
edited Nov 15 '13, 4:22 a.m.
I have to disagree, you are not supposed to change the data of a work item in an advisor/pre condition. See: , , show examples where the  work item gets updated. You get stale data, if the data in the work item is not the most recent. Try to resolve the work item again, before you modify it.
silencehill wu selected this answer as the correct answer

silencehill wu commented Nov 16 '13, 5:08 a.m.

You mean I can get the workitem the lastest like below:

IWorkItem workingCopy = WorkItemServer
    .resolveAuditable( (IWorkItem) saveParameter.getNewState(), IWorkItem.FULL_PROFILE, monitor)


then call

workItemServer.saveWorkItem2(workingCopy, null, null);

will save the change of value?

Ralph Schoon commented Nov 18 '13, 2:39 a.m.

I mean, stale data says that the item has been modified somewhere else. So before you get the working copy get the work item again, then do the modification and save. You can see in my posts how I do it.

silencehill wu commented Nov 25 '13, 11:42 a.m.

Thks, it has worked in follow action to refer your post.

But I still have a question, why we can set attribute value directly using setAttribute api without call save api, and it can work immediately in pre-condition, why setAttribute API can not work in follow action directly?

sam detweiler commented Nov 25 '13, 11:48 a.m.

the attribute should not change in the database unless a workitem Save has been completed.

setAttribute changes the in memory copy, but not the database.

One other answer

permanent link
Aradhya K (1.4k44345) | answered Nov 14 '13, 4:19 a.m.
If you are trying to update the work item then it would be better if it is done in the advisor than in the follow up action.
The usual flow is during the save operation all the pre conditions are first executed, then the work item will be saved on to the repository, after this the follow up actions will be invoked.

silencehill wu commented Nov 14 '13, 10:58 a.m. | edited Nov 14 '13, 11:27 a.m.

Yes, I know you mean.  

But my business logic like below:
1. create/ update work item finished.
2. User can change work item state and click save. If work item saved succcessly,  A new stream  will be auto created according to the work item information .
3. If  stream created success or fail, a special attribute of  work item to indicate stream creation is ok or failed  will be updated. Actually, business logic is more complex, I just try to describe it simple to understand.

So I have to coding in  follow up action, step 2, step 3 all need to be coding in follow up action.

So how can I change workitem values in follow up action.

Aradhya K commented Nov 14 '13, 10:54 p.m.

There is not much difference between creating other artifacts as part of the advisors or participants. But if you want to update the work item in transaction then it would be better if done in the advisor.

Your answer

Register or to post your answer.