How to create a new comment for a workitem programmatically
Accepted answer
see https://jazz.net/library/article/1000
if you do, then the logic is pretty simple
(see the javadoc, downloadable from the product download section)
get the workitem,
get the workitem working copy
workingcopy.getComments().xxx
where xxx is either
addComment(IComment object)
or
// corrected
newComment=createComment(.....)
append(newComment);
then save the workitem
private static IWorkItemServer fWorkItemServer = null;
fWorkItemServer = getService(IWorkItemServer.class);
fWorkItemServer.saveWorkItem3(workingCopy, null, null, some_indicator_for_recursion);
Comments
Hi Sam,
I have 1 more query. I am doing this as a follow up action on click of save of a workitem.
As soon as the user press save in the workitem, it will trigger my plugin (Operation Advisor) and i need to add some default comments to a workitem.
Is it possible with the above approach?
Regards,
Chandan
Yes - and a follow up action is called a participant. An advisor or pre-condition must not modify the element it is triggered for.
Please note, your save will trigger another run of your participant. So you need to make sure this does not happen - see https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/
https://rsjazz.wordpress.com/2013/07/12/work-item-command-line-client-to-add-a-comment/ shows in more detail how to add comments. Note it is client API, you want to use Sam's hints.
Your link loops, I will delete it.
Hello,
I tried something like below
IWorkItem workItem = (IWorkItem) workItemServer.getAuditableCommon().resolveAuditable( newState,IWorkItem.FULL_PROFILE,monitor).getWorkingCopy();
workItem.getComments().createComment(creatorHandle,XMLString.createFromPlainText("www.google.com"));
Set additionalParams = new HashSet();
additionalParams.add(ICreateReviewLinkDefinitions.EXTENSION_ID);
workItemServer.saveWorkItem3(
workItem, null, null,
additionalParams);
workItemServer.saveWorkItem3(
workItem, null, null,
additionalParams);
Seems not working....
What is not working? What happens if you debug it with Jetty.
hm.. your code is nearly exact to one of my plugins that works fine.
I have this
// this will cause this plugin to be called again
IStatus saveStatus = fWorkItemServer.saveWorkItem3(workingCopy, null, null, bypass);
then check the status
if(!saveStatus.isOK())
there was a save error
After executing
workItemServer.saveWorkItem3(workItem, null, null,additionalParams);
It is going back to top of the java file ISaveParameter block. Looks like some loop happening
ISaveParameter saveParameter = null;
if (data instanceof ISaveParameter) {
saveParameter = (ISaveParameter) data;
if (saveParameter.getAdditionalSaveParameters().contains(ICreateReviewLinkDefinitions.EXTENSION_ID)) {
return;
}
Breaking in the if condition of saveParameter.getAdditionalSaveParameters.
No errors Jetty is showing.
Any comments on the Approach.
have to use an answer as the text is too big
Thanks it worked fine Sam.... :)
IContributorHandle creatorHandle = newState.getOwner();
IContributor contributor=(IContributor) itemService.fetchItem(creatorHandle, IRepositoryItemService.COMPLETE);
IComment newComment = workItem.getComments().createComment(creatorHandle, XMLString.createFromPlainText("www.google.com"));
workItem.getComments().append(newComment);
workItem.setHTMLDescription(XMLString.createFromPlainText("www.google.com"));
You might want to consider to make the user that performs the operation the creator of the comment
IContributorHandle loggedIn = this.getAuthenticatedContributor();
If you want a hyperlink created, use the html tag and use
XMLString.createFromXMLText()
One other answer
will cause ANOTHER execution of ALL appropriate plugins.
the value of the third parm (the set) can be used to tell if YOUR plugin caused the recursion
I check this in the plugin mentioned above with this code
if (data instanceof ISaveParameter)
{
saveParameter = (ISaveParameter) data;
// check to see if we are being called again from our save
// if not, process, otherwise skip the work
if ( (saveParameter.getAdditionalSaveParameters() == null)
||
(!saveParameter.getAdditionalSaveParameters().contains(Recursion))
)
{
// not a recursion
}
Comments
My hint was:
Please note, your save will trigger another run of your participant. So you need to make sure this does not happen - see https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/
https://rsjazz.wordpress.com/2013/07/12/work-item-command-line-client-to-add-a-comment/ shows in more detail how to add comments. Note it is client API, you want to use Sam's hints.
Read https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ and see how to use the additional save parameter to exit the 2nd save.
Ralphs blog pointed out that I gave you wrong info..
if you use createcomment then you also have to use append()
I fixed my original post
the recursion thing still needs to be addressed of course