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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question





























    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?










    share|improve this question

























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 14 hours ago









      Oleksandr Berehovskiy

      6,90611633




      6,90611633










      asked 14 hours ago









      shukla yogesh

      7611




      7611




















          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;





          share|improve this answer






















          • 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











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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;





          share|improve this answer






















          • 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















          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;





          share|improve this answer






















          • 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













          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;





          share|improve this answer














          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;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)