Set collection doesn't always enforce uniqueness with the Date datatype? Does the following example seem correct?Apex Test not updating parent object field based on child object formula fieldsschema.getglobaldescribe needs test classCode Coverage to Test Custom Object Public Listturn an APEX trigger into scheduled batch updateSalesforce Cookbook - Last Chatter Date - Works MOST Of The TimeIs it strange to declare a collection (Set) with the final keyword as described above?Salesforce Contracts - Testing on Contract term/End DateHaving trouble with a date/time get;set;
How long would it take for people to notice a mass disappearance?
As matter approaches a black hole, does it speed up?
How was the quadratic formula created?
Pressure inside an infinite ocean?
How wide is a neg symbol, how to get the width for alignment?
String won't reverse using reverse_copy
Make some Prime Squares!
Can a nothic's Weird Insight action discover secrets about a player character that the character doesn't know about themselves?
What is the most remote airport from the center of the city it supposedly serves?
Why was the battle set up *outside* Winterfell?
I'm in your subnets, golfing your code
Why do money exchangers give different rates to different bills?
Point of the the Dothraki's attack in GoT S8E3?
How to model the curly cable part of the phone
Verb "geeitet" in an old scientific text
Can an isometry leave entropy invariant?
Can Infinity Stones be retrieved more than once?
What property of a BJT transistor makes it an amplifier?
Why is B♯ higher than C♭ in 31-ET?
Is latino sine flexione dead?
Double or Take game
Has a commercial or military jet bi-plane ever been manufactured?
Set collection doesn't always enforce uniqueness with the Date datatype? Does the following example seem correct?
Why wasn't the Night King naked in S08E03?
Set collection doesn't always enforce uniqueness with the Date datatype? Does the following example seem correct?
Apex Test not updating parent object field based on child object formula fieldsschema.getglobaldescribe needs test classCode Coverage to Test Custom Object Public Listturn an APEX trigger into scheduled batch updateSalesforce Cookbook - Last Chatter Date - Works MOST Of The TimeIs it strange to declare a collection (Set) with the final keyword as described above?Salesforce Contracts - Testing on Contract term/End DateHaving trouble with a date/time get;set;
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Consider the following unit test:
@istest
public static void datetests2()
Set<Date> dts = new Set<Date>();
dts.add(DateTime.now().Date());
dts.add(DateTime.now().addHours(1).Date());
dts.add(DateTime.now().addHours(2).Date());
system.debug(dts);
system.assertEquals(1, dts.size());
The result, as I'm sure you'd expect would be that the test would pass. The debug statement showing something like:
14:49:36:002 USER_DEBUG [22]|DEBUG|2019-05-01 00:00:00
Now consider this very similar unit test:
@istest
public static void datetests()
Set<Date> dts = new Set<Date>();
dts.add(Date.valueOf(DateTime.now()));
dts.add(Date.valueOf(DateTime.now().addHours(1)));
dts.add(Date.valueOf(DateTime.now().addHours(2)));
system.debug(dts);
system.assertEquals(1, dts.size());
You would expect it to pass. Well, I did. But in fact, it fails.
14:57:10:001 FATAL_ERROR System.AssertException: Assertion Failed: Expected: 1, Actual: 3
Displaying something like:
14:57:10:001 USER_DEBUG [10]|DEBUG|2019-05-01 09:00:00, 2019-05-01 10:00:00, 2019-05-01 11:00:00
Now, part of why this may be happening is clear - While logic suggests that Date.ValueOf returns a date, it actually seems to return a DateTime (which contradicts the docs, but whatever...)
In anonymous Apex:
system.debug(Date.ValueOf(DateTime.now()));
Returns at the moment:
15:00:24:002 USER_DEBUG [1]|DEBUG|2019-05-01 10:00:00
which is a datetime
But you'd really expect Set to enforce uniqueness across dates regardless, right?
I've searched online, known issues, etc. and didn't find anything obvious. I confess this completely caught me by surprise.
So my question are:
- Can you reproduce this?
- Can anyone find/show documentation that addresses this?
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
- Any other thoughts or ideas?
Note - this testing is on a fresh SFDX scratch org running Spring 19 API 45. I did run the unit tests at API 40 just to make sure it wasn't a version artifact.
apex datetime date set
add a comment |
Consider the following unit test:
@istest
public static void datetests2()
Set<Date> dts = new Set<Date>();
dts.add(DateTime.now().Date());
dts.add(DateTime.now().addHours(1).Date());
dts.add(DateTime.now().addHours(2).Date());
system.debug(dts);
system.assertEquals(1, dts.size());
The result, as I'm sure you'd expect would be that the test would pass. The debug statement showing something like:
14:49:36:002 USER_DEBUG [22]|DEBUG|2019-05-01 00:00:00
Now consider this very similar unit test:
@istest
public static void datetests()
Set<Date> dts = new Set<Date>();
dts.add(Date.valueOf(DateTime.now()));
dts.add(Date.valueOf(DateTime.now().addHours(1)));
dts.add(Date.valueOf(DateTime.now().addHours(2)));
system.debug(dts);
system.assertEquals(1, dts.size());
You would expect it to pass. Well, I did. But in fact, it fails.
14:57:10:001 FATAL_ERROR System.AssertException: Assertion Failed: Expected: 1, Actual: 3
Displaying something like:
14:57:10:001 USER_DEBUG [10]|DEBUG|2019-05-01 09:00:00, 2019-05-01 10:00:00, 2019-05-01 11:00:00
Now, part of why this may be happening is clear - While logic suggests that Date.ValueOf returns a date, it actually seems to return a DateTime (which contradicts the docs, but whatever...)
In anonymous Apex:
system.debug(Date.ValueOf(DateTime.now()));
Returns at the moment:
15:00:24:002 USER_DEBUG [1]|DEBUG|2019-05-01 10:00:00
which is a datetime
But you'd really expect Set to enforce uniqueness across dates regardless, right?
I've searched online, known issues, etc. and didn't find anything obvious. I confess this completely caught me by surprise.
So my question are:
- Can you reproduce this?
- Can anyone find/show documentation that addresses this?
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
- Any other thoughts or ideas?
Note - this testing is on a fresh SFDX scratch org running Spring 19 API 45. I did run the unit tests at API 40 just to make sure it wasn't a version artifact.
apex datetime date set
add a comment |
Consider the following unit test:
@istest
public static void datetests2()
Set<Date> dts = new Set<Date>();
dts.add(DateTime.now().Date());
dts.add(DateTime.now().addHours(1).Date());
dts.add(DateTime.now().addHours(2).Date());
system.debug(dts);
system.assertEquals(1, dts.size());
The result, as I'm sure you'd expect would be that the test would pass. The debug statement showing something like:
14:49:36:002 USER_DEBUG [22]|DEBUG|2019-05-01 00:00:00
Now consider this very similar unit test:
@istest
public static void datetests()
Set<Date> dts = new Set<Date>();
dts.add(Date.valueOf(DateTime.now()));
dts.add(Date.valueOf(DateTime.now().addHours(1)));
dts.add(Date.valueOf(DateTime.now().addHours(2)));
system.debug(dts);
system.assertEquals(1, dts.size());
You would expect it to pass. Well, I did. But in fact, it fails.
14:57:10:001 FATAL_ERROR System.AssertException: Assertion Failed: Expected: 1, Actual: 3
Displaying something like:
14:57:10:001 USER_DEBUG [10]|DEBUG|2019-05-01 09:00:00, 2019-05-01 10:00:00, 2019-05-01 11:00:00
Now, part of why this may be happening is clear - While logic suggests that Date.ValueOf returns a date, it actually seems to return a DateTime (which contradicts the docs, but whatever...)
In anonymous Apex:
system.debug(Date.ValueOf(DateTime.now()));
Returns at the moment:
15:00:24:002 USER_DEBUG [1]|DEBUG|2019-05-01 10:00:00
which is a datetime
But you'd really expect Set to enforce uniqueness across dates regardless, right?
I've searched online, known issues, etc. and didn't find anything obvious. I confess this completely caught me by surprise.
So my question are:
- Can you reproduce this?
- Can anyone find/show documentation that addresses this?
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
- Any other thoughts or ideas?
Note - this testing is on a fresh SFDX scratch org running Spring 19 API 45. I did run the unit tests at API 40 just to make sure it wasn't a version artifact.
apex datetime date set
Consider the following unit test:
@istest
public static void datetests2()
Set<Date> dts = new Set<Date>();
dts.add(DateTime.now().Date());
dts.add(DateTime.now().addHours(1).Date());
dts.add(DateTime.now().addHours(2).Date());
system.debug(dts);
system.assertEquals(1, dts.size());
The result, as I'm sure you'd expect would be that the test would pass. The debug statement showing something like:
14:49:36:002 USER_DEBUG [22]|DEBUG|2019-05-01 00:00:00
Now consider this very similar unit test:
@istest
public static void datetests()
Set<Date> dts = new Set<Date>();
dts.add(Date.valueOf(DateTime.now()));
dts.add(Date.valueOf(DateTime.now().addHours(1)));
dts.add(Date.valueOf(DateTime.now().addHours(2)));
system.debug(dts);
system.assertEquals(1, dts.size());
You would expect it to pass. Well, I did. But in fact, it fails.
14:57:10:001 FATAL_ERROR System.AssertException: Assertion Failed: Expected: 1, Actual: 3
Displaying something like:
14:57:10:001 USER_DEBUG [10]|DEBUG|2019-05-01 09:00:00, 2019-05-01 10:00:00, 2019-05-01 11:00:00
Now, part of why this may be happening is clear - While logic suggests that Date.ValueOf returns a date, it actually seems to return a DateTime (which contradicts the docs, but whatever...)
In anonymous Apex:
system.debug(Date.ValueOf(DateTime.now()));
Returns at the moment:
15:00:24:002 USER_DEBUG [1]|DEBUG|2019-05-01 10:00:00
which is a datetime
But you'd really expect Set to enforce uniqueness across dates regardless, right?
I've searched online, known issues, etc. and didn't find anything obvious. I confess this completely caught me by surprise.
So my question are:
- Can you reproduce this?
- Can anyone find/show documentation that addresses this?
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
- Any other thoughts or ideas?
Note - this testing is on a fresh SFDX scratch org running Spring 19 API 45. I did run the unit tests at API 40 just to make sure it wasn't a version artifact.
apex datetime date set
apex datetime date set
edited 4 hours ago
David Reed♦
41k82463
41k82463
asked 4 hours ago
kibitzerkibitzer
2,4341324
2,4341324
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
UPDATE
It seems to have been reported as a known issue here. Though the title says String.valueOf
but the code sample in the details reflects what is being observed here.
This overall behavior is impacted because of Date.valueOf
and not Set
, as Set
is behaving as expected. I think you will need to reach out to Salesforce Support (I will try to reach out as well) to report this behavior as the Date.valueOf
should not return the timestamp values at the first place at least based on the documentation.
Details and explanation below as response to your questions.
- Can you reproduce this?
Yes. I was able to reproduce the behavior you have mentioned here.
- Can anyone find/show documentation that addresses this?
You may have (re)discovered a behavior (bug) here in the Date.valueOf
method itself. There's a mention about the timestamp being returned in the docs (emphasis mine):
In API version 33.0 or earlier, if you call
Date.valueOf
with an object that represents aDatetime
, the method returns a Date value that contains the hours, minutes, and seconds.
It just seems with the latest API version, it is behaving just like versions prior to 33.0 (as far as I can confirm)
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
Based on the docs and the behavior, it seems this is a change in the expected behavior.
- Any other thoughts or ideas?
The behavior for Set
is as expected. In your second test, you are able to add the values in the Set
as they are still distinct because of the timestamp that is returned along with. And if there are distinct values, your Set
contains 3 different values in that case. This definitely needs to be reported.
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because aDate
could still contain the timestamp values, theSet
itself seems to be acting upon the data it contains.
– Jayant Das
1 hour ago
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%2f260789%2fset-collection-doesnt-always-enforce-uniqueness-with-the-date-datatype-does-th%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
UPDATE
It seems to have been reported as a known issue here. Though the title says String.valueOf
but the code sample in the details reflects what is being observed here.
This overall behavior is impacted because of Date.valueOf
and not Set
, as Set
is behaving as expected. I think you will need to reach out to Salesforce Support (I will try to reach out as well) to report this behavior as the Date.valueOf
should not return the timestamp values at the first place at least based on the documentation.
Details and explanation below as response to your questions.
- Can you reproduce this?
Yes. I was able to reproduce the behavior you have mentioned here.
- Can anyone find/show documentation that addresses this?
You may have (re)discovered a behavior (bug) here in the Date.valueOf
method itself. There's a mention about the timestamp being returned in the docs (emphasis mine):
In API version 33.0 or earlier, if you call
Date.valueOf
with an object that represents aDatetime
, the method returns a Date value that contains the hours, minutes, and seconds.
It just seems with the latest API version, it is behaving just like versions prior to 33.0 (as far as I can confirm)
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
Based on the docs and the behavior, it seems this is a change in the expected behavior.
- Any other thoughts or ideas?
The behavior for Set
is as expected. In your second test, you are able to add the values in the Set
as they are still distinct because of the timestamp that is returned along with. And if there are distinct values, your Set
contains 3 different values in that case. This definitely needs to be reported.
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because aDate
could still contain the timestamp values, theSet
itself seems to be acting upon the data it contains.
– Jayant Das
1 hour ago
add a comment |
UPDATE
It seems to have been reported as a known issue here. Though the title says String.valueOf
but the code sample in the details reflects what is being observed here.
This overall behavior is impacted because of Date.valueOf
and not Set
, as Set
is behaving as expected. I think you will need to reach out to Salesforce Support (I will try to reach out as well) to report this behavior as the Date.valueOf
should not return the timestamp values at the first place at least based on the documentation.
Details and explanation below as response to your questions.
- Can you reproduce this?
Yes. I was able to reproduce the behavior you have mentioned here.
- Can anyone find/show documentation that addresses this?
You may have (re)discovered a behavior (bug) here in the Date.valueOf
method itself. There's a mention about the timestamp being returned in the docs (emphasis mine):
In API version 33.0 or earlier, if you call
Date.valueOf
with an object that represents aDatetime
, the method returns a Date value that contains the hours, minutes, and seconds.
It just seems with the latest API version, it is behaving just like versions prior to 33.0 (as far as I can confirm)
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
Based on the docs and the behavior, it seems this is a change in the expected behavior.
- Any other thoughts or ideas?
The behavior for Set
is as expected. In your second test, you are able to add the values in the Set
as they are still distinct because of the timestamp that is returned along with. And if there are distinct values, your Set
contains 3 different values in that case. This definitely needs to be reported.
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because aDate
could still contain the timestamp values, theSet
itself seems to be acting upon the data it contains.
– Jayant Das
1 hour ago
add a comment |
UPDATE
It seems to have been reported as a known issue here. Though the title says String.valueOf
but the code sample in the details reflects what is being observed here.
This overall behavior is impacted because of Date.valueOf
and not Set
, as Set
is behaving as expected. I think you will need to reach out to Salesforce Support (I will try to reach out as well) to report this behavior as the Date.valueOf
should not return the timestamp values at the first place at least based on the documentation.
Details and explanation below as response to your questions.
- Can you reproduce this?
Yes. I was able to reproduce the behavior you have mentioned here.
- Can anyone find/show documentation that addresses this?
You may have (re)discovered a behavior (bug) here in the Date.valueOf
method itself. There's a mention about the timestamp being returned in the docs (emphasis mine):
In API version 33.0 or earlier, if you call
Date.valueOf
with an object that represents aDatetime
, the method returns a Date value that contains the hours, minutes, and seconds.
It just seems with the latest API version, it is behaving just like versions prior to 33.0 (as far as I can confirm)
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
Based on the docs and the behavior, it seems this is a change in the expected behavior.
- Any other thoughts or ideas?
The behavior for Set
is as expected. In your second test, you are able to add the values in the Set
as they are still distinct because of the timestamp that is returned along with. And if there are distinct values, your Set
contains 3 different values in that case. This definitely needs to be reported.
UPDATE
It seems to have been reported as a known issue here. Though the title says String.valueOf
but the code sample in the details reflects what is being observed here.
This overall behavior is impacted because of Date.valueOf
and not Set
, as Set
is behaving as expected. I think you will need to reach out to Salesforce Support (I will try to reach out as well) to report this behavior as the Date.valueOf
should not return the timestamp values at the first place at least based on the documentation.
Details and explanation below as response to your questions.
- Can you reproduce this?
Yes. I was able to reproduce the behavior you have mentioned here.
- Can anyone find/show documentation that addresses this?
You may have (re)discovered a behavior (bug) here in the Date.valueOf
method itself. There's a mention about the timestamp being returned in the docs (emphasis mine):
In API version 33.0 or earlier, if you call
Date.valueOf
with an object that represents aDatetime
, the method returns a Date value that contains the hours, minutes, and seconds.
It just seems with the latest API version, it is behaving just like versions prior to 33.0 (as far as I can confirm)
- Does anyone know if this has always been like this, or are we seeing a change in behavior?
Based on the docs and the behavior, it seems this is a change in the expected behavior.
- Any other thoughts or ideas?
The behavior for Set
is as expected. In your second test, you are able to add the values in the Set
as they are still distinct because of the timestamp that is returned along with. And if there are distinct values, your Set
contains 3 different values in that case. This definitely needs to be reported.
edited 1 hour ago
answered 1 hour ago
Jayant DasJayant Das
19.7k21331
19.7k21331
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because aDate
could still contain the timestamp values, theSet
itself seems to be acting upon the data it contains.
– Jayant Das
1 hour ago
add a comment |
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because aDate
could still contain the timestamp values, theSet
itself seems to be acting upon the data it contains.
– Jayant Das
1 hour ago
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Regarding the behavior of set being as expected - shouldn't the fact that the Set is declared as type Date (Set<Date>) enforce uniqueness based on the Date part of the value? Intuitively, that is what I would expect...
– kibitzer
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because a
Date
could still contain the timestamp values, the Set
itself seems to be acting upon the data it contains.– Jayant Das
1 hour ago
Ideally yes but I think because the data in the set is still unique, it works on the data itself. And that based on the docs prior to v34.0 and the known issue, because a
Date
could still contain the timestamp values, the Set
itself seems to be acting upon the data it contains.– Jayant Das
1 hour 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.
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%2f260789%2fset-collection-doesnt-always-enforce-uniqueness-with-the-date-datatype-does-th%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