Best practice using Schema.SObjectType





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






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
    yesterday










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

















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
    yesterday










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













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 yesterday









m Peixoto

384113




384113










asked yesterday









Salvation

174




174












  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    yesterday










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


















  • Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
    – Manjot Singh
    yesterday










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
















Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
– Manjot Singh
yesterday




Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId() why are you getting same recordTypeId in loop ? are you changing record type in loop?
– Manjot Singh
yesterday












@Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
– Salvation
yesterday




@Manjot Singh, Yes, I need to create accounts with different RC base on some logic.. But it all happens in a Loop.
– Salvation
yesterday










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
    yesterday






  • 3




    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
    yesterday










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    19 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',
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
    yesterday






  • 3




    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
    yesterday










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    19 hours ago















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
    yesterday






  • 3




    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
    yesterday










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    19 hours ago













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 yesterday









Manjot Singh

2,130521




2,130521












  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    yesterday






  • 3




    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
    yesterday










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    19 hours ago


















  • Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
    – Salvation
    yesterday






  • 3




    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
    yesterday










  • @ Charles T Thank you Charles! I will change it.
    – Salvation
    19 hours ago
















Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
yesterday




Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
yesterday




3




3




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
yesterday




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
yesterday












@ Charles T Thank you Charles! I will change it.
– Salvation
19 hours ago




@ Charles T Thank you Charles! I will change it.
– Salvation
19 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.





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