Class Not Passing SObject By Referenceupdate only one cell of a table on button clickNeed help writing test Apex Classeschema.getglobaldescribe needs test classNot able to escape quote in visualforce page?Error on Test Class - System.QueryException: List has no rows for assignment to SObjectCode Coverage to Test Custom Object Public ListTrying to implement a batchable wrapperturn an APEX trigger into scheduled batch updateVoid or non-void methodHow to avoid overwriting data in a Generic sObject List?

Why does blending blueberries, milk, banana and vanilla extract cause the mixture to have a yogurty consistency?

How does one write a Right-to-Left ellipsis in Pages?

Was there a dinosaur-counter in the original Jurassic Park movie?

Class Not Passing SObject By Reference

Is it normal for gliders not to have attitude indicators?

Huffman Code in C++

Sci-fi/fantasy book - ships on steel runners skating across ice sheets

Determine if a grid contains another grid

Installing Debian 10, upgrade to stable later?

Quadrilateral Similarity Question

How to use awk to extract data from a file based on the content of another file?

Which "exotic salt" can lower water's freezing point by –70 °C?

Gerrymandering Puzzle - Rig the Election

Can I combine SELECT TOP() with the IN operator?

Subnumcases as a part of align

Why would a military not separate its forces into different branches?

While drilling into kitchen wall, hit a wire - any advice?

The selling of the sheep

Does Thanos's ship land in the middle of the battlefield in "Avengers: Endgame"?

Can an earth elemental drag a tiny creature underground with Earth Glide?

TIP120 Transistor + Solenoid Failing Randomly

Is there a reason why Turkey took the Balkan territories of the Ottoman Empire, instead of Greece or another of the Balkan states?

Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?

Why can't argument be forwarded inside lambda without mutable?



Class Not Passing SObject By Reference


update only one cell of a table on button clickNeed help writing test Apex Classeschema.getglobaldescribe needs test classNot able to escape quote in visualforce page?Error on Test Class - System.QueryException: List has no rows for assignment to SObjectCode Coverage to Test Custom Object Public ListTrying to implement a batchable wrapperturn an APEX trigger into scheduled batch updateVoid or non-void methodHow to avoid overwriting data in a Generic sObject List?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I’m I created a class that consolidates SObject updates in my triggers. An issue that I am having is that when I call DynamicSObjectUpdater.getSObject(ID), it does not pass the SObject by reference. Is there something that I am missing here? What I am thinking is that when you return a Superclass (i.e. the SObject), it does not pass by reference.



The issue may lie in the following.



targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);


I basically want to do the following:



Contact con = new Contact(ID=sObjectID);


OR



Account acc = new Account(ID=sObjectID);


OR
etc...



I was under the impression that



targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); 


Would do this for me. How do I go about achieving this? –



public with sharing class DyanmicSObjectUpdater 

Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

public SObject getSObject(ID sObjectID)

SObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot))

sorToUpdate.put(sot, new Map<Id, SObject>());

SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);
if(targetSObject == null)

targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
sorToUpdate.get(sot).put(sObjectID, targetSObject);

return targetSObject;

public void updateSObjects()

List<SObject> sObjectsToUpdate = new List<SObject>();
if(sorToUpdate.size() > 0)

for(SObjectType sorType: sorToUpdate.keySet())

sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );


System.debug(sObjectsToUpdate);
if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;




This is the class that calls the SObject Updater.



 public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)

for(Contact con: cons)












