It's all about the answers!

Ask a question

appending Comments during an initial programmatic create


1
1
Susan Hanson (1.6k2201194) | asked Jun 27 '12, 1:01 a.m.
I am programmatically creating work items and I am having issues doing comments.  The code does this:

//get a "working copy" of an empty work item
final IWorkItemType workItemType = workItemClient.findWorkItemType (projArea, "defect", null);
final IWorkItemHandle handle = workItemClient.getWorkItemWorkingCopyManager().connectNew (workItemType, null);
final WorkItemWorkingCopy workingCopy = workItemClient.getWorkItemWorkingCopyManager ().getWorkingCopy (handle);
final IWorkItem workItem = workingCopy.getWorkItem ();

//fill in all the values
.
//Description
XMLString description = XMLString.createFromPlainText(descriptionText);
workItem.setHTMLDescription (description);           

.
//add the comments
IComments srcComments = workItem.getComments();
XMLString content = XMLString.createFromPlainText(contentString);
IComment ic = srcComments.createComment(creator , content);
srcComments.append(ic);
//workItem.setValue(comment,srcComments);

//save to create it
final IDetailedStatus status = workingCopy.save (null);

Now, I was trying the setValue() at the end but that gave a classCastException, so I removed it.  My code runs and the description shows up in the new work item, but no comments.  The description uses setHTMLDescription() but I didn't find a "setComments() or setHTMLComments().

Has anyone done this?  Can you send a snippet or tell me what I am doing wrong??

Thanks in advance,
susan

Accepted answer


permanent link
SEC Servizi (97123860) | answered Jun 27 '12, 9:06 a.m.
Hi Susan,
the code below works for us:
ITeamRepository repo = ...
WorkItemWorkingCopy workingCopy = ...
IWorkItem wiFreshCopy = workingCopy.getWorkItem();
IComments comments = wiFreshCopy.getComments();
String msg = ...
IComment comment = comments.createComment(repo.loggedInContributor(), XMLString.createFromPlainText(msg)); 
comments.append(comment);
workingCopy.save(null);

Cheers.
Ralph Schoon selected this answer as the correct answer

Comments
Susan Hanson commented Jun 27 '12, 9:58 a.m.

Okay, so very interesting .. .it seems like the content of my comment is causing the issue but I am not getting an error (it just ignores). For example, if my "msg" variable is: String msg = "my comment is here"; then it works. if I change it to: String msg = "my comment line 1" + "\n" + "my comment line 2";

then my comment is ignored AND I don't get an error.

Any ideas? I want to be able to have multiple lines, and I can do that using the eclipse client, so I assumed I could do this programmatically. Using the "\n" works programmatically in the Description.


SEC Servizi commented Jun 27 '12, 11:23 a.m. | edited Jun 27 '12, 11:31 a.m.

Weird... We use multi-line comments, too. Notice that we use: XMLString.createFromPlainText(msg);

One other answer



permanent link
Muthukumar C (32712833) | answered Jun 27 '12, 9:01 a.m.
Hi,

try this method.

WorkItem.getComments.append(IComment);

This IComment parameter you can create by WorkItem.getComment.CreateComment(); method.

Regards
Muthukumar.C

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.