The efforts are not reflected in "Time Spent" attribute even after updating efforts in "Time-tracking" tab using java code
Hello,
I have a java code to update the efforts in time tracking tab. The values in the time tracking tab is getting updated but not the time spent value.
Also, we have enabled the option to synchronize time tracking value with time spent attribute in our master template.
The time spent value is updated when we manually add the efforts in time tracking tab. It is not working via the code.
Below is the part of code:
public void updateTimeTracking(RTCConfig rtcConfig,int workItemId, String efforts, String date, IProjectAreaHandle projectAreaHandle)
throws Exception {
String resultString;
IWorkItemClient workItemClient = rtcConfig.getiWorkItemClient();// Obtain the IWorkItemClient instance
IProcessClientService processClient =
(IProcessClientService) rtcConfig.getiTeamRepository().getClientLibrary(IProcessClientService.class);
List<ITimeCode> timeCodes = rtcConfig.getiWorkItemClient().getTimeCodes(projectAreaHandle);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date datee = dateFormat.parse(date);
Timestamp timestamp = new Timestamp(datee.getTime());
Long timespent = Long.parseLong(efforts);
Duration duration = new Duration(timespent);
ITimeCode timeCode = timeCodes.get(2);
Identifier<ILiteral> workType = Identifier.create(ILiteral.class, "extended");
IWorkItemWorkingCopyManager workingCopyManager = null;
WorkItemWorkingCopy workingCopy = null;
try {
boolean hasEffortsForDate = false;
List<ITimeSheetEntry> existingEntries = readTimeSheetEntries(monitor, rtcConfig, workItemId);
for (ITimeSheetEntry entry : existingEntries) {
System.out.println(entry.getStartDate());
System.out.println(timestamp);
if (entry.getStartDate().equals(timestamp)) {
hasEffortsForDate = true;
break;
}
}
if (hasEffortsForDate) {
System.out.println("Efforts already exist for the date: " + date);
return; // Exit the method without updating
}
ITimeSheetEntry entry = workItemClient.createTimeSheetEntry(projectAreaHandle);
IWorkItem workItemHandle =
rtcConfig.getiWorkItemClient().findWorkItemById(workItemId, IWorkItem.FULL_PROFILE, monitor);
IContributorHandle contributerHandle = entry.getCreator();
IContributor owner = (IContributor) rtcConfig.getiTeamRepository().itemManager()
.fetchCompleteItem(contributerHandle, IItemManager.DEFAULT, null);
entry.setCreator(workItemHandle.getOwner());
entry.setStartDate(timestamp);
entry.setTimeSpent(duration);
entry.setTimeCode(timeCode.getTimeCodeLabel());
entry.setTimeCodeId(timeCode.getTimeCodeId());
entry.setWorkType(workType);
workingCopyManager = workItemClient.getWorkItemWorkingCopyManager();
workingCopyManager.connectCurrent(workItemHandle, IWorkItem.FULL_PROFILE, null);
workingCopy = workingCopyManager.getWorkingCopy(workItemHandle);
IWorkItemReferences workItemReferences = workingCopy.getReferences();
workItemReferences.add(WorkItemEndPoints.WORK_TIME, IReferenceFactory.INSTANCE.createReferenceToItem(entry));
workingCopy.getDependentItems().add(entry);
workingCopy.save(null);
logger.info("updated the efforts for "+workItemId);
}
catch (Exception e) {
logger.info("Error inside time tracter" + e);
}
}
One answer
I shared my experience with that API here: https://rsjazz.wordpress.com/2015/03/31/the-work-item-time-tracking-api/
This includes code and hints especially what you have to do to trigger the update..
Comments
Hi Ralph,
Thank you for your answer, I'm still not able to resolve this issue. As suggested in the link you provided, I added this lines
setTotalDuration(getTotalDuration().add(workDuration)); getWorkItem().setDuration(getTotalDuration().longValue());
but I'm getting the error of immutable property exception.
additionally these lines set estimate value and not the timespent which should automatically update when the timetracking is updated, which is currently not happening.
My code is downloadable, so if you want to test it, do so. I do not understand the last comment.