ahau
ahau

Reputation: 33

How can I work with multiple Task.Assignments in VSTO MS Project?

I got a collection of 'Users' which are working on task between two dates (firstPerformance and LastPerformance) for some hours. I would like to asign that information to a task with creating asignments and setting their Start, Finish and Work:

Assignment a =  task.Assignments.Add(ResourceID: user.Resource.Id);
                                                  
a.Start = user.Performances.FirstPerformance;
a.Finish = user.Performances.LastPerformance;
a.Work = user.Performances.TotalTime * 60 // TotalTime is in hours while Work is in minutes 

Before I do that I remove all assignments on the task and set it to pjFixedWork:

task.Type = PjTaskFixedType.pjFixedWork;
foreach (Assignment a in task.Assignments) a.Delete();

This results in strange behaivor: On the first run it sets Work and Start in Project correct and assigns the correct resource. On the second run it sets Work, Start and Finish correct And on the third run it also fixes the Unit

For example:

  1. Start: 19.10.21 Finish: 02.02.22 Work: 288,75h Resource: Andreas
  2. Start: 19.10.21 Finish: 27.01.22 Work: 288,75h Resource: Andreas [57%]
  3. Start: 19.10.21 Finish: 27.01.22 Work: 288,75h Resource: Andreas [60%]

Of course the input is on all three runs the same!

While still not understanding this behavior I experience something different if there is more than one resource to be assigned.

The data I'm assigning looks like:

User 1   First: 14.09.2021   Last: 02.12.2021    Total Time: 82,75
User 2   First: 31.08.2021   Last: 28.01.2022    Total Time: 263,5
User 3   First: 08.09.2021   Last: 09.09.2021    Total Time: 4,5
User 4   First: 02.12.2021   Last: 02.12.2021    Total Time: 1
User 5   First: 19.10.2021   Last: 28.01.2022    Total Time: 223
User 6   First: 06.12.2021   Last: 25.01.2022    Total Time: 18,5
User 7   First: 07.09.2021   Last: 14.12.2021    Total Time: 126

The results of different runs look like:

  1. Start: 31.08.21 Finish: 14.01.22 Work: 170h Resource: User1; User2; ...
  2. Start: 31.08.21 Finish: 14.01.22 Work: 169,04h Resource: User1; User2; ...
  3. Start: 31.08.21 Finish: 14.01.22 Work: 169,04h Resource: User1; User2; ... NO CHANGES

Since I have assigned 1€/hour on the resouces I can see that something is going on since the costs are:

User 1  0,21€
User 2  2,59€
User 3  0,16€
User 4  0,07€
User 5  30,79€
User 6  9,22€
User 7  126,00€

What adds up to 169,04.

What am I doing wrong here?

Best Regards

Andreas

Upvotes: 2

Views: 214

Answers (2)

Kenny Arnold
Kenny Arnold

Reputation: 378

Rachel's answer is sufficient but here's my piece for what it's worth:

  1. The resources that you assign to tasks in code should be the same period of performance as the task itself. (i.e. if the task starts on 31.08.21 and finishes on 14.01.22, all of it's resource assignments should also start and finish on those dates). When you start to mess around with that, MS Project starts to behave erroneously and you'll eventually start seeing things in the schedule that make you think "What the heck is going on? This makes no sense". Assuming your VSTO is going to be for production use, this is not something you want to subject your users to.
  2. Don't worry about the assigned resource units on a task. I'm not sure exactly how MS Project calculates the resource assignment units (maybe Rachel can shed some light on this), but I find them to often not make sense and be confusing. Personally I just ignore them. I just worry about the total number of hours each resource assignment has. (FYI be weary of the Effort Driven flag if you are working on a task that is set to fixed duration)

Two other general things about working with resource assignments on tasks:

  1. If you start to delete or add resources to a task in code, you need to set the task to 0% complete first. Results may vary otherwise. You may be doing this already but that's why I asked before.
  2. When you delete resource assignments off a task, make sure the task is set to fixed duration.
  3. You will probably want to store at least the original duration and start date of the task in memory before you start messing with the resources so you can make sure the task is still occurring at the same time after you have altered the resources assignments.

Upvotes: 1

Rachel Hettinger
Rachel Hettinger

Reputation: 8442

What am I doing wrong here?

Your code is not wrong. The 'wrong' part is trying to use Microsoft Project in a way it is not designed to work. Let me explain.

Microsoft Project uses a Critical Path Method algorithm to schedule tasks based on such things as duration of the tasks, links between them and other dependencies such as constraint dates and working hours (calendars). Given a set of these inputs, Project calculates task start and finish dates, work required, etc.

Problems arise when, instead of allowing the CPM algorithm to do the calculations, too many exact dates (for example) are forced into the schedule. Trying to force an exact amount of work for each resource also works against the CPM engine.

Having a task with more than a few resources is a good indication that it should be split up. Having tasks that are longer than a few weeks is also a good indication that they might be better split up. Break large tasks down into smaller parts.

Use Resource calendars to limit the availability of each resource as necessary. Use leveling to ensure resources are not overloaded. Let Project do what it does best--schedule tasks based on the constraints. Telling Project exactly when and how much each resource will work on each task is the opposite of how it is designed to be used. Don't fight it.

Upvotes: 1

Related Questions