Best practice using Schema.SObjectType
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
apex soql loop schema
edited Dec 4 at 12:11
m Peixoto
386113
386113
asked Dec 4 at 10:22
Salvation
385
385
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
add a comment |
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
add a comment |
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.
Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53
4
BTWgetRecordTypeInfosByName
is bad practice for looking up a RecordType because it will give different results for different user languages. Use the newgetRecordTypeInfosByDeveloperName
. 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
add a comment |
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.
Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53
4
BTWgetRecordTypeInfosByName
is bad practice for looking up a RecordType because it will give different results for different user languages. Use the newgetRecordTypeInfosByDeveloperName
. 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
add a comment |
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.
Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53
4
BTWgetRecordTypeInfosByName
is bad practice for looking up a RecordType because it will give different results for different user languages. Use the newgetRecordTypeInfosByDeveloperName
. 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
add a comment |
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.
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.
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
BTWgetRecordTypeInfosByName
is bad practice for looking up a RecordType because it will give different results for different user languages. Use the newgetRecordTypeInfosByDeveloperName
. 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
add a comment |
Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
Dec 4 at 10:53
4
BTWgetRecordTypeInfosByName
is bad practice for looking up a RecordType because it will give different results for different user languages. Use the newgetRecordTypeInfosByDeveloperName
. 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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