Adding Task link. item with the same key has already been added. Key: 21

Hi Team.I am using Aspose.Tasks Python via .net for exporting to MPP.

I am trying to add task link and I am receiving this error.
link=prj.task_links.add(predecessor, successor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Proxy error(ArgumentException): An item with the same key has already been added. Key: 21

Please help me out.

@sitha

The error message “An item with the same key has already been added. Key: 21” typically indicates that you are trying to add a task link that already exists in the collection. This can happen if you attempt to create a link between the same predecessor and successor tasks multiple times without removing the existing link first.

To resolve this issue, you can check if the task link already exists before adding it. Here’s a general approach you can follow:

  1. Check Existing Links: Before adding a new link, iterate through the existing task links to see if a link between the specified predecessor and successor already exists.

  2. Add Link Only if Not Exists: If the link does not exist, proceed to add it.

Here’s a sample code snippet that demonstrates this approach:

# Assuming 'project' is your Project object
# and 'predecessor' and 'successor' are your Task objects

# Check if the link already exists
link_exists = any(link.PredTask == predecessor and link.SuccTask == successor for link in project.TaskLinks)

if not link_exists:
    # Add the task link if it does not exist
    project.TaskLinks.Add(predecessor, successor)
else:
    print("The task link already exists.")

This code checks for existing links before attempting to add a new one, which should help you avoid the error you are encountering.

If you continue to face issues, please ensure that the task IDs you are using are correct and that you are not inadvertently trying to create duplicate links.

Feel free to reach out if you need further assistance!

Yes I am doing that still I am recieving error.

I don’t have duplicate links still I am receiving the error.Can I connect with someone.

@sitha ,
I’ve executed the line you’ve attached and task link was added successfully:

prj = tsk.Project()
predecessor = prj.root_task.children.add('new task 1');
successor = prj.root_task.children.add('new task 2');
prj.task_links.add(predecessor, successor);

@vasiliy.sinitsyn
Task link is sometimes working and sometimes failing I am not understanding why its failing.Can I show u.Can we connect.There are no duplicate links as well.

@sitha ,
can you prepare code sample along with input file (if any) so we can run it and reproduce the issue on our end?

@vasiliy.sinitsyn

Here is the sample code

https://replit.com/join/ndqgekbqhu-llyodryan43

If you have any doubts we can connect on Google meet or Zoom any time which is convenient for you.

@vasiliy.sinitsyn

@sitha ,
can you attach zip file with your sample code to this topic ?

@vasiliy.sinitsyn

sample code
sample-code.zip (23.7 KB)

@vasiliy.sinitsyn
Can u please speed up the process.Its already been 3 days.I need to make a decision whether to take this license or not.My temporary license is expiring.

looks like there is a problem in internal recalculations after task link is added.
We will investigate it further.

As a workaround you can turn off recalculations (it will also speed up creation of the project)
by setting calculation_mode to MANUAL before adding task links:

prj.calculation_mode = asp_tasks.CalculationMode.MANUAL

for dep_data in dependency_data:
    # Print the predecessor and successor task data
[ ...] 

and restoring it to AUTOMATIC after adding task links:

prj.calculation_mode = asp_tasks.CalculationMode.AUTOMATIC

@sitha
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): TASKSNET-11408

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@vasiliy.sinitsyn .Thanks

The issues you have found earlier (filed as TASKSNET-11408) have been fixed in this update.
Download link: Aspose.Tasks for .NET