I need to write trigger on task object
On Contact workflow will fire based on fields update:
This workflow will create task with related to this Contact:
Now there is survey Object and and there are few survey records with this contact:
Now i need to update the task field called Related to With the condition of Most_Recent_NPS_Survey__c = TRUE of Survey record with related to same contact
This is what created trigger but when creating Task i am getting nothing. Can you please any one help me find solution:
public class TaskTriggerActions {
public static void RecentSurvey(List<Task> lTask) {
Set<Id> completedIds = new Set<Id>();
Task[] scoredTasks = new Task[0];
for(Task record: lTask) {
if(record.WhoId != null && record.Subject != null &&
record.Subject.contains('Contact scored one or more questions')) {
//Add the task id to the array and contactid to the set
scoredTasks.add(record);
completedIds.add(record.WhoId);
}
system.debug('List of Task Ids: '+scoredTasks);
system.debug('List of WhoIds: '+completedIds);
}
if(scoredTasks.isEmpty()) {
return;
}
//Get all the surveys related to the contact in the task
Map<Id, Id> contactToSurveys = new Map<Id, Id>();
for(Survey__c record: [SELECT Contact__c FROM Survey__c WHERE Most_Recent_NPS_Survey__c = TRUE AND Contact__c IN :completedIds]) {
contactToSurveys.put(record.Contact__c, record.Id);
system.debug('List of Surveys: '+contactToSurveys);
}
//Trasfer the survey id from the map to the instance of original task
for(Task record: scoredTasks) {
record.WhatId = contactToSurveys.get(record.Id);
system.debug('Task what id: '+record.whatid);
}
}}
Here is my debug:
Please find Handler and Trigger:
public class TaskTriggerHandler {
//variable declaration
private boolean m_isExecuting = false;
private integer BatchSize = 0;
//constructor
public TaskTriggerHandler(boolean isExecuting, integer size){
m_isExecuting = isExecuting;
BatchSize = size;
}
public void OnBeforeInsert(List<Task> triggerNew){
TaskTriggerActions.RecentSurvey(triggerNew);
}//end of before Insert
public void OnAfterInsert(List<Task> triggerNew){
}//end of after Insert
public void OnBeforeUpdate(List<Task> triggerNew){
}//end of before Update
public void OnAfterUpdate(List<Task> triggerNew){
}//end of after Update
}
Trigger
trigger TaskTrigger on Task (before insert,after insert,before update,after update) {
Trigger_Controls__c CS = Trigger_Controls__c.getOrgDefaults();
if(CS.Task_Trigger__c){// trigger toggle
TaskTriggerHandler handler = new TaskTriggerHandler (Trigger.isExecuting, Trigger.size);
//Runs on before insert only
if(Trigger.isBefore && Trigger.isInsert) {
handler.OnBeforeInsert(trigger.new);
}//e
//Runs on after insert only
if(Trigger.isAfter && Trigger.isInsert) {
handler.onAfterInsert(trigger.new);
}//end of after insert condition
//Runs on before update only
if(Trigger.isBefore && Trigger.isUpdate) {
handler.onBeforeUpdate(trigger.new);
}//end of before update condition
//Runs on after update only
// if(Trigger.isAfter && Trigger.isUpdate) {
// handler.OnAfterUpdate(trigger.new,trigger.old,trigger.newMap,trigger.oldMap);
//}//end of afte update condition
}// end of trigger toggle
}