Using String Object or Field Names instead of literals. Using object prefixesHow to handle REST API JSON responseExternal id specification in child object foreign key reference fieldHow to perform a field describe dynamically using the API name of an sObject?How to get a list of installed packages' names using APIGetting field value dynamically while using describefieldresultSELECT ActivityHistories For Any SObjectSchema global describe map keysetGet Field value from object using string key

Is low emotional intelligence associated with right-wing and prejudiced attitudes?

Where can I get an anonymous Rav Kav card issued?

Can I disable a battery powered device by reversing half of its batteries?

Why isn't `typename` required for a base class that is a nested type?

Glue or not to glue boots

Is there a real-world mythological counterpart to WoW's "kill your gods for power" theme?

What jurisdiction do Scottish courts have over the Westminster parliament?

What are uses of the byte after BRK instruction on 6502?

How to say "quirky" in German without sounding derogatory?

Understanding Cursive /Joined Writing in Irish Register Death

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

Are Democrats more likely to believe Astrology is a science?

Selecting 2 column in an Inner join

Job offer without any details but asking me to withdraw other applications - is it normal?

Could a Scotland-NI bridge break Brexit impasse?

When was the earliest opportunity the Voyager crew had to return to the Alpha quadrant?

Parallel resistance in electric circuits

Is English tonal for some words, like "permit"?

Do they still use tiger roars in the 2019 "Lion King" movie?

How to stabilise the bicycle seatpost and saddle when it is all the way up?

Why is the T-1000 humanoid?

Can the UK veto its own extension request?

Linear Programming with additional "if-then"/"Default to zero" constraints?

Random point on a sphere



Using String Object or Field Names instead of literals. Using object prefixes


How to handle REST API JSON responseExternal id specification in child object foreign key reference fieldHow to perform a field describe dynamically using the API name of an sObject?How to get a list of installed packages' names using APIGetting field value dynamically while using describefieldresultSELECT ActivityHistories For Any SObjectSchema global describe map keysetGet Field value from object using string key






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








1















Is there any benefit of getting sobject type using



Schema.getGlobalDescribe().get('Lead');


instead of



 Lead.sObjectType;


Also what is the benefit of using 3 char prefixes of known IDs like
Id valueId = '00Q......; instead of Id.getSobjectType method?










