function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michelle ArndtMichelle Arndt 

Trigger on custom object tasks

I would like for a field "contacted last 90 days" to be checked if an activity has been performed in that time frame and to uncheck if 90 days has lapsed. If a user hits the log a call or send an email under the activity history is it possible to do this with a trigger since it can't be done with a workflow?
R Z KhanR Z Khan
You would need 2 things
1 a trigger on task that will update the field
2 a scheduled apex that will query your custom recordswhere the field is true and then see if the related tasks were performed in the last 90 days. You would schedule it to run on daily basis or during idfferent intervals based on your need

For trigger 
trigger YOUR_TRIGGERNAME on Task(before insert){
List<YOUR_OBJECT_NAME__c> listToUpdate = new List<YOUR_OBJECT_NAME__c>();
Schema.DescribeSObjectResult r = YOUR_OBJECT_NAME__c.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();

for(Task t: trigger.new){
String objId = t.WhatId;

if(objId.contains(keyPrefix)){
listToUpdate.add(new YOUR_OBJECT_NAME__c(Id=objId, contacted_last_90_days__c=true));
}
}

if(!listToUpdate.isEmpty()){
update listToUpdate;
}


}

for scheudled jobs you can learn more using this trailhead

https://developer.salesforce.com/trailhead/asynchronous_apex/async_apex_scheduled