You need to sign in to do that
Don't have an account?
José Teixeira Gomes
Apex Trigger with field dependencies
Hello, I am new to Salesforce, currently still with a trial version, and I am trying to create an Apex Trigger but before I get into it I wanted to ask if my idea is even possible. So I want a trigger that fires when a task is completed and creates a task the same account in (+) a specific time period. For the trigger part I want it to: (1) change the due date to the date it is completed; (2) schedule the next task depending on an account custom field (frequency); (3) assign it to a contact of that account, but only the one that is assigned as primary (custom field). Also the trigger needs to be built so it can handle mass updates of accounts/contacts/tasks.
I have tried to build something but still have very limited knowledge. If anyone has built anything similar and would like to share it with I would be extremely appreciated. Any help is welcome! Thanks
I have tried to build something but still have very limited knowledge. If anyone has built anything similar and would like to share it with I would be extremely appreciated. Any help is welcome! Thanks
All Answers
You can absolutely do that with an APEX trigger. The first thing you should do is get famiiar with APEX triggers and how they work - I put a blog post out there that can help you out.
http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/
As for the trigger itself, you would do something along these lines: (This code is just in a post - it's not compiled or checked for errors...it's just to guide you)
I have been trying to write something but had no success. I think that maybe it is best if I write two separate triggers: one to update the due date to the completion date if the due date is later than the completion date; and another to create the new task. I think I managed the first one:
trigger CompletedTasks on Task (after update) {
List <Task> CompletedTasks = new List <Task>();
Set <Id> TaskIds = new Set <Id> ();
if (Trigger.isUpdate) {
for(Task tsk : trigger.new)
{
if(tsk.Status != Trigger.oldMap.get (tsk.Id).Status && tsk.Status=='Completed')
{
TaskIds.add (tsk.Id);
}
}
if (!TaskIds.isEmpty () )
{
for (Task tsk: [select Id, Status from Task where Id in :TaskIds])
{
tsk.ActivityDate = System.today();
CompletedTasks.add (tsk);
}
update CompletedTasks;
}
}
}
But now I am wondering if, when I write the other one, it will conflict with the first one since I still it to only fire when the task is completed (is there any order by which they trigger, or is it at the same time). Also, is it better if I include everything in the same trigger?