Best practice using Schema.SObjectType











up vote
2
down vote

favorite
1












Wants to know please,



1) Why using the Schema.SObjectType is better than using a SOQL?



2) If using the Schema.SObjectType inside a for loop is a bad Idea...
for example:



Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();


I checked and saw that this doesn't affect the SOQL governor Limit - So I guess this is ok to use it inside a for loop?










share|improve this question
























  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    Dec 4 at 10:33










  • @Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
    – Salvation
    Dec 4 at 10:50















up vote
2
down vote

favorite
1












Wants to know please,



1) Why using the Schema.SObjectType is better than using a SOQL?



2) If using the Schema.SObjectType inside a for loop is a bad Idea...
for example:



Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();


I checked and saw that this doesn't affect the SOQL governor Limit - So I guess this is ok to use it inside a for loop?










share|improve this question
























  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    Dec 4 at 10:33










  • @Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
    – Salvation
    Dec 4 at 10:50













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





Wants to know please,



1) Why using the Schema.SObjectType is better than using a SOQL?



2) If using the Schema.SObjectType inside a for loop is a bad Idea...
for example:



Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();


I checked and saw that this doesn't affect the SOQL governor Limit - So I guess this is ok to use it inside a for loop?










share|improve this question















Wants to know please,



1) Why using the Schema.SObjectType is better than using a SOQL?



2) If using the Schema.SObjectType inside a for loop is a bad Idea...
for example:



Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();


I checked and saw that this doesn't affect the SOQL governor Limit - So I guess this is ok to use it inside a for loop?







apex soql loop schema






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 12:11









m Peixoto

409213




409213










asked Dec 4 at 10:22









Salvation

565




565












  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    Dec 4 at 10:33










  • @Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
    – Salvation
    Dec 4 at 10:50


















  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    Dec 4 at 10:33










  • @Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
    – Salvation
    Dec 4 at 10:50
















Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
– Manjot Singh
Dec 4 at 10:33




Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
– Manjot Singh
Dec 4 at 10:33












@Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
– Salvation
Dec 4 at 10:50




@Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
– Salvation
Dec 4 at 10:50










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










Calling Schema class again and again in loop is not a best practice. It doesn't consume soul limits but can cause




cpu time limit exceed exception




. So what I normally do is get schema describe result of any object outside loop and then inside loop get which ever value you want to get from that object.



Like for your case I will do



Map<String, Schema.RecordTypeInfo> recordtypeMap = Schema.SObjectType.Account.getRecordTypeInfosByName();


and in loop use recordtypeMap.get('recordtype').getRecordTypeId();



In this way I am calling schema class only once and using it at multiple places.






share|improve this answer





















  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    Dec 4 at 10:53






  • 4




    BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
    – Charles T
    Dec 4 at 13:02










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    Dec 5 at 11:09











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%2f241337%2fbest-practice-using-schema-sobjecttype%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








up vote
4
down vote



accepted










Calling Schema class again and again in loop is not a best practice. It doesn't consume soul limits but can cause




cpu time limit exceed exception




. So what I normally do is get schema describe result of any object outside loop and then inside loop get which ever value you want to get from that object.



Like for your case I will do



Map<String, Schema.RecordTypeInfo> recordtypeMap = Schema.SObjectType.Account.getRecordTypeInfosByName();


and in loop use recordtypeMap.get('recordtype').getRecordTypeId();



In this way I am calling schema class only once and using it at multiple places.






share|improve this answer





















  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    Dec 4 at 10:53






  • 4




    BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
    – Charles T
    Dec 4 at 13:02










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    Dec 5 at 11:09















up vote
4
down vote



accepted










Calling Schema class again and again in loop is not a best practice. It doesn't consume soul limits but can cause




cpu time limit exceed exception




. So what I normally do is get schema describe result of any object outside loop and then inside loop get which ever value you want to get from that object.



Like for your case I will do



Map<String, Schema.RecordTypeInfo> recordtypeMap = Schema.SObjectType.Account.getRecordTypeInfosByName();


and in loop use recordtypeMap.get('recordtype').getRecordTypeId();



In this way I am calling schema class only once and using it at multiple places.






share|improve this answer





















  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    Dec 4 at 10:53






  • 4




    BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
    – Charles T
    Dec 4 at 13:02










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    Dec 5 at 11:09













up vote
4
down vote



accepted







up vote
4
down vote



accepted






Calling Schema class again and again in loop is not a best practice. It doesn't consume soul limits but can cause




cpu time limit exceed exception




. So what I normally do is get schema describe result of any object outside loop and then inside loop get which ever value you want to get from that object.



Like for your case I will do



Map<String, Schema.RecordTypeInfo> recordtypeMap = Schema.SObjectType.Account.getRecordTypeInfosByName();


and in loop use recordtypeMap.get('recordtype').getRecordTypeId();



In this way I am calling schema class only once and using it at multiple places.






share|improve this answer












Calling Schema class again and again in loop is not a best practice. It doesn't consume soul limits but can cause




cpu time limit exceed exception




. So what I normally do is get schema describe result of any object outside loop and then inside loop get which ever value you want to get from that object.



Like for your case I will do



Map<String, Schema.RecordTypeInfo> recordtypeMap = Schema.SObjectType.Account.getRecordTypeInfosByName();


and in loop use recordtypeMap.get('recordtype').getRecordTypeId();



In this way I am calling schema class only once and using it at multiple places.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 10:51









Manjot Singh

2,180521




2,180521












  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    Dec 4 at 10:53






  • 4




    BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
    – Charles T
    Dec 4 at 13:02










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    Dec 5 at 11:09


















  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    Dec 4 at 10:53






  • 4




    BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
    – Charles T
    Dec 4 at 13:02










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    Dec 5 at 11:09
















Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53




Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53




4




4




BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
– Charles T
Dec 4 at 13:02




BTW getRecordTypeInfosByName is bad practice for looking up a RecordType because it will give different results for different user languages. Use the new getRecordTypeInfosByDeveloperName. Make sure the Apex class is set to at least version 43.
– Charles T
Dec 4 at 13:02












@ Charles T Thank you Charles! I will change it.
– Salvation
Dec 5 at 11:09




@ Charles T Thank you Charles! I will change it.
– Salvation
Dec 5 at 11:09


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f241337%2fbest-practice-using-schema-sobjecttype%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

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei