Retrieve SObject using Schema SObjectType class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to create a dynamic class to retrieve a record for update.
I struggle with the following part of the code:
targetSObject = new sor.getSObjectType()(ID = sObjectID);
For example, if this were an account record, I would want the result to be:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
Essentially what I am asking is how do I recreate the above example using the Schema class.
Also, I am curious what the community thinks about retrieving records using the new Sobject(ID = '...')
public with sharing class UpdateAccountGoalsFromOpp {
Map<String, Map<Id, SObject>> sorToUpdate = new Map<String, Map<Id, SObject>>();
public SObject getSObject(ID sObjectID)
{
Schema.DescribeSObjectResult sor = sObjectID.getSobjectType().getDescribe();
String recObject = String.valueOf(sor.getName());
if(!sorToUpdate.containsKey(recObject))
{
sorToUpdate.put(recObject, new Map<Id, SObject>());
}
SObject targetSObject = sorToUpdate.get(recObject).get(sObjectID);
if(targetSObject == null)
{
targetSObject = new sor.getSObjectType()(ID = sObjectID);
sorToUpdate.get(recObject).put(sObjectID, targetSObject);
}
return targetSObject;
}
}
After doing research:
targetSObject = Schema.getGlobalDescribe().get(recObject).newSObject(sObjectID);
^ I think the above code may be the equivalent to:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
apex sobject sobjecttype
add a comment |
I am trying to create a dynamic class to retrieve a record for update.
I struggle with the following part of the code:
targetSObject = new sor.getSObjectType()(ID = sObjectID);
For example, if this were an account record, I would want the result to be:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
Essentially what I am asking is how do I recreate the above example using the Schema class.
Also, I am curious what the community thinks about retrieving records using the new Sobject(ID = '...')
public with sharing class UpdateAccountGoalsFromOpp {
Map<String, Map<Id, SObject>> sorToUpdate = new Map<String, Map<Id, SObject>>();
public SObject getSObject(ID sObjectID)
{
Schema.DescribeSObjectResult sor = sObjectID.getSobjectType().getDescribe();
String recObject = String.valueOf(sor.getName());
if(!sorToUpdate.containsKey(recObject))
{
sorToUpdate.put(recObject, new Map<Id, SObject>());
}
SObject targetSObject = sorToUpdate.get(recObject).get(sObjectID);
if(targetSObject == null)
{
targetSObject = new sor.getSObjectType()(ID = sObjectID);
sorToUpdate.get(recObject).put(sObjectID, targetSObject);
}
return targetSObject;
}
}
After doing research:
targetSObject = Schema.getGlobalDescribe().get(recObject).newSObject(sObjectID);
^ I think the above code may be the equivalent to:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
apex sobject sobjecttype
add a comment |
I am trying to create a dynamic class to retrieve a record for update.
I struggle with the following part of the code:
targetSObject = new sor.getSObjectType()(ID = sObjectID);
For example, if this were an account record, I would want the result to be:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
Essentially what I am asking is how do I recreate the above example using the Schema class.
Also, I am curious what the community thinks about retrieving records using the new Sobject(ID = '...')
public with sharing class UpdateAccountGoalsFromOpp {
Map<String, Map<Id, SObject>> sorToUpdate = new Map<String, Map<Id, SObject>>();
public SObject getSObject(ID sObjectID)
{
Schema.DescribeSObjectResult sor = sObjectID.getSobjectType().getDescribe();
String recObject = String.valueOf(sor.getName());
if(!sorToUpdate.containsKey(recObject))
{
sorToUpdate.put(recObject, new Map<Id, SObject>());
}
SObject targetSObject = sorToUpdate.get(recObject).get(sObjectID);
if(targetSObject == null)
{
targetSObject = new sor.getSObjectType()(ID = sObjectID);
sorToUpdate.get(recObject).put(sObjectID, targetSObject);
}
return targetSObject;
}
}
After doing research:
targetSObject = Schema.getGlobalDescribe().get(recObject).newSObject(sObjectID);
^ I think the above code may be the equivalent to:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
apex sobject sobjecttype
I am trying to create a dynamic class to retrieve a record for update.
I struggle with the following part of the code:
targetSObject = new sor.getSObjectType()(ID = sObjectID);
For example, if this were an account record, I would want the result to be:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
Essentially what I am asking is how do I recreate the above example using the Schema class.
Also, I am curious what the community thinks about retrieving records using the new Sobject(ID = '...')
public with sharing class UpdateAccountGoalsFromOpp {
Map<String, Map<Id, SObject>> sorToUpdate = new Map<String, Map<Id, SObject>>();
public SObject getSObject(ID sObjectID)
{
Schema.DescribeSObjectResult sor = sObjectID.getSobjectType().getDescribe();
String recObject = String.valueOf(sor.getName());
if(!sorToUpdate.containsKey(recObject))
{
sorToUpdate.put(recObject, new Map<Id, SObject>());
}
SObject targetSObject = sorToUpdate.get(recObject).get(sObjectID);
if(targetSObject == null)
{
targetSObject = new sor.getSObjectType()(ID = sObjectID);
sorToUpdate.get(recObject).put(sObjectID, targetSObject);
}
return targetSObject;
}
}
After doing research:
targetSObject = Schema.getGlobalDescribe().get(recObject).newSObject(sObjectID);
^ I think the above code may be the equivalent to:
targetSobject = new Account(ID = '0016A000003tqQfQAI')
apex sobject sobjecttype
apex sobject sobjecttype
edited 36 mins ago
sheilak
378415
378415
asked 10 hours ago
Matthew MetrosMatthew Metros
634
634
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id:
targetSObject = new sor.getSObjectType().newSobject(sObjectID);
As an aside, if you already know the Id, you don't need the describe:
targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
Also, you can use an sObjectType token instead of a string to avoid a describe call entirely:
Map<sObjectType, Map<Id, sObject>> sorToUpdate = new Map<sObjectType, Map<Id, sObject>>();
...
sObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot)) {
sorToUpdate.put(sot, new Map<Id, sObject>());
}
...
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours 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%2f260287%2fretrieve-sobject-using-schema-sobjecttype-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id:
targetSObject = new sor.getSObjectType().newSobject(sObjectID);
As an aside, if you already know the Id, you don't need the describe:
targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
Also, you can use an sObjectType token instead of a string to avoid a describe call entirely:
Map<sObjectType, Map<Id, sObject>> sorToUpdate = new Map<sObjectType, Map<Id, sObject>>();
...
sObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot)) {
sorToUpdate.put(sot, new Map<Id, sObject>());
}
...
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours ago
add a comment |
From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id:
targetSObject = new sor.getSObjectType().newSobject(sObjectID);
As an aside, if you already know the Id, you don't need the describe:
targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
Also, you can use an sObjectType token instead of a string to avoid a describe call entirely:
Map<sObjectType, Map<Id, sObject>> sorToUpdate = new Map<sObjectType, Map<Id, sObject>>();
...
sObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot)) {
sorToUpdate.put(sot, new Map<Id, sObject>());
}
...
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours ago
add a comment |
From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id:
targetSObject = new sor.getSObjectType().newSobject(sObjectID);
As an aside, if you already know the Id, you don't need the describe:
targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
Also, you can use an sObjectType token instead of a string to avoid a describe call entirely:
Map<sObjectType, Map<Id, sObject>> sorToUpdate = new Map<sObjectType, Map<Id, sObject>>();
...
sObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot)) {
sorToUpdate.put(sot, new Map<Id, sObject>());
}
...
From the sObjectType object, use the newSobject method to create a new record in memory. It accepts a single optional parameter for the record Id:
targetSObject = new sor.getSObjectType().newSobject(sObjectID);
As an aside, if you already know the Id, you don't need the describe:
targetSObject = sObjectID.getSobjectType().newSobject(sObjectID);
Also, you can use an sObjectType token instead of a string to avoid a describe call entirely:
Map<sObjectType, Map<Id, sObject>> sorToUpdate = new Map<sObjectType, Map<Id, sObject>>();
...
sObjectType sot = sObjectID.getSobjectType();
if(!sorToUpdate.containsKey(sot)) {
sorToUpdate.put(sot, new Map<Id, sObject>());
}
...
edited 9 hours ago
answered 9 hours ago
sfdcfoxsfdcfox
267k13213461
267k13213461
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours ago
add a comment |
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours ago
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours ago
Amazing, Thank you so much! I just started learning the Schema class today and still really do not understand how to navigate the class. I am beginning to notice patterns, but I am still quite lost on this.
– Matthew Metros
9 hours 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%2f260287%2fretrieve-sobject-using-schema-sobjecttype-class%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