Showing posts with label Workflow. Show all posts
Showing posts with label Workflow. Show all posts

Thursday, December 29, 2011

Last Modified Date Rounding to Whole Seconds

The DateTime class in Salesforce has precision down to the order of milliseconds, as implied by the getTime method which "returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object." Since the Last Modified Date field is a Date/Time field, one would assume that the Last Modified Date includes the millisecond value when stamping a record, right? Alas, this is not the case.

A video demonstrating the behavior: http://www.youtube.com/watch?v=IyRd1woiS24

It seems that Salesforce only includes the year, month, day, hour, minute and second when stamping the Last Modified Date. The millisecond value is simply zeroed out without any rounding. So, if a record was actually modified at 12/29/2011 12:31:20.578, Salesforce will stamp the record as being modified at 12/29/2011 12:31:20.000.

Okay, so there's a tiny discrepancy. I mean, we're talking tiny differences here. The million-dollar question: Why do I care?

I care because this little quirk caused my unit test to fail over 80% of the time due to the fact that comparing the Last Modified Date to DateTime.now() was producing unexpected results. For example:
  • At the beginning of the test method, DateTime.now() returned 12/29/2011 4:00:01.500.
  • At the end of the test method, the updated record's Last Modified Date shows 12/29/2011 4:00:01.000.
  • System.assert(record.LastModifiedDate >= testStartDateTime) fails unexpectedly and inconsistently.

The oddity with Last Modified Date is not an impossible problem to work around, but should I really be working around it? What does this mean for test-driven development that Apex is supposed to enable?

Thursday, November 17, 2011

Notification of New Comment on Idea in Salesforce

A quick Google search for "salesforce idea new comment notification" revealed that I wasn't the only person frustrated with how un-social Ideas are in the Social Enterprise. My only consolation is that at least it is possible to setup an Email Alert to notify the Idea Creator that a new comment was added, only when a new comment is added and not when the Idea is modified.

To do this, we just need three things:
  • Workflow Rule: Alert Creator on New Comment
  • Email Alert: Idea: New Comment Added
  • Email Template: Idea: New Comment

Workflow Rule: Alert Creator on New Comment

Object: Idea
Evaluation Criteria: Every time a record is created or edited
Rule Criteria: LastCommentDate > LastModifiedDate

Email Alert: Idea: New Comment Added

Object: Idea
Recipients: Record Creator

Email Template: Idea: New Comment

Subject:
New Comment ({!Idea.NumComments}) on Idea: {!Idea.Title}
Plain Text Body:
A new comment has been added to your Idea: https://na11.salesforce.com/ideas/viewIdea.apexp?id={!Idea.Id}

Please use Chatter or email to notify others who may also wish to respond to the comment, since you are the only person who received this notification.

Chatter: https://na11.salesforce.com/_ui/core/chatter/ui/ChatterPage

Thank you

Thank you, everyone who talked about this problem and frustration in the online communities:

Thursday, October 13, 2011

Best Practice for Automated Task Creation?

In retrospect, I think a best practice for automating task creation in Salesforce is to use workflow rules instead of Apex triggers. The primary reason is that the tasks are easier to manage and can be avoided during a mass update is the user leaves the "trigger workflow" checkbox unmarked in the import wizard.