It's all about the answers!

Ask a question

How to create a new comment for a workitem programmatically


Chandan M B (11314184) | asked Jun 11 '15, 8:36 a.m.
Hi,

Please let me know how to create a new workitem using java/server side plugin programmatically.

Regards,
Chandan

Accepted answer


permanent link
sam detweiler (12.5k6195201) | answered Jun 11 '15, 11:17 a.m.
edited Jun 12 '15, 7:50 a.m.
if you don't know how to create a plugin, then you should take the extensins workshop,
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);
Chandan M B selected this answer as the correct answer

Comments
Chandan M B commented Jun 12 '15, 1:35 a.m. | edited Jun 12 '15, 2:41 a.m.

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


Ralph Schoon commented Jun 12 '15, 2:41 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

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.


Chandan M B commented Jun 12 '15, 5:17 a.m.

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);


Chandan M B commented Jun 12 '15, 5:18 a.m.


workItemServer.saveWorkItem3(
                                workItem, null, null,
                                additionalParams);

Seems not working....


Ralph Schoon commented Jun 12 '15, 5:24 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

What is not working? What happens if you debug it with Jetty.


sam detweiler commented Jun 12 '15, 5:33 a.m.

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


Chandan M B commented Jun 12 '15, 6:16 a.m.

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.


sam detweiler commented Jun 12 '15, 6:22 a.m.

have to use an answer as the text is too big



Chandan M B commented Jun 12 '15, 7:48 a.m.

Thanks it worked fine Sam.... :)


Chandan M B commented Jun 12 '15, 7:49 a.m.

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"));


Ralph Schoon commented Jun 12 '15, 7:55 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

You might want to consider to make the user that performs the operation the creator of the comment

IContributorHandle loggedIn = this.getAuthenticatedContributor();


Ralph Schoon commented Jun 12 '15, 7:58 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

If you want a hyperlink created, use the html tag and use

XMLString.createFromXMLText()

showing 5 of 12 show 7 more comments

One other answer



permanent link
sam detweiler (12.5k6195201) | answered Jun 12 '15, 6:22 a.m.
edited Jun 12 '15, 6:22 a.m.
saveworkitem (and saveworkitem2 and saveworkitem3)

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
Ralph Schoon commented Jun 12 '15, 6:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

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.


sam detweiler commented Jun 12 '15, 7:01 a.m.

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

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.