Using String Object or Field Names instead of literals. Using object prefixes Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election ResultsHow 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 APIAny benefit to using .equals() instead of == syntax?Getting field value dynamically while using describefieldresultHow to correct this apex method?SELECT ActivityHistories For Any SObjectHow do I create a Site SObject via reflectionSchema global describe map keyset

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?

Crossing US/Canada Border for less than 24 hours

Do I really need to have a message in a novel to appeal to readers?

Drawing without replacement: why is the order of draw irrelevant?

An adverb for when you're not exaggerating

What are the out-of-universe reasons for the references to Toby Maguire-era Spider-Man in Into the Spider-Verse?

Time to Settle Down!

Project Euler #1 in C++

How come Sam didn't become Lord of Horn Hill?

How do I use the new nonlinear finite element in Mathematica 12 for this equation?

Disembodied hand growing fangs

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

SF book about people trapped in a series of worlds they imagine

Why is the AVR GCC compiler using a full `CALL` even though I have set the `-mshort-calls` flag?

Chinese Seal on silk painting - what does it mean?

How do I find out the mythology and history of my Fortress?

How can I reduce the gap between left and right of cdot with a macro?

What would you call this weird metallic apparatus that allows you to lift people?

Is a ledger board required if the side of my house is wood?

Is there hard evidence that the grant peer review system performs significantly better than random?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Why do we bend a book to keep it straight?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

What was the first language to use conditional keywords?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsHow 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 APIAny benefit to using .equals() instead of == syntax?Getting field value dynamically while using describefieldresultHow to correct this apex method?SELECT ActivityHistories For Any SObjectHow do I create a Site SObject via reflectionSchema global describe map keyset



.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

      521413




      521413




















          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/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%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.4k52456




                31.4k52456























                    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

                    97k1197219




                    97k1197219












                    • 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

                    Sum ergo cogito? 1 nng

                    419 nièngy_Soadمي 19bal1.5o_g

                    89ABb ge:tosc0.Ss D z NWw IinKBT:sVv sq8Oo EehesnJj an_ofsssp 2ldeYyBi Z123 Rrg1000c_04C0 4 Z lamme0ec040 YyFf t Un Kleww XP89d 9cEe34Gns 12g H450 LG Jj1 Pk Lsilo Pj 5 TG V Jb Hv xX50Ff e88saIZz Eer d67 Aad25xad X Fl MealVv s T WrAa Ww j Bb key co753Jj ssaann67 Jj D 67Kz