share|improve this question






























    1















    Is there any benefit of getting sobject type using



    Schema.getGlobalDescribe().get('Lead');


    instead of



     Lead.sObjectType;


    Also what is the benefit of using 3 char prefixes of known IDs like
    Id valueId = '00Q......; instead of Id.getSobjectType method?










    share|improve this question


























      1












      1








      1








      Is there any benefit of getting sobject type using



      Schema.getGlobalDescribe().get('Lead');


      instead of



       Lead.sObjectType;


      Also what is the benefit of using 3 char prefixes of known IDs like
      Id valueId = '00Q......; instead of Id.getSobjectType method?










      share|improve this question














      Is there any benefit of getting sobject type using



      Schema.getGlobalDescribe().get('Lead');


      instead of



       Lead.sObjectType;


      Also what is the benefit of using 3 char prefixes of known IDs like
      Id valueId = '00Q......; instead of Id.getSobjectType method?







      apex bestpractice cleanup






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 15 at 6:06









      pklochkovpklochkov

      5584 silver badges13 bronze badges




      5584 silver badges13 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          2
















          In the first question, Schema.getGlobalDescribe().get('Lead'); is used when you have a object Name as String and you want to create an instance of SObjectType.



          If you already have specific SObject Id then from the Id we can get SObjectType using leadId.getSObjectType()



          Performance wise, first option is much slower than 2nd option.



          You can refer my blog for more details. Efficient way of dynamically casting SObject to Specific Object to update records






          share|improve this answer
































            1
















            If you are writing code where the SObject type is known, the code is compile time checked and also easier to read if you reference the type directly i.e.:



            SObjectType t = Lead.sObjectType;


            or:



            Lead l = ...;
            String company = l.Company;


            Note that an Id has a getSObjectType() method so that is cleaner to use that than looking at the first 3 characters of the Id.



            The 3 character key prefix can be found if you really need it using:



            SObjectType t = Lead.sObjectType;
            String prefix = t.getDescribe).getKeyPrefix();


            Only use the more dynamic style of code when you have to e.g. when you are writing code designed to work with multiple types of SObject or where the concrete type is not available at compile time.



            PS



            Another factor relating to managed packages is whether you want to couple your code to objects that require a more expensive license. Using the dynamic style (and politely failing if the object isn't available) allows your managed package to work with or without the licensed objects.






            share|improve this answer



























            • I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

              – pklochkov
              Apr 15 at 8:29






            • 1





              @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

              – Keith C
              Apr 15 at 9:21






            • 1





              The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

              – Charles T
              Apr 15 at 12:13













            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/4.0/"u003ecc by-sa 4.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%2f257837%2fusing-string-object-or-field-names-instead-of-literals-using-object-prefixes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2
















            In the first question, Schema.getGlobalDescribe().get('Lead'); is used when you have a object Name as String and you want to create an instance of SObjectType.



            If you already have specific SObject Id then from the Id we can get SObjectType using leadId.getSObjectType()



            Performance wise, first option is much slower than 2nd option.



            You can refer my blog for more details. Efficient way of dynamically casting SObject to Specific Object to update records






            share|improve this answer





























              2
















              In the first question, Schema.getGlobalDescribe().get('Lead'); is used when you have a object Name as String and you want to create an instance of SObjectType.



              If you already have specific SObject Id then from the Id we can get SObjectType using leadId.getSObjectType()



              Performance wise, first option is much slower than 2nd option.



              You can refer my blog for more details. Efficient way of dynamically casting SObject to Specific Object to update records






              share|improve this answer



























                2














                2










                2









                In the first question, Schema.getGlobalDescribe().get('Lead'); is used when you have a object Name as String and you want to create an instance of SObjectType.



                If you already have specific SObject Id then from the Id we can get SObjectType using leadId.getSObjectType()



                Performance wise, first option is much slower than 2nd option.



                You can refer my blog for more details. Efficient way of dynamically casting SObject to Specific Object to update records






                share|improve this answer













                In the first question, Schema.getGlobalDescribe().get('Lead'); is used when you have a object Name as String and you want to create an instance of SObjectType.



                If you already have specific SObject Id then from the Id we can get SObjectType using leadId.getSObjectType()



                Performance wise, first option is much slower than 2nd option.



                You can refer my blog for more details. Efficient way of dynamically casting SObject to Specific Object to update records







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 15 at 7:03









                Santanu BoralSantanu Boral

                31.9k5 gold badges26 silver badges57 bronze badges




                31.9k5 gold badges26 silver badges57 bronze badges


























                    1
















                    If you are writing code where the SObject type is known, the code is compile time checked and also easier to read if you reference the type directly i.e.:



                    SObjectType t = Lead.sObjectType;


                    or:



                    Lead l = ...;
                    String company = l.Company;


                    Note that an Id has a getSObjectType() method so that is cleaner to use that than looking at the first 3 characters of the Id.



                    The 3 character key prefix can be found if you really need it using:



                    SObjectType t = Lead.sObjectType;
                    String prefix = t.getDescribe).getKeyPrefix();


                    Only use the more dynamic style of code when you have to e.g. when you are writing code designed to work with multiple types of SObject or where the concrete type is not available at compile time.



                    PS



                    Another factor relating to managed packages is whether you want to couple your code to objects that require a more expensive license. Using the dynamic style (and politely failing if the object isn't available) allows your managed package to work with or without the licensed objects.






                    share|improve this answer



























                    • I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                      – pklochkov
                      Apr 15 at 8:29






                    • 1





                      @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                      – Keith C
                      Apr 15 at 9:21






                    • 1





                      The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                      – Charles T
                      Apr 15 at 12:13















                    1
















                    If you are writing code where the SObject type is known, the code is compile time checked and also easier to read if you reference the type directly i.e.:



                    SObjectType t = Lead.sObjectType;


                    or:



                    Lead l = ...;
                    String company = l.Company;


                    Note that an Id has a getSObjectType() method so that is cleaner to use that than looking at the first 3 characters of the Id.



                    The 3 character key prefix can be found if you really need it using:



                    SObjectType t = Lead.sObjectType;
                    String prefix = t.getDescribe).getKeyPrefix();


                    Only use the more dynamic style of code when you have to e.g. when you are writing code designed to work with multiple types of SObject or where the concrete type is not available at compile time.



                    PS



                    Another factor relating to managed packages is whether you want to couple your code to objects that require a more expensive license. Using the dynamic style (and politely failing if the object isn't available) allows your managed package to work with or without the licensed objects.






                    share|improve this answer



























                    • I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                      – pklochkov
                      Apr 15 at 8:29






                    • 1





                      @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                      – Keith C
                      Apr 15 at 9:21






                    • 1





                      The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                      – Charles T
                      Apr 15 at 12:13













                    1














                    1










                    1









                    If you are writing code where the SObject type is known, the code is compile time checked and also easier to read if you reference the type directly i.e.:



                    SObjectType t = Lead.sObjectType;


                    or:



                    Lead l = ...;
                    String company = l.Company;


                    Note that an Id has a getSObjectType() method so that is cleaner to use that than looking at the first 3 characters of the Id.



                    The 3 character key prefix can be found if you really need it using:



                    SObjectType t = Lead.sObjectType;
                    String prefix = t.getDescribe).getKeyPrefix();


                    Only use the more dynamic style of code when you have to e.g. when you are writing code designed to work with multiple types of SObject or where the concrete type is not available at compile time.



                    PS



                    Another factor relating to managed packages is whether you want to couple your code to objects that require a more expensive license. Using the dynamic style (and politely failing if the object isn't available) allows your managed package to work with or without the licensed objects.






                    share|improve this answer















                    If you are writing code where the SObject type is known, the code is compile time checked and also easier to read if you reference the type directly i.e.:



                    SObjectType t = Lead.sObjectType;


                    or:



                    Lead l = ...;
                    String company = l.Company;


                    Note that an Id has a getSObjectType() method so that is cleaner to use that than looking at the first 3 characters of the Id.



                    The 3 character key prefix can be found if you really need it using:



                    SObjectType t = Lead.sObjectType;
                    String prefix = t.getDescribe).getKeyPrefix();


                    Only use the more dynamic style of code when you have to e.g. when you are writing code designed to work with multiple types of SObject or where the concrete type is not available at compile time.



                    PS



                    Another factor relating to managed packages is whether you want to couple your code to objects that require a more expensive license. Using the dynamic style (and politely failing if the object isn't available) allows your managed package to work with or without the licensed objects.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 15 at 9:19

























                    answered Apr 15 at 8:19









                    Keith CKeith C

                    101k14 gold badges105 silver badges243 bronze badges




                    101k14 gold badges105 silver badges243 bronze badges















                    • I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                      – pklochkov
                      Apr 15 at 8:29






                    • 1





                      @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                      – Keith C
                      Apr 15 at 9:21






                    • 1





                      The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                      – Charles T
                      Apr 15 at 12:13

















                    • I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                      – pklochkov
                      Apr 15 at 8:29






                    • 1





                      @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                      – Keith C
                      Apr 15 at 9:21






                    • 1





                      The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                      – Charles T
                      Apr 15 at 12:13
















                    I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                    – pklochkov
                    Apr 15 at 8:29





                    I do prefer using Lead.sObjectType but does Salesforce puts license constraints when using Lead.sObjectType Opportunity.sObjectType expressions or on field expression when checking accessibility in managed package?

                    – pklochkov
                    Apr 15 at 8:29




                    1




                    1





                    @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                    – Keith C
                    Apr 15 at 9:21





                    @pklochkov I added a PS on that subject. I can't detail the specific consequences (because I don't know them), but the dynamic style is the choice we have made for this situation.

                    – Keith C
                    Apr 15 at 9:21




                    1




                    1





                    The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                    – Charles T
                    Apr 15 at 12:13





                    The main license types that can't access Lead and Opportunity would be Salesforce Platform, right? But if an org contains only those license types it typically comes with one admin license that can see CRM objects but is contractually obligated not to use them. In any case that license is still able to install packages that have CRM object dependencies, as long as end-users don't do anything that interacts with those objects.

                    – Charles T
                    Apr 15 at 12:13


















                    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%2f257837%2fusing-string-object-or-field-names-instead-of-literals-using-object-prefixes%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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?