Set doesn't maintain uniqueness although I am fetching Id field

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am trying to upsert a list of records and I may get duplicates so what I did In loop I am adding records to a set then that set into the list for DML.
Now problem is coming when I add a record in set then also I am getting duplicates although I have included this record id while querying.
Any idea how to fix this?
apex soql list set
add a comment |Â
up vote
1
down vote
favorite
I am trying to upsert a list of records and I may get duplicates so what I did In loop I am adding records to a set then that set into the list for DML.
Now problem is coming when I add a record in set then also I am getting duplicates although I have included this record id while querying.
Any idea how to fix this?
apex soql list set
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to upsert a list of records and I may get duplicates so what I did In loop I am adding records to a set then that set into the list for DML.
Now problem is coming when I add a record in set then also I am getting duplicates although I have included this record id while querying.
Any idea how to fix this?
apex soql list set
I am trying to upsert a list of records and I may get duplicates so what I did In loop I am adding records to a set then that set into the list for DML.
Now problem is coming when I add a record in set then also I am getting duplicates although I have included this record id while querying.
Any idea how to fix this?
apex soql list set
apex soql list set
edited 14 hours ago
Oleksandr Berehovskiy
6,90611633
6,90611633
asked 14 hours ago
shukla yogesh
7611
7611
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
here is an example, why storing in Set sobjects is a not a best idea.
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Set<Account> accts = new Set<Account>();
accts.add(acct1);
accts.add(acct2);
System.debug('accts.size():' + accts.size()); // accts.size():2 oops, size is 2
System.debug('accts:' + accts); // two account records are printed
So uniquness of records is defined NOT ONLY by Id, but rest of the fields are considered as well.
in order to correct storing records to update, you can use Map<Id, Sobject>
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
sobjectsToUpdate.put(acct1.Id, acct1);
sobjectsToUpdate.put(acct2.Id, acct2);
System.debug('sobjectsToUpdate.size():' + sobjectsToUpdate.size()); // sobjectsToUpdate.size():1 nice!
System.debug('sobjectsToUpdate:' + sobjectsToUpdate); // one account record is printed
Try to put in sets (or in keys of map) immutable types
in your current case, if you want to call upsert DML, you can have two collections: Map to store records to update with key by Id and List for records to insert. Merge them into one collection and do upsert.
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
List<Sobject> sobjectsToInsert = new List<Sobject>();
...
List<Sobject> sobjectsToUpsert = new List<Sobject>();
sobjectsToUpsert.addAll(sobjectsToUpdate);
sobjectsToUpsert.addAll(sobjectsToInsert);
upsert sobjectsToUpsert;
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
here is an example, why storing in Set sobjects is a not a best idea.
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Set<Account> accts = new Set<Account>();
accts.add(acct1);
accts.add(acct2);
System.debug('accts.size():' + accts.size()); // accts.size():2 oops, size is 2
System.debug('accts:' + accts); // two account records are printed
So uniquness of records is defined NOT ONLY by Id, but rest of the fields are considered as well.
in order to correct storing records to update, you can use Map<Id, Sobject>
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
sobjectsToUpdate.put(acct1.Id, acct1);
sobjectsToUpdate.put(acct2.Id, acct2);
System.debug('sobjectsToUpdate.size():' + sobjectsToUpdate.size()); // sobjectsToUpdate.size():1 nice!
System.debug('sobjectsToUpdate:' + sobjectsToUpdate); // one account record is printed
Try to put in sets (or in keys of map) immutable types
in your current case, if you want to call upsert DML, you can have two collections: Map to store records to update with key by Id and List for records to insert. Merge them into one collection and do upsert.
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
List<Sobject> sobjectsToInsert = new List<Sobject>();
...
List<Sobject> sobjectsToUpsert = new List<Sobject>();
sobjectsToUpsert.addAll(sobjectsToUpdate);
sobjectsToUpsert.addAll(sobjectsToInsert);
upsert sobjectsToUpsert;
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
add a comment |Â
up vote
3
down vote
here is an example, why storing in Set sobjects is a not a best idea.
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Set<Account> accts = new Set<Account>();
accts.add(acct1);
accts.add(acct2);
System.debug('accts.size():' + accts.size()); // accts.size():2 oops, size is 2
System.debug('accts:' + accts); // two account records are printed
So uniquness of records is defined NOT ONLY by Id, but rest of the fields are considered as well.
in order to correct storing records to update, you can use Map<Id, Sobject>
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
sobjectsToUpdate.put(acct1.Id, acct1);
sobjectsToUpdate.put(acct2.Id, acct2);
System.debug('sobjectsToUpdate.size():' + sobjectsToUpdate.size()); // sobjectsToUpdate.size():1 nice!
System.debug('sobjectsToUpdate:' + sobjectsToUpdate); // one account record is printed
Try to put in sets (or in keys of map) immutable types
in your current case, if you want to call upsert DML, you can have two collections: Map to store records to update with key by Id and List for records to insert. Merge them into one collection and do upsert.
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
List<Sobject> sobjectsToInsert = new List<Sobject>();
...
List<Sobject> sobjectsToUpsert = new List<Sobject>();
sobjectsToUpsert.addAll(sobjectsToUpdate);
sobjectsToUpsert.addAll(sobjectsToInsert);
upsert sobjectsToUpsert;
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
here is an example, why storing in Set sobjects is a not a best idea.
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Set<Account> accts = new Set<Account>();
accts.add(acct1);
accts.add(acct2);
System.debug('accts.size():' + accts.size()); // accts.size():2 oops, size is 2
System.debug('accts:' + accts); // two account records are printed
So uniquness of records is defined NOT ONLY by Id, but rest of the fields are considered as well.
in order to correct storing records to update, you can use Map<Id, Sobject>
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
sobjectsToUpdate.put(acct1.Id, acct1);
sobjectsToUpdate.put(acct2.Id, acct2);
System.debug('sobjectsToUpdate.size():' + sobjectsToUpdate.size()); // sobjectsToUpdate.size():1 nice!
System.debug('sobjectsToUpdate:' + sobjectsToUpdate); // one account record is printed
Try to put in sets (or in keys of map) immutable types
in your current case, if you want to call upsert DML, you can have two collections: Map to store records to update with key by Id and List for records to insert. Merge them into one collection and do upsert.
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
List<Sobject> sobjectsToInsert = new List<Sobject>();
...
List<Sobject> sobjectsToUpsert = new List<Sobject>();
sobjectsToUpsert.addAll(sobjectsToUpdate);
sobjectsToUpsert.addAll(sobjectsToInsert);
upsert sobjectsToUpsert;
here is an example, why storing in Set sobjects is a not a best idea.
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Set<Account> accts = new Set<Account>();
accts.add(acct1);
accts.add(acct2);
System.debug('accts.size():' + accts.size()); // accts.size():2 oops, size is 2
System.debug('accts:' + accts); // two account records are printed
So uniquness of records is defined NOT ONLY by Id, but rest of the fields are considered as well.
in order to correct storing records to update, you can use Map<Id, Sobject>
Id someAcctId = '001Z000001ORdLz';
Account acct1 = [
select Id, Name
from Account
where Id = :someAcctId
];
Account acct2 = [
select Id, Name
from Account
where Id = :someAcctId
];
acct2.Name += 'Z';
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
sobjectsToUpdate.put(acct1.Id, acct1);
sobjectsToUpdate.put(acct2.Id, acct2);
System.debug('sobjectsToUpdate.size():' + sobjectsToUpdate.size()); // sobjectsToUpdate.size():1 nice!
System.debug('sobjectsToUpdate:' + sobjectsToUpdate); // one account record is printed
Try to put in sets (or in keys of map) immutable types
in your current case, if you want to call upsert DML, you can have two collections: Map to store records to update with key by Id and List for records to insert. Merge them into one collection and do upsert.
Map<Id, Sobject> sobjectsToUpdate = new Map<Id, Sobject>();
List<Sobject> sobjectsToInsert = new List<Sobject>();
...
List<Sobject> sobjectsToUpsert = new List<Sobject>();
sobjectsToUpsert.addAll(sobjectsToUpdate);
sobjectsToUpsert.addAll(sobjectsToInsert);
upsert sobjectsToUpsert;
edited 14 hours ago
answered 14 hours ago
Oleksandr Berehovskiy
6,90611633
6,90611633
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
add a comment |Â
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
This also applies to Lists -- which is good to keep in mind if you need to see whether records have been modified by some other process: salesforce.stackexchange.com/questions/51204/â¦
â Shane Steinfeld
13 hours ago
add a comment |Â
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%2f235263%2fsetsobject-doesnt-maintain-uniqueness-although-i-am-fetching-id-field%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