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 do I make a function that generates nth natural number that isn't a perfect square?
Upside-Down Pyramid Addition...REVERSED!
How to apply differences on part of a list and keep the rest
Fitch Proof Question
Make some Prime Squares!
Why was the battle set up *outside* Winterfell?
How wide is a neg symbol, how to get the width for alignment?
What to use instead of cling film to wrap pastry
Building a list of products from the elements in another list
What was the design of the Macintosh II's MMU replacement?
Multi-channel audio upsampling interpolation
How can I close a gap between my fence and my neighbor's that's on his side of the property line?
Send iMessage from Firefox
On which topic did Indiana Jones write his doctoral thesis?
What are the differences between credential stuffing and password spraying?
Why do people keep telling me that I am a bad photographer?
Prove that the limit exists or does not exist
Why is B♯ higher than C♭ in 31-ET?
If your medical expenses exceed your income does the IRS pay you?
Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?
Missing Piece of Pie - Can you find it?
Position of past participle and extent of the Verbklammer
How does this change to the opportunity attack rule impact combat?
What property of a BJT transistor makes it an amplifier?
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 3 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