Retrieve SObject using Schema SObjectType class





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







2















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')









share|improve this question































    2















    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')









    share|improve this question



























      2












      2








      2








      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')









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 31 mins ago









      sheilak

      378415




      378415










      asked 10 hours ago









      Matthew MetrosMatthew Metros

      634




      634






















          1 Answer
          1






          active

          oldest

          votes


















          3














          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>());
          }
          ...





          share|improve this answer


























          • 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












          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%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









          3














          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>());
          }
          ...





          share|improve this answer


























          • 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
















          3














          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>());
          }
          ...





          share|improve this answer


























          • 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














          3












          3








          3







          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>());
          }
          ...





          share|improve this answer















          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>());
          }
          ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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


















          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%2f260287%2fretrieve-sobject-using-schema-sobjecttype-class%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

          Category:香港粉麵

          List *all* the tuples!

          Channel [V]