0

Got following logic in helper class for Task object trigger

public class TaskTriggerHelper {

public static void UpdateNextActionDate(List<Task> taskList) {

    Set<ID> rfpIdSet = new set<Id> (); 
    List<RFP__c> rfpList = new List<RFP__c> ();


    for (Task t : taskList) {
        if (String.valueOf(t.WhatId.getSObjectType()) == 'RFP__c') {
            rfpIdSet.add(t.WhatId);
        }
    }


    Map<Id, Task> rfpIdTaskMap = new Map<Id, Task> ();
    for (Task tsk :[Select Id, Subject, ActivityDate, Status, WhatId FROM Task
         where WhatId in :rfpIdSet
         //AND ActivityDate != null
         AND Status != 'Completed'
         Order by ActivityDate DESC]) {
        rfpIdTaskMap.put(tsk.WhatId, tsk);
    }

    for (Id thisRFP : rfpIdTaskMap.keySet()) {
        RFP__c rfpRec = New RFP__C();
        rfpRec.Id = thisRFP;
        rfpRec.NextActionDate__c = rfpIdTaskMap.get(thisRFP).ActivityDate;

        rfpList.add(rfpRec);
    }

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




}

What code does - updates NextActionDate__c field on custom object (RFP__C) with the ActivityDate field from related task after task has been inserted or updated. Functionality works great, however, the trigger for some reason does not fire if I insert a task record in test class, see following code:

//Create an RFP
    RFP__c testRFP = new RFP__c(Name = 'Test RFP', NextActionDate__c = d, Opportunity__c = testOpportunity.Id);
    insert testRFP;

    System.debug('Test RFP created ' + testRFP);

    //Create task
    Task testTask = new Task(Subject = 'Test',
                             ActivityDate = d2,
                             Priority = 'Normal',
                             Status = 'Open',

                             IsRecurrence = false,
                             IsReminderSet = false,
                             TaskSubtype = 'Task',

                             WhatId = testRFP.id

                             );

    //here Trigger should fire after insert and update NextActionDate__c field on RFP__C object...
    insert testTask; 

    //... however, this assertion fails and NextActionDate__c has the same value as on insert 
    System.assertEquals(testTask.ActivityDate, testRFP.NextActionDate__c);

what am I missing here?

EDIT: Trigger code

trigger Task on Task (after delete, after insert, after update, before insert, before update) {


  TaskTriggerHelper.UpdateNextActionDate(Trigger.new);

}

  • Could you provide a trigger code? – notmyf4ulty Jan 31 '18 at 13:29
  • Added to the end of original post – Juris Upenieks Jan 31 '18 at 13:47
1

To see the value set in ActivityDate, you will need to requery RFP__c in your test after the trigger has run.

(Note the ordering of the assertEquals arguments: the first one should be the expected value and the second one the actual value so that the message output when the assert fails is correct.)

Not an answer to your question, but I would strongly advise that you change your code to this pattern:

public static void UpdateNextActionDate(List<Task> taskList) {

    Set<ID> rfpIdSet = new Set<Id>(); 
    for (Task t : taskList) {
        if (t.WhatId != null && t.WhatId.getSObjectType() == RFP__c.SObjectType) {
            rfpIdSet.add(t.WhatId);
        }
    }
    if (!rfpIdSet.isEmpty()) {
        ...
    } else {
        // Do no harm
    }
}

The point is that Task objects are used in many contexts and by many apps so when you add a trigger you are risking breaking those uses.

So the two changes in the above are:

  • The WhatId isn't always set: your code would NullPointerException for all those cases
  • There is no point in doing a query (5ms and consuming the query governor limit) when there are no Id values
  • Thanks Keith, took that into consideration. In re-gards to requery - it is not Task object where value is updated, it is RFP__C (Custom object) related record. Also, I've tried to re-query that after inserting task but that did not help, value still remains the same (as if trigger wasn`t executed). Any other ideas? – Juris Upenieks Jan 31 '18 at 13:59
  • 1
    @JurisUpenieks My bad on the object - I've changed that. I'd definitely update the question to make it clear that requerying doesn't help. I can't see what's wrong: might be worth looking at the debug logs for any surprises. – Keith C Jan 31 '18 at 14:03
  • Sorry, my bad, figured that out, turns out that requery actually helps but problem was with the syntax used for querying object. When I changed assertion to the format systemAssertEquals(EXPECTED, SOQL QUERY FOR RFP) then it works perfectly fine. Thanks for the assistance! – Juris Upenieks Jan 31 '18 at 14:06

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.