appending Comments during an initial programmatic create
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
//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
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.
Comments
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.
Weird... We use multi-line comments, too. Notice that we use: XMLString.createFromPlainText(msg);