You need to sign in to do that
Don't have an account?
DougWD
Activity Completed Apex Trigger
Hi, I am new to apex trigger programming, though a long-time programmer.
I would like to track the date and time of day when tasks are marked "Completed". This functionality is not captured by Due Date or Date of Last Activity.
trigger DateCompleted on Task ( before insert, before update )
{
try
{
// task.DateCompleted = todaysDate();
// task.TimeCompleted = now;
}
catch ( Exception ex )
{
}
}
I'd appreciate your help on this - also (and related), I looked around for some time, but could not find a resource with a bunch of example Apex triggers - this would be really helpful.
I would like to track the date and time of day when tasks are marked "Completed". This functionality is not captured by Due Date or Date of Last Activity.
trigger DateCompleted on Task ( before insert, before update )
{
try
{
// task.DateCompleted = todaysDate();
// task.TimeCompleted = now;
}
catch ( Exception ex )
{
}
}
I'd appreciate your help on this - also (and related), I looked around for some time, but could not find a resource with a bunch of example Apex triggers - this would be really helpful.
All Answers
Look here: http://wiki.apexdevnet.com/index.php/Documentation
Your trigger seems pretty straightforward. Assuming you created a custom field to hold the completed datetime, it would look something like this:
You will also need to create a Testmethod to test the trigger before it can be deployed to production. Both the trigger and the testmethod will need to be deployed.
Here is a simple testmethod:
Good Luck!!!
Trigger DateCompleted on Task (before update, before insert) {
for (Task t : Trigger.new) {
}
if(t.DateCompleted__c == NULL && t.IsClosed)
{
t.DateCompleted__c = date.today();
t.DateTimeCompleted__c = date.now();
}
}
I had created the t.DateCompleted__c and t.DateTimeCompleted__c fields as custom Activity fields in my sandbox. I recieved the error:
Variable does not exist: t.DateCompleted__c at line 6 column 9
Do I need to do something special to get to a custom activity field from a task (tasks don't seem to have their own custom fields). Thanks!
IF you called it Date Completed when you created it, the API name would be Date_Completed__c. Go back to the setup, and select the field you created in the Custom Activity Fields. It will show you what the API name is.
Field Definition Detail
Review all error messages below to correct your data.
Field Information:
Did you set the field level security so that the new field was visible (to at least your profile) and not read only? Ultimately it will need to be visible to any profile that could cause the trigger to fire.
Too easy. Im suprised everyone missed this:
Look at the bit in bold. "t" only exists within the "for" segment.
Here is a workaround that does not require an Apex Trigger. You can create a workflow rule that fires an immediate action field update to update a custom field.
Evaluation Criteria: Every time a record is created or edited
Rule Criteria: NOT(ISPICKVAL(PRIORVALUE (Status), "Completed")) && ISPICKVAL(Status, "Completed")
Field Update: set custom field "Date Completed" = NOW()
This seems to be working great for me and requires no Apex triggers and test methods.
Great solution - thanks!