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?
Which version of the Squat Nimbleness feat is correct?
Was there a dinosaur-counter in the original Jurassic Park movie?
Can you figure out this language?
Why doesn't a particle exert force on itself?
Antivirus for Ubuntu 18.04
What is more safe for browsing the web: PC or smartphone?
Can an earth elemental drag a tiny creature underground with Earth Glide?
Is there any other simpler way to draw the following cross section?
What is the meaning of 「隣のおじいさんは言いました」
How to deal with employer who keeps me at work after working hours
While drilling into kitchen wall, hit a wire - any advice?
Game artist computer workstation set-up – is this overkill?
What does the coin flipping before dying mean?
Why increasing of the temperature of the objects like wood, paper etc. doesn't fire them?
Debian 9 server no sshd in auth.log
Why didn't this character get a funeral at the end of Avengers: Endgame?
Which "exotic salt" can lower water's freezing point by –70 °C?
How to say something covers all the view up to the horizon line?
Why can't argument be forwarded inside lambda without mutable?
Class Not Passing SObject By Reference
TIP120 Transistor + Solenoid Failing Randomly
How important are good looking people in a novel/story?
Where to draw the line between quantum mechanics theory and its interpretation(s)?
What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?
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.
trigger Opportunity_Trigger on Opportunity (before insert, before update, after insert, after update)
DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();
if(Trigger.OperationType == TriggerOperation.AFTER_UPDATE)
UpdateOpportunityContacts.handleTrigger(Trigger.newMap, sObjectUpdater);
sObjectUpdater.updateSObjects();
Then the UpdateOpportunityContacts class
public with sharing class UpdateOpportunityContacts
{
public static void handleTrigger(Map<Id, Opportunity> newOppsMap, DynamicSObjectUpdater sObjectUpdater)
Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole = new Map<Id, List<OpportunityContactRole>>();
for(OpportunityContactRole oppCon : [SELECT id, ContactId , Opportunity.StageName, OpportunityId, Role, Opportunity.Deal_Velocity__c FROM OpportunityContactRole WHERE OpportunityId in : newOppsMap.keyset()])
if(!conIdsWOpportunityContactRole.containsKey(oppCon.ContactId))
conIdsWOpportunityContactRole.put( oppCon.ContactId, New List<OpportunityContactRole>());
conIdsWOpportunityContactRole.get(oppCon.ContactId).add(oppCon);
List<Contact> cons = QuerySelector.dynamicQuerySelector(conIdsWOpportunityContactRole.keyset());
updateContactOutreachStatus(cons, conIdsWOpportunityContactRole, sObjectUpdater);
public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)
for(Contact con: cons)
conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Won (Non-Annual Contract)')
conToUpdate.Outreach_Status__c = 'Current Client';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else if(conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Lost')
conToUpdate.Outreach_Status__c = 'Closed Lost';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else
conToUpdate.Outreach_Status__c = 'Opportunity';
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.
trigger Opportunity_Trigger on Opportunity (before insert, before update, after insert, after update)
DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();
if(Trigger.OperationType == TriggerOperation.AFTER_UPDATE)
UpdateOpportunityContacts.handleTrigger(Trigger.newMap, sObjectUpdater);
sObjectUpdater.updateSObjects();
Then the UpdateOpportunityContacts class
public with sharing class UpdateOpportunityContacts
{
public static void handleTrigger(Map<Id, Opportunity> newOppsMap, DynamicSObjectUpdater sObjectUpdater)
Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole = new Map<Id, List<OpportunityContactRole>>();
for(OpportunityContactRole oppCon : [SELECT id, ContactId , Opportunity.StageName, OpportunityId, Role, Opportunity.Deal_Velocity__c FROM OpportunityContactRole WHERE OpportunityId in : newOppsMap.keyset()])
if(!conIdsWOpportunityContactRole.containsKey(oppCon.ContactId))
conIdsWOpportunityContactRole.put( oppCon.ContactId, New List<OpportunityContactRole>());
conIdsWOpportunityContactRole.get(oppCon.ContactId).add(oppCon);
List<Contact> cons = QuerySelector.dynamicQuerySelector(conIdsWOpportunityContactRole.keyset());
updateContactOutreachStatus(cons, conIdsWOpportunityContactRole, sObjectUpdater);
public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)
for(Contact con: cons)
conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Won (Non-Annual Contract)')
conToUpdate.Outreach_Status__c = 'Current Client';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else if(conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Lost')
conToUpdate.Outreach_Status__c = 'Closed Lost';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else
conToUpdate.Outreach_Status__c = 'Opportunity';
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.
trigger Opportunity_Trigger on Opportunity (before insert, before update, after insert, after update)
DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();
if(Trigger.OperationType == TriggerOperation.AFTER_UPDATE)
UpdateOpportunityContacts.handleTrigger(Trigger.newMap, sObjectUpdater);
sObjectUpdater.updateSObjects();
Then the UpdateOpportunityContacts class
public with sharing class UpdateOpportunityContacts
{
public static void handleTrigger(Map<Id, Opportunity> newOppsMap, DynamicSObjectUpdater sObjectUpdater)
Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole = new Map<Id, List<OpportunityContactRole>>();
for(OpportunityContactRole oppCon : [SELECT id, ContactId , Opportunity.StageName, OpportunityId, Role, Opportunity.Deal_Velocity__c FROM OpportunityContactRole WHERE OpportunityId in : newOppsMap.keyset()])
if(!conIdsWOpportunityContactRole.containsKey(oppCon.ContactId))
conIdsWOpportunityContactRole.put( oppCon.ContactId, New List<OpportunityContactRole>());
conIdsWOpportunityContactRole.get(oppCon.ContactId).add(oppCon);
List<Contact> cons = QuerySelector.dynamicQuerySelector(conIdsWOpportunityContactRole.keyset());
updateContactOutreachStatus(cons, conIdsWOpportunityContactRole, sObjectUpdater);
public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)
for(Contact con: cons)
conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Won (Non-Annual Contract)')
conToUpdate.Outreach_Status__c = 'Current Client';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else if(conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Lost')
conToUpdate.Outreach_Status__c = 'Closed Lost';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else
conToUpdate.Outreach_Status__c = 'Opportunity';
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.
trigger Opportunity_Trigger on Opportunity (before insert, before update, after insert, after update)
DynamicSObjectUpdater sObjectUpdater = new DynamicSObjectUpdater();
if(Trigger.OperationType == TriggerOperation.AFTER_UPDATE)
UpdateOpportunityContacts.handleTrigger(Trigger.newMap, sObjectUpdater);
sObjectUpdater.updateSObjects();
Then the UpdateOpportunityContacts class
public with sharing class UpdateOpportunityContacts
{
public static void handleTrigger(Map<Id, Opportunity> newOppsMap, DynamicSObjectUpdater sObjectUpdater)
Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole = new Map<Id, List<OpportunityContactRole>>();
for(OpportunityContactRole oppCon : [SELECT id, ContactId , Opportunity.StageName, OpportunityId, Role, Opportunity.Deal_Velocity__c FROM OpportunityContactRole WHERE OpportunityId in : newOppsMap.keyset()])
if(!conIdsWOpportunityContactRole.containsKey(oppCon.ContactId))
conIdsWOpportunityContactRole.put( oppCon.ContactId, New List<OpportunityContactRole>());
conIdsWOpportunityContactRole.get(oppCon.ContactId).add(oppCon);
List<Contact> cons = QuerySelector.dynamicQuerySelector(conIdsWOpportunityContactRole.keyset());
updateContactOutreachStatus(cons, conIdsWOpportunityContactRole, sObjectUpdater);
public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)
for(Contact con: cons)
conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Won (Non-Annual Contract)')
conToUpdate.Outreach_Status__c = 'Current Client';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else if(conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.StageName == 'Closed Lost')
conToUpdate.Outreach_Status__c = 'Closed Lost';
conToUpdate.Deal_Velocity__c = conIdsWOpportunityContactRole.get(con.Id)[0].Opportunity.Deal_Velocity__c;
else
conToUpdate.Outreach_Status__c = 'Opportunity';
public static void updateContactOutreachStatus(List<Contact> cons, Map<Id, List<OpportunityContactRole>> conIdsWOpportunityContactRole, DynamicSObjectUpdater sObjectUpdater)
for(Contact con: cons)
apex
apex
edited 17 mins 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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 4 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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 1 hour 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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
38 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
38 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 mins ago
Also sorry for the misspelling! lol
– Matthew Metros
32 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