Updating Map values returns list exception (row with duplicate Id at index: X)
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have a list of tasks that need to be updated. To avoid duplicates, it first gets converted into a Map<Id, Task>
whose values
are updated as follows:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>(taskList);
update mapIdTasksToUpdate.values();
The problem is: initializing the map as new Map<Id, Task>(taskList)
throws a list exception: 'System.ListException: Row with duplicate Id at index: X'. The number X varies from one execution to another. However, when trying to initialize an empty map and then put row by row manually as:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>();
for(Task taskToUpdate : taskList)
mapIdTasksToUpdate.put(taskToUpdate.Id, taskToUpdate);
update mapIdTasksToUpdate.values();
it executes without any exceptions.
What is the difference between the two approaches that makes the system throws the list exception? Why is it complaining about duplicates when maps can't hold any (assuming Id
as keys)? Any ideas?
P.S.: I think its worth to mention that the exception is thrown from an Apex Test class. I couldn't reproduce the error otherwise. Additionally, I already made sure (using System.debug
) that there is not a duplicate Id
being inserted on the map.
I appreciate any help you may give me.
Charles
EDIT: I'm adding how the taskList is created and populated:
public void updateClosedTasks(List<Task> closedTasks)
The method TaskBO.getInstance().updateTasks()
does the updating from the map.
apex map duplicate-value
New contributor
 |Â
show 1 more comment
up vote
2
down vote
favorite
I have a list of tasks that need to be updated. To avoid duplicates, it first gets converted into a Map<Id, Task>
whose values
are updated as follows:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>(taskList);
update mapIdTasksToUpdate.values();
The problem is: initializing the map as new Map<Id, Task>(taskList)
throws a list exception: 'System.ListException: Row with duplicate Id at index: X'. The number X varies from one execution to another. However, when trying to initialize an empty map and then put row by row manually as:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>();
for(Task taskToUpdate : taskList)
mapIdTasksToUpdate.put(taskToUpdate.Id, taskToUpdate);
update mapIdTasksToUpdate.values();
it executes without any exceptions.
What is the difference between the two approaches that makes the system throws the list exception? Why is it complaining about duplicates when maps can't hold any (assuming Id
as keys)? Any ideas?
P.S.: I think its worth to mention that the exception is thrown from an Apex Test class. I couldn't reproduce the error otherwise. Additionally, I already made sure (using System.debug
) that there is not a duplicate Id
being inserted on the map.
I appreciate any help you may give me.
Charles
EDIT: I'm adding how the taskList is created and populated:
public void updateClosedTasks(List<Task> closedTasks)
The method TaskBO.getInstance().updateTasks()
does the updating from the map.
apex map duplicate-value
New contributor
Can you show howtaskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.
â David Reed
1 hour ago
1
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
That code initializingtaskList
shouldn't work at all - it's iterating over an empty, newly-created List.
â David Reed
1 hour ago
@DavidReed I'm sorry. There was a typo in thefor
header. I fix'd it.
â Charles Aguiar
1 hour ago
 |Â
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a list of tasks that need to be updated. To avoid duplicates, it first gets converted into a Map<Id, Task>
whose values
are updated as follows:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>(taskList);
update mapIdTasksToUpdate.values();
The problem is: initializing the map as new Map<Id, Task>(taskList)
throws a list exception: 'System.ListException: Row with duplicate Id at index: X'. The number X varies from one execution to another. However, when trying to initialize an empty map and then put row by row manually as:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>();
for(Task taskToUpdate : taskList)
mapIdTasksToUpdate.put(taskToUpdate.Id, taskToUpdate);
update mapIdTasksToUpdate.values();
it executes without any exceptions.
What is the difference between the two approaches that makes the system throws the list exception? Why is it complaining about duplicates when maps can't hold any (assuming Id
as keys)? Any ideas?
P.S.: I think its worth to mention that the exception is thrown from an Apex Test class. I couldn't reproduce the error otherwise. Additionally, I already made sure (using System.debug
) that there is not a duplicate Id
being inserted on the map.
I appreciate any help you may give me.
Charles
EDIT: I'm adding how the taskList is created and populated:
public void updateClosedTasks(List<Task> closedTasks)
The method TaskBO.getInstance().updateTasks()
does the updating from the map.
apex map duplicate-value
New contributor
I have a list of tasks that need to be updated. To avoid duplicates, it first gets converted into a Map<Id, Task>
whose values
are updated as follows:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>(taskList);
update mapIdTasksToUpdate.values();
The problem is: initializing the map as new Map<Id, Task>(taskList)
throws a list exception: 'System.ListException: Row with duplicate Id at index: X'. The number X varies from one execution to another. However, when trying to initialize an empty map and then put row by row manually as:
Map<Id, Task> mapIdTasksToUpdate = new Map<Id, Task>();
for(Task taskToUpdate : taskList)
mapIdTasksToUpdate.put(taskToUpdate.Id, taskToUpdate);
update mapIdTasksToUpdate.values();
it executes without any exceptions.
What is the difference between the two approaches that makes the system throws the list exception? Why is it complaining about duplicates when maps can't hold any (assuming Id
as keys)? Any ideas?
P.S.: I think its worth to mention that the exception is thrown from an Apex Test class. I couldn't reproduce the error otherwise. Additionally, I already made sure (using System.debug
) that there is not a duplicate Id
being inserted on the map.
I appreciate any help you may give me.
Charles
EDIT: I'm adding how the taskList is created and populated:
public void updateClosedTasks(List<Task> closedTasks)
The method TaskBO.getInstance().updateTasks()
does the updating from the map.
apex map duplicate-value
apex map duplicate-value
New contributor
New contributor
edited 1 hour ago
New contributor
asked 1 hour ago
Charles Aguiar
113
113
New contributor
New contributor
Can you show howtaskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.
â David Reed
1 hour ago
1
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
That code initializingtaskList
shouldn't work at all - it's iterating over an empty, newly-created List.
â David Reed
1 hour ago
@DavidReed I'm sorry. There was a typo in thefor
header. I fix'd it.
â Charles Aguiar
1 hour ago
 |Â
