2

I need to write trigger on task object

On Contact workflow will fire based on fields update: enter image description here

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:

enter image description here

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

enter image description here

enter image description here

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:

enter image description here

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

   }

enter image description here

  • So what is the output of your debugs? – Eric Aug 18 '16 at 13:28
  • There is a debug and nothing happening. Finally i need to assign the survey record to that Related To (WhatId). – Neymar Da Silva Aug 18 '16 at 13:32
  • 1
    Can you share the task trigger? I can only see the class, maybe your trigger isn't calling the class... – Girbot Aug 18 '16 at 13:39
  • Thank you very much for your time, I appreciate. Please find Trigger and handler. – Neymar Da Silva Aug 18 '16 at 13:53
2

What I would do is collect the Contact Ids as you are now and query the Contacts directly with the Surveys as a subquery. This way it's easier to associate the Survey with the right Contact.

Here is some sample code.

// SOQL Query gets all Contacts and their child surveys (assuming Most_Recent_NPS_Survey__c = TRUE results in only one survey per contact)
Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id, (SELECT Id FROM Surveys__r WHERE Most_Recent_NPS_Survey__c = true) FROM Contact WHERE Id IN :completedIDs]);

for(Task record : scoredTasks)
{
    // Get the contact
    Contact c = contactMap.get(record.WhoId);
    // Make sure a Contact record was found, and it has at least one survey record
    if(c != null && !c.Surveys__r.isEmpty())
    {
        // Get the survey record
        Survey__c survey = c.Surveys__r[0];

        // Assign Survey to Task record
        record.WhatId = survey.Id;
    }
}

My code assumes that the WHERE condition WHERE Most_Recent_NPS_Survey__c = true will return at max one survey. If this is not the case then you may need to do some extra processing to ensure you get the right survey with score < 7

| improve this answer | |
  • I am getting error at this line "Survey__c survey = contactMap.Surveys__r[0]; " Error: Compile Error: Initial term of field expression must be a concrete SObject: Map<Id,Contact> at line 33 column 28 Should i use get or how overcome it ? – Neymar Da Silva Aug 18 '16 at 14:12
  • There was a typo in my code. I updated my answer, basically change Survey__c survey = contactMap.Surveys__r[0]; to Survey__c survey = c.Surveys__r[0]; – dBeltowski Aug 18 '16 at 14:18
  • Hi you code was good but nothing happening. The related To is not updated and Why I'm not getting Logs that is what weird for me. – Neymar Da Silva Aug 18 '16 at 14:22
  • @NeymarDaSilva Are you sure the code is actually being run? Can you double check your Trigger_Controls__c custom setting to ensure that Task_Trigger__c is evaluating to TRUE – dBeltowski Aug 18 '16 at 14:28
  • Yes that is default Checked. Please see the picture – Neymar Da Silva Aug 18 '16 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.