+ Start a Discussion
JessBJessB 

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;
}
}

}
}

 

Sean TanSean Tan

You can use the Apex's date object month property to determine if it's the same month.

 

Try this:

 

trigger UpdateLastMonthCompletedTasks on Task(after insert, after update) {
    ASPIRAX.RelationHandler.invoke();
    Map<Id, Account> accountMap = new Map<Id, Account>{};
    
    for (Task t : Trigger.new)
    {
        if (t.WhatId != null && t.WhatId.getSObjectType() == Account.SObjectType)
        {
            accountMap.put(t.WhatId);
        }
    }
    
    if (!accountMap.isEmpty())
    {
        accountMap.putAll([select id,Visits_this_Month__c from account where Id IN :accountMap.keySet()]);                
        Date today = Date.today();
        
        for (Task t: trigger.new)
        {
            If(t.WhatId != null && t.Status=='Completed' && t.Interaction_Type__c == 'Visit' && (t.Date != null && t.Date.month() == today.month()))
            {                                        
                Account a = accountMap.get(t.WhatId);
                if(a.Visits_this_Month__c == null)
                    a.Visits_this_Month__c =1;
                else
                    a.Visits_this_Month__c +=1;                    
            }
        }
        
        update accountMap.values();
    }    
}

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.