You need to sign in to do that
Don't have an account?
Task Trigger: Need Help to Add Date Range as Additional Criteria
Hi,
I have a custom number field on the account page that is counting the number of activities related to it's record.
My criteria is that the status must be marked as "Completed," but also that my Activity custom field, "Interaction Type" (Interaction_Type__c) be equal to the picklist value, "Visit," and that the Date field is in the current month. I can get as far as setting the criteria that the task status must be marked as Completed, and my custom field picklist value criteria, but the Date field respective to this month critieria, I am stuck on.
Help?
Here is my current code, see below this with my notes inside the code of where I think it may go...
trigger UpdateLastMonthCompletedTasks on Task(after insert, after update){ASPIRAX.RelationHandler.invoke();
Account ac=[select id,Visits_this_Month__c from account where id=:trigger.new[0].whatid];
for (Task t: trigger.new)
If(trigger.new[0].status=='Completed')
{
if(t.Interaction_Type__c=='Visit')
{
if(Ac.Visits_this_Month__c == null)
Ac.Visits_this_Month__c =1;
else
Ac.Visits_this_Month__c +=1;
update ac;
}
}
}
DESIRED:
trigger UpdateLastMonthCompletedTasks on Task(after insert, after update){ASPIRAX.RelationHandler.invoke();
Account ac=[select id,Visits_this_Month__c from account where id=:trigger.new[0].whatid];
for (Task t: trigger.new)
If(trigger.new[0].status=='Completed')
{
if(t.Interaction_Type__c=='Visit')
{
t.Date==THISMONTH()
if(Ac.Visits_this_Month__c == null)
Ac.Visits_this_Month__c =1;
else
Ac.Visits_this_Month__c +=1;
update ac;
}
}
}
}
You can use the Apex's date object month property to determine if it's the same month.
Try this:
You're trigger code also won't handle bulk updates of tasks / inserts of tasks (pointing to different accounts), so I've bulk safed it. It can probably be even more efficient in the first loop by filtering out account ids for tasks that don't match the criteria at all.