Best practice using Schema.SObjectType
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
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
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
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 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
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
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
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
yesterday
3
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
yesterday
@ Charles T Thank you Charles! I will change it.
– Salvation
19 hours ago
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
yesterday
3
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
yesterday
@ Charles T Thank you Charles! I will change it.
– Salvation
19 hours ago
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
yesterday
3
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
yesterday
@ Charles T Thank you Charles! I will change it.
– Salvation
19 hours ago
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 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
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
yesterday
@ Charles T Thank you Charles! I will change it.
– Salvation
19 hours ago
add a comment |
Ofcourse! Dont know why I didnt think about it on my own. Thanks a lot!
– Salvation
yesterday
3
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
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
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
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