show 1 more comment
Can you show howtaskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.
â David Reed
1 hour ago
1
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
That code initializingtaskList
shouldn't work at all - it's iterating over an empty, newly-created List.
â David Reed
1 hour ago
@DavidReed I'm sorry. There was a typo in thefor
header. I fix'd it.
â Charles Aguiar
1 hour ago
Can you show how
taskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.â David Reed
1 hour ago
Can you show how
taskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.â David Reed
1 hour ago
1
1
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
That code initializing
taskList
shouldn't work at all - it's iterating over an empty, newly-created List.â David Reed
1 hour ago
That code initializing
taskList
shouldn't work at all - it's iterating over an empty, newly-created List.â David Reed
1 hour ago
@DavidReed I'm sorry. There was a typo in the
for
header. I fix'd it.â Charles Aguiar
1 hour ago
@DavidReed I'm sorry. There was a typo in the
for
header. I fix'd it.â Charles Aguiar
1 hour ago
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
3
down vote
While your code initializing taskList
may reveal the underlying (ultimate) problem, we can illustrate the proximate issue with some Anonymous Apex. It has to do not with the update
DML or the Map itself, but with the Apex idiom that converts from an sObject List to a Map<Id, sObject>
.
This snippet works just fine:
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000002');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
But if we create your situation synthetically by changing b
to have the same Id as a
,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
we get back the expected exception:
System.ListException: Row with duplicate Id at index: 1
Apex doesn't know which of the two Accounts should be stored as the value for their shared Id in the resulting Map. There's no inherent precendence between the two. When, however, you manually iterate over the list and add each item to the map in order,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>();
for (Account i : new List<Account> a, b )
acctMap.put(i.Id, i);
everything is fine, because you're overwriting the previous value for that duplicated Id when you successively call acctMap.put()
. In essence, you're establishing that the ordering of the array disambiguates between the duplicates, with later entries winning.
Additionally, theput()
method returns the old value, if you replaced an existing entry that was already in the map.
â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list innew Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution:(Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
add a comment |Â
up vote
0
down vote
What Put method does ?
If the map previously contained a mapping for this key, the old value is returned by put
method and then replaced.
What Map(recordList) does ?
It creates a new instance of the Map class and populates it with the passed-in list of sObject records. It throws above duplicate record error.
If you want to do that in one go instead of iterating whole records and calling put method for each record. You can use putAll(sobjectArray)
method of map class. Which adds the list of sObject records to a map declared as Map<ID, sObject>
or Map<String, sObject>
.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
While your code initializing taskList
may reveal the underlying (ultimate) problem, we can illustrate the proximate issue with some Anonymous Apex. It has to do not with the update
DML or the Map itself, but with the Apex idiom that converts from an sObject List to a Map<Id, sObject>
.
This snippet works just fine:
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000002');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
But if we create your situation synthetically by changing b
to have the same Id as a
,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
we get back the expected exception:
System.ListException: Row with duplicate Id at index: 1
Apex doesn't know which of the two Accounts should be stored as the value for their shared Id in the resulting Map. There's no inherent precendence between the two. When, however, you manually iterate over the list and add each item to the map in order,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>();
for (Account i : new List<Account> a, b )
acctMap.put(i.Id, i);
everything is fine, because you're overwriting the previous value for that duplicated Id when you successively call acctMap.put()
. In essence, you're establishing that the ordering of the array disambiguates between the duplicates, with later entries winning.
Additionally, theput()
method returns the old value, if you replaced an existing entry that was already in the map.
â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list innew Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution:(Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
add a comment |Â
up vote
3
down vote
While your code initializing taskList
may reveal the underlying (ultimate) problem, we can illustrate the proximate issue with some Anonymous Apex. It has to do not with the update
DML or the Map itself, but with the Apex idiom that converts from an sObject List to a Map<Id, sObject>
.
This snippet works just fine:
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000002');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
But if we create your situation synthetically by changing b
to have the same Id as a
,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
we get back the expected exception:
System.ListException: Row with duplicate Id at index: 1
Apex doesn't know which of the two Accounts should be stored as the value for their shared Id in the resulting Map. There's no inherent precendence between the two. When, however, you manually iterate over the list and add each item to the map in order,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>();
for (Account i : new List<Account> a, b )
acctMap.put(i.Id, i);
everything is fine, because you're overwriting the previous value for that duplicated Id when you successively call acctMap.put()
. In essence, you're establishing that the ordering of the array disambiguates between the duplicates, with later entries winning.
Additionally, theput()
method returns the old value, if you replaced an existing entry that was already in the map.
â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list innew Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution:(Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
While your code initializing taskList
may reveal the underlying (ultimate) problem, we can illustrate the proximate issue with some Anonymous Apex. It has to do not with the update
DML or the Map itself, but with the Apex idiom that converts from an sObject List to a Map<Id, sObject>
.
This snippet works just fine:
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000002');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
But if we create your situation synthetically by changing b
to have the same Id as a
,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
we get back the expected exception:
System.ListException: Row with duplicate Id at index: 1
Apex doesn't know which of the two Accounts should be stored as the value for their shared Id in the resulting Map. There's no inherent precendence between the two. When, however, you manually iterate over the list and add each item to the map in order,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>();
for (Account i : new List<Account> a, b )
acctMap.put(i.Id, i);
everything is fine, because you're overwriting the previous value for that duplicated Id when you successively call acctMap.put()
. In essence, you're establishing that the ordering of the array disambiguates between the duplicates, with later entries winning.
While your code initializing taskList
may reveal the underlying (ultimate) problem, we can illustrate the proximate issue with some Anonymous Apex. It has to do not with the update
DML or the Map itself, but with the Apex idiom that converts from an sObject List to a Map<Id, sObject>
.
This snippet works just fine:
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000002');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
But if we create your situation synthetically by changing b
to have the same Id as a
,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>(
new List<Account> a, b
);
we get back the expected exception:
System.ListException: Row with duplicate Id at index: 1
Apex doesn't know which of the two Accounts should be stored as the value for their shared Id in the resulting Map. There's no inherent precendence between the two. When, however, you manually iterate over the list and add each item to the map in order,
Account a, b;
a = new Account(Id = '001000000000001');
b = new Account(Id = '001000000000001');
Map<Id, Account> acctMap = new Map<Id, Account>();
for (Account i : new List<Account> a, b )
acctMap.put(i.Id, i);
everything is fine, because you're overwriting the previous value for that duplicated Id when you successively call acctMap.put()
. In essence, you're establishing that the ordering of the array disambiguates between the duplicates, with later entries winning.
answered 1 hour ago
David Reed
21.1k31640
21.1k31640
Additionally, theput()
method returns the old value, if you replaced an existing entry that was already in the map.
â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list innew Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution:(Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
add a comment |Â
Additionally, theput()
method returns the old value, if you replaced an existing entry that was already in the map.
â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list innew Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution:(Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
Additionally, the
put()
method returns the old value, if you replaced an existing entry that was already in the map.â Mark Pond
1 hour ago
Additionally, the
put()
method returns the old value, if you replaced an existing entry that was already in the map.â Mark Pond
1 hour ago
David, I completely get your point here and agree with it. The problem is that the list in
new Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution: (Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
David, I completely get your point here and agree with it. The problem is that the list in
new Map<Id, Task>(taskList)
has no duplicated Ids as I could see in the logs. Here is a sample from the last execution: (Task:Id=00TV000000Cw2MyMAJ, Task:Id=00TV000000Cw2MxMAJ)
â Charles Aguiar
52 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Hmm. That is distinctly weird. Any chance you could distill out a reproducible example that I/we can drop in a developer edition and experiment with?
â David Reed
49 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
Yes, I will try to do that and add to the original question later. Thank you anyways. Just by reading your answer I learned a lot about maps.
â Charles Aguiar
24 mins ago
add a comment |Â
up vote
0
down vote
What Put method does ?
If the map previously contained a mapping for this key, the old value is returned by put
method and then replaced.
What Map(recordList) does ?
It creates a new instance of the Map class and populates it with the passed-in list of sObject records. It throws above duplicate record error.
If you want to do that in one go instead of iterating whole records and calling put method for each record. You can use putAll(sobjectArray)
method of map class. Which adds the list of sObject records to a map declared as Map<ID, sObject>
or Map<String, sObject>
.
add a comment |Â
up vote
0
down vote
What Put method does ?
If the map previously contained a mapping for this key, the old value is returned by put
method and then replaced.
What Map(recordList) does ?
It creates a new instance of the Map class and populates it with the passed-in list of sObject records. It throws above duplicate record error.
If you want to do that in one go instead of iterating whole records and calling put method for each record. You can use putAll(sobjectArray)
method of map class. Which adds the list of sObject records to a map declared as Map<ID, sObject>
or Map<String, sObject>
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
What Put method does ?
If the map previously contained a mapping for this key, the old value is returned by put
method and then replaced.
What Map(recordList) does ?
It creates a new instance of the Map class and populates it with the passed-in list of sObject records. It throws above duplicate record error.
If you want to do that in one go instead of iterating whole records and calling put method for each record. You can use putAll(sobjectArray)
method of map class. Which adds the list of sObject records to a map declared as Map<ID, sObject>
or Map<String, sObject>
.
What Put method does ?
If the map previously contained a mapping for this key, the old value is returned by put
method and then replaced.
What Map(recordList) does ?
It creates a new instance of the Map class and populates it with the passed-in list of sObject records. It throws above duplicate record error.
If you want to do that in one go instead of iterating whole records and calling put method for each record. You can use putAll(sobjectArray)
method of map class. Which adds the list of sObject records to a map declared as Map<ID, sObject>
or Map<String, sObject>
.
answered 1 hour ago
Pragati Jain
41538
41538
add a comment |Â
add a comment |Â
Charles Aguiar is a new contributor. Be nice, and check out our Code of Conduct.
Charles Aguiar is a new contributor. Be nice, and check out our Code of Conduct.
Charles Aguiar is a new contributor. Be nice, and check out our Code of Conduct.
Charles Aguiar is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235182%2fupdating-map-values-returns-list-exception-row-with-duplicate-id-at-index-x%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can you show how
taskList
is being created? The duplicate Id is there, not in the Map, which as you say cannot contain duplicates.â David Reed
1 hour ago
1
Sure. I will add it to the original question.
â Charles Aguiar
1 hour ago
In the version where you're looping through the taskList and inserting into the map one at a time, if there is a task record with the same Id -- go with me here -- then the value for that key would be overwritten. No error expected there. I'm not sure how that would be handled by the first version -- instantiating the map and initializing it with the values of the list at the same time. I was thinking you must have the same task in the list twice, but you said you've checked for that. Hmm.
â Shane Steinfeld
1 hour ago
That code initializing
taskList
shouldn't work at all - it's iterating over an empty, newly-created List.â David Reed
1 hour ago
@DavidReed I'm sorry. There was a typo in the
for
header. I fix'd it.â Charles Aguiar
1 hour ago