Most efficient way to switch on SObjectType?Efficient way to create master-detail recordsChecking CRUD for Static SobjectTypeMost efficient way to transform AggregateResults to String, Object MapsMost Efficient Method to Convert Set Type?Writting efficient trigger wayMost efficient way to insert test records for every Opportunity stageNull Pointer if sObjectType isn't CaseGood approach to making switch on string values case insensitive?Get sObjectType by ExternalIDHow can I access variable in switch case outside of switch block?
Why are GND pads often only connected by four traces?
Need to read my home electrical Meter
My players want to grind XP but we're using milestone advancement
Is my plasma cannon concept viable?
Why did Theresa May offer a vote on a second Brexit referendum?
Can I install a back bike rack without attachment to the rear part of the frame?
Dad jokes are fun
Is there a context where the expression `a.b::c` makes sense?
Are there any German nonsense poems (Jabberwocky)?
Is it legal to meet with potential future employers in the UK, whilst visiting from the USA
The art of clickbait captions
How was Daenerys able to legitimise this character?
Is it possible to remotely hack the GPS system and disable GPS service worldwide?
How to deal with a colleague who is being aggressive?
Python program for a simple calculator
What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?
Python program to take in two strings and print the larger string
Dealing with spaghetti codebase, manager asks for things I can't deliver
Why A=2 and B=1 in the call signs for Spirit and Opportunity?
Of strange atmospheres - the survivable but unbreathable
What's difference between "depends on" and "is blocked by" relations between issues in Jira next-gen board?
Is it truly impossible to tell what a CPU is doing?
Is it possible to prohibit all prohibitable schools of magic with a single character?
Why are Stein manifolds/spaces the analog of affine varieties/schemes in algebraic geometry?
Most efficient way to switch on SObjectType?
Efficient way to create master-detail recordsChecking CRUD for Static SobjectTypeMost efficient way to transform AggregateResults to String, Object MapsMost Efficient Method to Convert Set Type?Writting efficient trigger wayMost efficient way to insert test records for every Opportunity stageNull Pointer if sObjectType isn't CaseGood approach to making switch on string values case insensitive?Get sObjectType by ExternalIDHow can I access variable in switch case outside of switch block?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
It is a bit odd we can't evaluate a switch
statement directly on SObjectType
, so I am trying to determine conclusively which workaround is the most efficient.
Just to verify, I tried to compile this code:
void demo(SObjectType input)
switch on input
when Account.sObjectType
when else
And this code generates the error:
Schema.SObjectType is not a valid switch expression type
Bummer. There are workarounds however. The two most obvious that I have come up with:
void demoRecordInstantiation(SObjectType input)
switch on input.newSObject()
when Account a
when else
void demoStringTyping(SObjectType input)
switch on String.valueOf(input)
when 'Account'
when else
Maybe there are others, so if there is an obvious approach I have missed which performs better or is more readable/maintainable, let me know.
My question is, what approach offers the best performance? If I am calling this method many times in a loop, is the SObjectType.newSObject
method resource intensive? Or calling String.valueOf(SObjectType)
? Should I consider caching these values?
apex bestpractice performance sobjecttype switch-case
add a comment |
It is a bit odd we can't evaluate a switch
statement directly on SObjectType
, so I am trying to determine conclusively which workaround is the most efficient.
Just to verify, I tried to compile this code:
void demo(SObjectType input)
switch on input
when Account.sObjectType
when else
And this code generates the error:
Schema.SObjectType is not a valid switch expression type
Bummer. There are workarounds however. The two most obvious that I have come up with:
void demoRecordInstantiation(SObjectType input)
switch on input.newSObject()
when Account a
when else
void demoStringTyping(SObjectType input)
switch on String.valueOf(input)
when 'Account'
when else
Maybe there are others, so if there is an obvious approach I have missed which performs better or is more readable/maintainable, let me know.
My question is, what approach offers the best performance? If I am calling this method many times in a loop, is the SObjectType.newSObject
method resource intensive? Or calling String.valueOf(SObjectType)
? Should I consider caching these values?
apex bestpractice performance sobjecttype switch-case
1
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
2
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
3
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55
add a comment |
It is a bit odd we can't evaluate a switch
statement directly on SObjectType
, so I am trying to determine conclusively which workaround is the most efficient.
Just to verify, I tried to compile this code:
void demo(SObjectType input)
switch on input
when Account.sObjectType
when else
And this code generates the error:
Schema.SObjectType is not a valid switch expression type
Bummer. There are workarounds however. The two most obvious that I have come up with:
void demoRecordInstantiation(SObjectType input)
switch on input.newSObject()
when Account a
when else
void demoStringTyping(SObjectType input)
switch on String.valueOf(input)
when 'Account'
when else
Maybe there are others, so if there is an obvious approach I have missed which performs better or is more readable/maintainable, let me know.
My question is, what approach offers the best performance? If I am calling this method many times in a loop, is the SObjectType.newSObject
method resource intensive? Or calling String.valueOf(SObjectType)
? Should I consider caching these values?
apex bestpractice performance sobjecttype switch-case
It is a bit odd we can't evaluate a switch
statement directly on SObjectType
, so I am trying to determine conclusively which workaround is the most efficient.
Just to verify, I tried to compile this code:
void demo(SObjectType input)
switch on input
when Account.sObjectType
when else
And this code generates the error:
Schema.SObjectType is not a valid switch expression type
Bummer. There are workarounds however. The two most obvious that I have come up with:
void demoRecordInstantiation(SObjectType input)
switch on input.newSObject()
when Account a
when else
void demoStringTyping(SObjectType input)
switch on String.valueOf(input)
when 'Account'
when else
Maybe there are others, so if there is an obvious approach I have missed which performs better or is more readable/maintainable, let me know.
My question is, what approach offers the best performance? If I am calling this method many times in a loop, is the SObjectType.newSObject
method resource intensive? Or calling String.valueOf(SObjectType)
? Should I consider caching these values?
apex bestpractice performance sobjecttype switch-case
apex bestpractice performance sobjecttype switch-case
edited May 17 at 16:47
Adrian Larson
asked May 17 at 15:51
Adrian Larson♦Adrian Larson
113k19124264
113k19124264
1
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
2
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
3
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55
add a comment |
1
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
2
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
3
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55
1
1
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
2
2
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
3
3
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55
add a comment |
1 Answer
1
active
oldest
votes
The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values:
Long t3, t2, t1 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on a.newsobject()
when account ac
when contact co
t2 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on string.valueof(a)
when 'Account'
when 'Contact'
t3 = datetime.now().gettime();
system.debug(t3-t2);
system.debug(t2-t1);
That said, it appears that the actual performance difference is insignificant (less than 0.008 ms per call), so unless you need the performance, choose whichever works best for you.
As an aside, make sure you comment this code for followup later. It has been said salesforce.com would like to include other data types in future releases, and this may include the sObjectType data type as well.
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
add a comment |
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
);
);
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%2f262841%2fmost-efficient-way-to-switch-on-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
The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values:
Long t3, t2, t1 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on a.newsobject()
when account ac
when contact co
t2 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on string.valueof(a)
when 'Account'
when 'Contact'
t3 = datetime.now().gettime();
system.debug(t3-t2);
system.debug(t2-t1);
That said, it appears that the actual performance difference is insignificant (less than 0.008 ms per call), so unless you need the performance, choose whichever works best for you.
As an aside, make sure you comment this code for followup later. It has been said salesforce.com would like to include other data types in future releases, and this may include the sObjectType data type as well.
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
add a comment |
The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values:
Long t3, t2, t1 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on a.newsobject()
when account ac
when contact co
t2 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on string.valueof(a)
when 'Account'
when 'Contact'
t3 = datetime.now().gettime();
system.debug(t3-t2);
system.debug(t2-t1);
That said, it appears that the actual performance difference is insignificant (less than 0.008 ms per call), so unless you need the performance, choose whichever works best for you.
As an aside, make sure you comment this code for followup later. It has been said salesforce.com would like to include other data types in future releases, and this may include the sObjectType data type as well.
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
add a comment |
The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values:
Long t3, t2, t1 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on a.newsobject()
when account ac
when contact co
t2 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on string.valueof(a)
when 'Account'
when 'Contact'
t3 = datetime.now().gettime();
system.debug(t3-t2);
system.debug(t2-t1);
That said, it appears that the actual performance difference is insignificant (less than 0.008 ms per call), so unless you need the performance, choose whichever works best for you.
As an aside, make sure you comment this code for followup later. It has been said salesforce.com would like to include other data types in future releases, and this may include the sObjectType data type as well.
The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string values:
Long t3, t2, t1 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on a.newsobject()
when account ac
when contact co
t2 = datetime.now().gettime();
for(Integer i = 0; i < 100000; i++)
sobjecttype a = account.sobjecttype;
switch on string.valueof(a)
when 'Account'
when 'Contact'
t3 = datetime.now().gettime();
system.debug(t3-t2);
system.debug(t2-t1);
That said, it appears that the actual performance difference is insignificant (less than 0.008 ms per call), so unless you need the performance, choose whichever works best for you.
As an aside, make sure you comment this code for followup later. It has been said salesforce.com would like to include other data types in future releases, and this may include the sObjectType data type as well.
answered May 17 at 16:41
sfdcfoxsfdcfox
271k13219471
271k13219471
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
add a comment |
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
2
2
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
Huh, good to know on that last paragraph. Would be nice to see them add support for this one, as it seems like it would be a very common use case.
– Adrian Larson♦
May 17 at 16:46
2
2
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
@AdrianLarson Yes, I've had a number of times since switch came out that I wished I could use sObjectType. It'd be the most efficient version of the switch statement for its use case.
– sfdcfox
May 17 at 16:48
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
On the plus side, this post is a likely canary if I fail to catch it in the release notes when they do make this change. Seems fair to assume when not if here.
– Adrian Larson♦
May 17 at 16:50
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.
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%2f262841%2fmost-efficient-way-to-switch-on-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
1
I will profile this question as time allows, but right now I am not able to perform an in depth analysis and would be happy for someone to beat me to the punch.
– Adrian Larson♦
May 17 at 15:52
2
i've used these workarounds myself and had to wash my hands afterwards. Let's hope that switch on SObjectType comes soon
– cropredy
May 17 at 16:54
3
I strongly prefer the former, as at least you maintain concrete object references.
– Adrian Larson♦
May 17 at 16:55