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;
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
add a comment |
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
add a comment |
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
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
apex
edited 1 hour ago
Matthew Metros
asked 4 hours ago
Matthew MetrosMatthew Metros
966
966
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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);
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 returntargetSObjectupwards? 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
|
show 2 more comments
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).
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
add a comment |
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
);
);
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
Required, but never shown
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
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);
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 returntargetSObjectupwards? 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
|
show 2 more comments
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);
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 returntargetSObjectupwards? 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
|
show 2 more comments
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);
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);
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 returntargetSObjectupwards? 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
|
show 2 more comments
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 returntargetSObjectupwards? 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
|
show 2 more comments
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).
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
add a comment |
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).
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
add a comment |
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).
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).
answered 26 mins ago
David Reed♦David 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
add a comment |
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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