share|improve this question






























    1















    I’m I created a class that consolidates SObject updates in my triggers. An issue that I am having is that when I call DynamicSObjectUpdater.getSObject(ID), it does not pass the SObject by reference. Is there something that I am missing here? What I am thinking is that when you return a Superclass (i.e. the SObject), it does not pass by reference.



    The issue may lie in the following.



    targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);


    I basically want to do the following:



    Contact con = new Contact(ID=sObjectID);


    OR



    Account acc = new Account(ID=sObjectID);


    OR
    etc...



    I was under the impression that



    targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); 


    Would do this for me. How do I go about achieving this? –



    public with sharing class DyanmicSObjectUpdater 

    Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

    public SObject getSObject(ID sObjectID)

    SObjectType sot = sObjectID.getSobjectType();
    if(!sorToUpdate.containsKey(sot))

    sorToUpdate.put(sot, new Map<Id, SObject>());

    SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);
    if(targetSObject == null)

    targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
    sorToUpdate.get(sot).put(sObjectID, targetSObject);

    return targetSObject;

    public void updateSObjects()

    List<SObject> sObjectsToUpdate = new List<SObject>();
    if(sorToUpdate.size() > 0)

    for(SObjectType sorType: sorToUpdate.keySet())

    sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );


    System.debug(sObjectsToUpdate);
    if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;




    This is the class that calls the SObject Updater.



     public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)

    for(Contact con: cons)












    share|improve this question


























      1












      1








      1








      I’m I created a class that consolidates SObject updates in my triggers. An issue that I am having is that when I call DynamicSObjectUpdater.getSObject(ID), it does not pass the SObject by reference. Is there something that I am missing here? What I am thinking is that when you return a Superclass (i.e. the SObject), it does not pass by reference.



      The issue may lie in the following.



      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);


      I basically want to do the following:



      Contact con = new Contact(ID=sObjectID);


      OR



      Account acc = new Account(ID=sObjectID);


      OR
      etc...



      I was under the impression that



      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); 


      Would do this for me. How do I go about achieving this? –



      public with sharing class DyanmicSObjectUpdater 

      Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

      public SObject getSObject(ID sObjectID)

      SObjectType sot = sObjectID.getSobjectType();
      if(!sorToUpdate.containsKey(sot))

      sorToUpdate.put(sot, new Map<Id, SObject>());

      SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);
      if(targetSObject == null)

      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
      sorToUpdate.get(sot).put(sObjectID, targetSObject);

      return targetSObject;

      public void updateSObjects()

      List<SObject> sObjectsToUpdate = new List<SObject>();
      if(sorToUpdate.size() > 0)

      for(SObjectType sorType: sorToUpdate.keySet())

      sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );


      System.debug(sObjectsToUpdate);
      if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;




      This is the class that calls the SObject Updater.



       public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)

      for(Contact con: cons)












      share|improve this question
















      I’m I created a class that consolidates SObject updates in my triggers. An issue that I am having is that when I call DynamicSObjectUpdater.getSObject(ID), it does not pass the SObject by reference. Is there something that I am missing here? What I am thinking is that when you return a Superclass (i.e. the SObject), it does not pass by reference.



      The issue may lie in the following.



      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);


      I basically want to do the following:



      Contact con = new Contact(ID=sObjectID);


      OR



      Account acc = new Account(ID=sObjectID);


      OR
      etc...



      I was under the impression that



      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); 


      Would do this for me. How do I go about achieving this? –



      public with sharing class DyanmicSObjectUpdater 

      Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

      public SObject getSObject(ID sObjectID)

      SObjectType sot = sObjectID.getSobjectType();
      if(!sorToUpdate.containsKey(sot))

      sorToUpdate.put(sot, new Map<Id, SObject>());

      SObject targetSObject = sorToUpdate.get(sot).get(sObjectID);
      if(targetSObject == null)

      targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
      sorToUpdate.get(sot).put(sObjectID, targetSObject);

      return targetSObject;

      public void updateSObjects()

      List<SObject> sObjectsToUpdate = new List<SObject>();
      if(sorToUpdate.size() > 0)

      for(SObjectType sorType: sorToUpdate.keySet())

      sObjectsToUpdate.addAll( sorToUpdate.get(sorType).values() );


      System.debug(sObjectsToUpdate);
      if(sObjectsToUpdate.size()>0) update sObjectsToUpdate;




      This is the class that calls the SObject Updater.



       public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)

      for(Contact con: cons)









      apex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      Matthew Metros

















      asked 4 hours ago









      Matthew MetrosMatthew Metros

      966




      966




















          2 Answers
          2






          active

          oldest

          votes


















          2














          According to this post, SObjects (and other non-primitives) are actually passed by value but happen to behave like pass by reference, unless you call new or the equivalent:




          Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.



          The behavior nearly always appears like non-primitives are passed by reference, and you would rarely notice that they are actually not passed by reference. However, when you act on the variable itself in the method, like calling “new” on it, you will notice that things do not behave in pass-by-reference fashion.




          I believe this line of code is giving the pass by value behavior:



          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);





          share|improve this answer

























          • targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

            – Matthew Metros
            3 hours ago












          • What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

            – Matthew Metros
            3 hours ago












          • I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

            – Brian Miller
            2 hours ago











          • targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

            – Matthew Metros
            2 hours ago












          • Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

            – Brian Miller
            2 hours ago


















          1














          The premise of this question seems to me to be mistaken in identifying the issue as pass-by-reference semantics.



          I pasted the DyanmicSObjectUpdater class unaltered in my Developer Edition and ran the following Anonymous Apex:



          Id contactId = '0033600001gyv5BAAQ'; // This is a real Contact
          DyanmicSObjectUpdater s = new DyanmicSObjectUpdater();
          Contact c = (Contact)s.getSObject(contactId);
          c.FirstName = 'TestSobjectUpdater';

          s.updateSObjects();


          Contact 0033600001gyv5BAAQ was updated exactly as expected.



          My suspicion is that the updates you believe are being lost are not being persisted because you are holding a reference to an instance of DyanmicSObjectUpdater in a static variable somewhere but don't call updateSobjects() at the right point, resulting in your instance being reset at a transaction boundary - or at least a problem similar to that structure.



          (Also, Dynamic is misspelled - this drives me crazy in my own code).






          share|improve this answer























          • I added in the class that calls it in the question. Maybe its because I call it within a for loop?

            – Matthew Metros
            5 mins 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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f261190%2fclass-not-passing-sobject-by-reference%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          According to this post, SObjects (and other non-primitives) are actually passed by value but happen to behave like pass by reference, unless you call new or the equivalent:




          Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.



          The behavior nearly always appears like non-primitives are passed by reference, and you would rarely notice that they are actually not passed by reference. However, when you act on the variable itself in the method, like calling “new” on it, you will notice that things do not behave in pass-by-reference fashion.




          I believe this line of code is giving the pass by value behavior:



          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);





          share|improve this answer

























          • targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

            – Matthew Metros
            3 hours ago












          • What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

            – Matthew Metros
            3 hours ago












          • I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

            – Brian Miller
            2 hours ago











          • targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

            – Matthew Metros
            2 hours ago












          • Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

            – Brian Miller
            2 hours ago















          2














          According to this post, SObjects (and other non-primitives) are actually passed by value but happen to behave like pass by reference, unless you call new or the equivalent:




          Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.



          The behavior nearly always appears like non-primitives are passed by reference, and you would rarely notice that they are actually not passed by reference. However, when you act on the variable itself in the method, like calling “new” on it, you will notice that things do not behave in pass-by-reference fashion.




          I believe this line of code is giving the pass by value behavior:



          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);





          share|improve this answer

























          • targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

            – Matthew Metros
            3 hours ago












          • What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

            – Matthew Metros
            3 hours ago












          • I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

            – Brian Miller
            2 hours ago











          • targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

            – Matthew Metros
            2 hours ago












          • Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

            – Brian Miller
            2 hours ago













          2












          2








          2







          According to this post, SObjects (and other non-primitives) are actually passed by value but happen to behave like pass by reference, unless you call new or the equivalent:




          Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.



          The behavior nearly always appears like non-primitives are passed by reference, and you would rarely notice that they are actually not passed by reference. However, when you act on the variable itself in the method, like calling “new” on it, you will notice that things do not behave in pass-by-reference fashion.




          I believe this line of code is giving the pass by value behavior:



          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);





          share|improve this answer















          According to this post, SObjects (and other non-primitives) are actually passed by value but happen to behave like pass by reference, unless you call new or the equivalent:




          Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.



          The behavior nearly always appears like non-primitives are passed by reference, and you would rarely notice that they are actually not passed by reference. However, when you act on the variable itself in the method, like calling “new” on it, you will notice that things do not behave in pass-by-reference fashion.




          I believe this line of code is giving the pass by value behavior:



          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 3 hours ago









          Brian MillerBrian Miller

          871321




          871321












          • targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

            – Matthew Metros
            3 hours ago












          • What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

            – Matthew Metros
            3 hours ago












          • I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

            – Brian Miller
            2 hours ago











          • targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

            – Matthew Metros
            2 hours ago












          • Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

            – Brian Miller
            2 hours ago

















          • targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

            – Matthew Metros
            3 hours ago












          • What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

            – Matthew Metros
            3 hours ago












          • I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

            – Brian Miller
            2 hours ago











          • targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

            – Matthew Metros
            2 hours ago












          • Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

            – Brian Miller
            2 hours ago
















          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

          – Matthew Metros
          3 hours ago






          targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); I basically want to do the following: Contact con = new Contact(ID=sObjectID); I was under the impression that targetSObject = sObjectID.getSobjectType().newSobject(sObjectID); Would do this for me. How do I go about achieving this?

          – Matthew Metros
          3 hours ago














          What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

          – Matthew Metros
          3 hours ago






          What do you mean "unless you call new or the equivalent". Are you saying "New" keyword turns it into a pass by reference?

          – Matthew Metros
          3 hours ago














          I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

          – Brian Miller
          2 hours ago





          I didn't test myself, just noticed what the article was saying - look at the bold and italics at the end of the quote above. Either way, I think I'm missing the bigger picture - what's the issue when you return targetSObject upwards? Maybe show some relevant code at that level?

          – Brian Miller
          2 hours ago













          targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

          – Matthew Metros
          2 hours ago






          targetSObject doesnt pass by reference Contact conToUpdate = (Contact) DynamicSObjectUpdater.getSObject(con.Id); conToUpdate.Name = 'Bob'; that will not change the value of the sobject in: Map<SObjectType, Map<Id, sObject>> sorToUpdate = new Map<SObjectType, Map<Id, sObject>>();

          – Matthew Metros
          2 hours ago














          Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

          – Brian Miller
          2 hours ago





          Why do you need by reference? If the whole sobject is there, with the Id, then what's the difference? Again, showing some code example on that level might help

          – Brian Miller
          2 hours ago













          1














          The premise of this question seems to me to be mistaken in identifying the issue as pass-by-reference semantics.



          I pasted the DyanmicSObjectUpdater class unaltered in my Developer Edition and ran the following Anonymous Apex:



          Id contactId = '0033600001gyv5BAAQ'; // This is a real Contact
          DyanmicSObjectUpdater s = new DyanmicSObjectUpdater();
          Contact c = (Contact)s.getSObject(contactId);
          c.FirstName = 'TestSobjectUpdater';

          s.updateSObjects();


          Contact 0033600001gyv5BAAQ was updated exactly as expected.



          My suspicion is that the updates you believe are being lost are not being persisted because you are holding a reference to an instance of DyanmicSObjectUpdater in a static variable somewhere but don't call updateSobjects() at the right point, resulting in your instance being reset at a transaction boundary - or at least a problem similar to that structure.



          (Also, Dynamic is misspelled - this drives me crazy in my own code).






          share|improve this answer























          • I added in the class that calls it in the question. Maybe its because I call it within a for loop?

            – Matthew Metros
            5 mins ago















          1














          The premise of this question seems to me to be mistaken in identifying the issue as pass-by-reference semantics.



          I pasted the DyanmicSObjectUpdater class unaltered in my Developer Edition and ran the following Anonymous Apex:



          Id contactId = '0033600001gyv5BAAQ'; // This is a real Contact
          DyanmicSObjectUpdater s = new DyanmicSObjectUpdater();
          Contact c = (Contact)s.getSObject(contactId);
          c.FirstName = 'TestSobjectUpdater';

          s.updateSObjects();


          Contact 0033600001gyv5BAAQ was updated exactly as expected.



          My suspicion is that the updates you believe are being lost are not being persisted because you are holding a reference to an instance of DyanmicSObjectUpdater in a static variable somewhere but don't call updateSobjects() at the right point, resulting in your instance being reset at a transaction boundary - or at least a problem similar to that structure.



          (Also, Dynamic is misspelled - this drives me crazy in my own code).






          share|improve this answer























          • I added in the class that calls it in the question. Maybe its because I call it within a for loop?

            – Matthew Metros
            5 mins ago













          1












          1








          1







          The premise of this question seems to me to be mistaken in identifying the issue as pass-by-reference semantics.



          I pasted the DyanmicSObjectUpdater class unaltered in my Developer Edition and ran the following Anonymous Apex:



          Id contactId = '0033600001gyv5BAAQ'; // This is a real Contact
          DyanmicSObjectUpdater s = new DyanmicSObjectUpdater();
          Contact c = (Contact)s.getSObject(contactId);
          c.FirstName = 'TestSobjectUpdater';

          s.updateSObjects();


          Contact 0033600001gyv5BAAQ was updated exactly as expected.



          My suspicion is that the updates you believe are being lost are not being persisted because you are holding a reference to an instance of DyanmicSObjectUpdater in a static variable somewhere but don't call updateSobjects() at the right point, resulting in your instance being reset at a transaction boundary - or at least a problem similar to that structure.



          (Also, Dynamic is misspelled - this drives me crazy in my own code).






          share|improve this answer













          The premise of this question seems to me to be mistaken in identifying the issue as pass-by-reference semantics.



          I pasted the DyanmicSObjectUpdater class unaltered in my Developer Edition and ran the following Anonymous Apex:



          Id contactId = '0033600001gyv5BAAQ'; // This is a real Contact
          DyanmicSObjectUpdater s = new DyanmicSObjectUpdater();
          Contact c = (Contact)s.getSObject(contactId);
          c.FirstName = 'TestSobjectUpdater';

          s.updateSObjects();


          Contact 0033600001gyv5BAAQ was updated exactly as expected.



          My suspicion is that the updates you believe are being lost are not being persisted because you are holding a reference to an instance of DyanmicSObjectUpdater in a static variable somewhere but don't call updateSobjects() at the right point, resulting in your instance being reset at a transaction boundary - or at least a problem similar to that structure.



          (Also, Dynamic is misspelled - this drives me crazy in my own code).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 26 mins ago









          David ReedDavid Reed

          41.4k82463




          41.4k82463












          • I added in the class that calls it in the question. Maybe its because I call it within a for loop?

            – Matthew Metros
            5 mins ago

















          • I added in the class that calls it in the question. Maybe its because I call it within a for loop?

            – Matthew Metros
            5 mins ago
















          I added in the class that calls it in the question. Maybe its because I call it within a for loop?

          – Matthew Metros
          5 mins ago





          I added in the class that calls it in the question. Maybe its because I call it within a for loop?

          – Matthew Metros
          5 mins ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f261190%2fclass-not-passing-sobject-by-reference%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480