How to properly set the task completion

Hi,
I’m trying to set the task completion using Aspose.Tasks for Java 22.9.

The following code works (sets the completion to 20%):

int p = 20;
task.set(Tsk.PERCENT_COMPLETE,  p);

The following doesn’t (completion remains at 0%):

public int getCompletionPercentageInt() {...}
// returns some non-zero integer, e.g. 30
...
task.set(Tsk.PERCENT_COMPLETE,  getCompletionPercentageInt());

What’s wrong using getCompletionPercentageInt()?

Shall the result be converted to some Aspose.Tasks specific type? Which one?
I haven’t found any suitable for and examples are not addressing this topic (at least I have not found any).
Regards

@piomal,
as you haven’t provided a full source code, I’ve tried with v. 22.9 and the following code.

...
        Project project = new Project();
        Task task = project.getRootTask().getChildren().add("Task");
        task.set(Tsk.PERCENT_COMPLETE, getPercentage());
        System.out.println(task.get(Tsk.PERCENT_COMPLETE));
    }

    static int getPercentage() { return 42; }

and the result is ok.

The obvious thing to do is to output the result of the method to console and to check whether it return the correct value:

int percentage = getCompletionPercentageInt();
System.out.println(percentage);
task.set(Tsk.PERCENT_COMPLETE,  percentage);
System.out.println(task.get(Tsk.PERCENT_COMPLETE));

Many thanks for the effort to understand and evaluate the problem.

However, setting the PERCENT_COMPLETE for a task itself was not the problem.

It turned out that the problem was setting the completion to 0% for summary tasks (group tasks) what resulted in automatic resetting the completion for subtasks to 0%.

One may observe this behavior directly in MS Project. The Aspose.Tasks library appears to do it too.

The solution was to not set the completion for summary tasks.

Many thanks.

1 Like