0

I have problems trying to assign ressources to tasks and subtasks with the library MPXJ. Actually, when I assign the ressource to a subtask, all task durations are modified and I dont understant why. If I dont assign the ressources, the duration is well set (4 days per subtask), but when I do, is 0.

Can anybody help me to understand why? Here is the code in VB.net, I've tried with c# and same problem:

    Dim xmlwriter As New MSPDIWriter
    Dim projectfile As New ProjectFile
    Dim personcount = 1
    Dim pre As Task = Nothing

    'Filling file file with some dummy data
    For i As Integer = 1 To 10

        Dim task As Task = projectfile.addTask
        task.setName("Example Task" & i)
        Dim presub As Task = Nothing

        'Add some subtasks
        For j As Integer = 1 To 10

            Dim subtask As Task = task.addTask()
            subtask.setName("Sub Task" & j)

            'Set the subtasks duration = ' hours for every sub task
            subtask.setDuration(Duration.getInstance(4, TimeUnit.DAYS))


            'add Resources to the subtask = one resource for every task ^^
            ' 1) add resource to the general projectfile
            Dim res As Resource = projectfile.addResource()
            res.setName("person" & personcount)
            personcount += 1

            'Asociate the resource with the courent task
            Dim assgmnt As ResourceAssignment = subtask.addResourceAssignment(res)


            'Concatenate the subtasks, so that one subtask is performed after
            'another in the timeline
            'The first task has no predecessor
            If j = 1 Then
                presub = subtask
            Else
                subtask.addPredecessor(presub, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS))
                presub = subtask

            End If
            'presub.setDuration(Duration.getInstance(4, TimeUnit.DAYS))
        Next

        'Concatenate the tasks, son that one main task is performed after
        'another on the timeline
        'The first task has no predecessor,
        If i = 1 Then
            'set the start date of the project
            Dim rightnow As java.util.Calendar = java.util.Calendar.getInstance()
            rightnow.set(2014, 11, 1)
            task.setStart(rightnow.getTime())
            pre = task
        Else
            task.addPredecessor(pre, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS))
            pre = task
        End If

    Next

    'Writng the project file

    xmlwriter.write(projectfile, "C:\temp\exo.xml")

Thanks!

1 Answers1

1

If you add a resource assignment to a task, you will also need to set the amount of work for that task.

For example, if you have a 5 day task, with no resource assignments, it is sufficient to configure a start date and a five day duration for the task:

Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setDuration(Duration.getInstance(5, TimeUnit.DAYS));

This should appear in MS Project as you would expect.

If you assign a resource to that task, you'll now need to set the amount of work required to complete the task as this will drive the duration:

Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setWork(Duration.getInstance(5, TimeUnit.DAYS))

Dim res As Resource = projectfile.addResource()
res.setName("Example resource")
task.addResourceAssignment(res)

If you want to indicate that the task is complete, or partially complete, you will also need to set the actual work attribute to an appropriate value too. For example, to make the 5 day task 50% complete:

task.setActualWork(Duration.getInstance(2.5, TimeUnit.DAYS))

The more resources you add to the task, the shorter it will get, for example, if your task required 5 days of work to complete and you add two resources, then you task will be complete in 2.5 days. This is effort-driven scheduling, as described here.

You can switch off effort-driven scheduling on a task-by-task basis if you wish. In this case you task will have a fixed duration, regardless of how many resources are assigned to it:

Dim task As Task = projectfile.addTask
task.setName("Example Task")
task.setDuration(Duration.getInstance(5, TimeUnit.DAYS))
task.setEffortDriven(False)
Jon Iles
  • 2,519
  • 1
  • 20
  • 30
  • Thank ou for your answer Jon. I set de work amount But I sill have the same problem, the tasks duration are 0 (they shoul be 4 days). – Katherine Gutierrez Sep 17 '14 at 12:54
  • Apologies... I should have read the comments in my own example code! (MpxjCreate.java shipped with MPXJ). I think you'll also need to set the work, actual work, remaining work, and start and end dates on each of the individual assignments. Let me know how you get on, I'll update the answer if that works for you. – Jon Iles Sep 17 '14 at 13:06