I have a problem with changing task druation in java using mpxj library. I've created the simple project structure in MS Project 2016 like this:
This is how I am trying to change duration in Java:
for (int i = 0; i < allTasks.size(); i++){
if (allTasks.get(i).getID() == 3){
allTasks.get(i).setDuration(Duration.getInstance(3, TimeUnit.DAYS));
}
}
I am saving file as XML at the end but when I open it in MS Project 2016 nothing is changed. Any tips on this?
P.S I've tried to change Remaining duration and work is not even set in MS Project during creation of tasks.
This is a very interesting problem. The heart of the issue is that Microsoft Project does actually have at least one resource assignment for each task, it's just that when you have no "real" resource assignments, all the work for the task is assigned to a "nobody" resource. If you look in the XML that Microsoft Project has generated, you'll see <Assignment>
tags, with the resource shown as <ResourceUID>-65535</ResourceUID>
, which is the "null" resource (real resources will have a positive integer value here).
In order to modify the file so that the duration of the task increases to 3 days, you'll need to change the Remaining Work attribute of this assignment.
Here is a fragment of code which does that for you:
// Read the sample project
ProjectFile project = new UniversalProjectReader().read("so-duration-question.xml");
// Find the task we want to update
Task task = project.getTaskByID(Integer.valueOf(3));
// With this sample data we know we only have one resource assignment
ResourceAssignment assignment = task.getResourceAssignments().get(0);
// Set remaining work seems to be the driver for MS Project
assignment.setRemainingWork(Duration.getInstance(3, TimeUnit.DAYS));
// Write our modified file
new MSPDIWriter().write(project, "so-duration-answer.xml");
Obviously your real code will need to check what resource assignments actually exist for the task you are modifying.