Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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

0 votes



One answer

Permanent link

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..

0 votes

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.

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 457

Question asked: May 08 '24, 8:01 a.m.

Question was seen: 575 times

Last updated: May 13 '24, 2:58 a.m.

Confirmation Cancel Confirm