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
Eric BrunerEric Bruner 

Writing custom trigger that applies points to task subjects

I am trying to write a trigger that will add points to a custom field within Leads based on the subject and created by id of a task.  These points would need to accumulate on top of each other over time.  I am completely new to writing triggers and am looking for any recommendations or starting points.
Nathan EhrmannNathan Ehrmann
Hi Eric,

Not a problem! We can write a trigger that calculates these values and sends them on up to the associated Lead via the WhoId.
 
trigger YourTrigger on Task(after insert){
    
    TriggerHelper.leadPoints(Trigger.New);
    //consider standard callout format
}

public class TriggerHelper{
    
    public static void leadPoints(List<Task> tasks){
        
        Set<Id> taskId = new Set<Id>();
        for(Task t : tasks){
            taskId.add(t.Id);
        }
        
        Map<Id, Lead> idToLead = new Map<Id, Lead>([SELECT Id, Points__c FROM Lead WHERE WhoId IN :taskId]);

        for(Task t : tasks){
            Integer points = 0;
            //logic for determining points from Task
            idToLead.get(t.WhoId).Points__c += points;
        }

        UPDATE idToLead.values();
    }
}
Let me know if I can provide any more assistance! I was exactly where you are not that long ago.