How to Set Attribute of a Work Item in an Asynchrounous Task?
I'm implementing an asynchronous task and have gotten some work items as I expected.
My question is, how to set a specific attribute with my own value for a work item?
Is it possible to do that in an asynchronous task?
I've tried to get a workingCopy before I call the setValue method.
However, it returns an 'assertion exception' and I have no idea how to deal with it.
Please help on this issue, thanks!
3 answers
IWorkItem findWorkItem = workItemClient.findWorkItemById(currentWorkItemID, IWorkItem.FULL_PROFILE, monitor);
IWorkItemHandle wiHandle = (IWorkItemHandle) findworkItem.getItemHandle();
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(handle, IWorkItem.FULL_PROFILE, null);
WorkItemWorkingCopy wc = wcm.getWorkingCopy(handle);
IWorkItem wcWorkItem = wc.getWorkItem(); /* do whatever you want */ wc.save(monitor);
Comments
Hi Hakki,
Thanks so much for your reply. You provide a code snippet using client side API. Do you have the same example for server side API?
Hello Kenery,
Yes I have, what are you trying to do? Operation Participant (server side plugin)?
I'm implementing an asynchronous task: https://jazz.net/wiki/bin/view/Main/AsynchronousTasks
On server side you need to use this, where workItemService is an instance of IWorkItemServer:
IWorkItem freshCopy = (IWorkItem)workItemService.findWorkItemById(wi.getId(), IWorkItem.FULL_PROFILE, null).getWorkingCopy(); freshCopy.setValue(automationAttribute, Boolean.valueOf(false)); workItemService.saveWorkItem2(freshCopy, null, null);
Comments
Hi Michele,
Why you r using set value function? Do you want to set a custom attribute?
Exactly, set value for a custom attribute. I also try the other method like 'setDuration' for built-in attribute, failed either.
Can you comment "attribute_delayDays" definition from your code?
Can you past here the exception you have?
Have you tried to get your work item with the code I pasted?
IWorkItem freshCopy = (IWorkItem)workItemService.findWorkItemById(wi.getId(), IWorkItem.FULL_PROFILE, null).getWorkingCopy();
The setValue method works correctly if the object is a real working copy. It seems like your object has something wrong if you get an exception...
The Exception is shown as my own post Answer.
Let me say that this is more a permission problem. You can always set that field modification permission set to everyone. Or, more likely, you can use ImpersonationService and run the save operation as a pre-configured user. I use this with the event handler (which is, basically, an asynchronous task runned by "admin" user) but I suppose you can use it too.
I've checked the attribute has been set as everyone can modify it. I think it might not be the permission problem...
Yes, you are right they are different from each other, but i think not huge.
IWorkItemCommon workItemCommonService = (IWorkItemCommon) getService(IWorkItemCommon.class);
IWorkItemServer fworkItemServer = getService(IWorkItemServer.class);
Comments
If you look at the source code (WorkItemImpl.java), on that line you'll find this:
Assert.isTrue(hasAttribute(attribute));
So, it seems that the work item that you're trying to set value to does not have that specific attribute. Did you try to synchronize it before run the task?
What do you mean about 'try to synchronize it before run the task'? I'm pretty sure the work item I got (or workingCopy) contains the 'delay_days' attribute.
I don't know, this is the reason of the error you have... basically it does not find an attribute if the work item has been created before the custom attribute has been added and the work item has not been synchronized (it is the operation that add new attribute to old work items, you can do it from eclipse client and probably also web client) or if the profile you're using is not correct. But as you're using IWorkItem.FULL_PROFILE this shouldn't be the case.