2

I am trying to have my tasks be marked as completed with isclosed mapping to true because I need them to appear in an activity history SOQL query.

I am creating a task with the following field mappings and am very confused as to why isclosed is mapping to false. Doesnt "Completed" move isclosed to true.

            'Subject' => '[Outreach][Out][Outbound][Gmail] Re: Invoice Purchasing',
            'Description' => 'Running a test',
            'ActivityDate' => Date.today()- 1,
            'Status' => 'Completed',
            'WhatId' => Opportunities[0].Id

After I insert the tasks, I am checking the following fields and finding the following results

System.debug(tasks[0].Status); --> Completed
System.debug(tasks[0].IsClosed); ---> False
2
  • 1
    You may like to check what is Completed Status mapped to in your Org. Check the picklist values in Status field.
    – Jayant Das
    Commented Nov 14, 2019 at 18:51
  • It is mapping to closed Commented Nov 14, 2019 at 18:54

1 Answer 1

5

IsClosed is not true until the record is loaded from the database and the Status is a Closed value. This typically means that you need to use after triggers in order to reliably detect when a task has closed. I verified this behavior with the following code:

trigger taskTriggerX on Task (before insert, after insert) {
    for(Task record: Trigger.new) {
        if(Trigger.isBefore && record.Subject=='Before' && !record.IsClosed) {
           record.addError('Is Closed is false in before insert');
        }
        if(Trigger.isAfter && record.Subject=='After' && !record.IsClosed) {
           record.addError('Is Closed is false in after insert');
        }
    }
}

And the following execute anonymous script:

Task[] tasks = new Task[] {
    new Task(Subject='Before',Status='Completed'),
    new Task(Subject='After',Status='Completed')
};
Database.insert(tasks, false);
System.assert(tasks[0].Id == null);
System.assert(tasks[1].Id != null);

Alternatively, if you're checking the data in memory, you need to remember to query the records back from the database:

Task[] tasks = new Task[] {
    new Task(Subject='Before',Status='Completed')
};
Database.insert(tasks, false);
tasks = [SELECT IsClosed FROM Task WHERE Id = :tasks];
System.assert(tasks[0].IsClosed);
4
  • Okay - Well now these are not appearing under my activity history for the Whatid and WhoId. Why is this? Is there another requirement? Commented Nov 14, 2019 at 19:23
  • @MatthewMetros Is this in Classic or Lightning? Is it a Visualforce page or other custom code? Have you tried reloading the page? The more details you can provide, the better.
    – sfdcfox
    Commented Nov 14, 2019 at 19:25
  • I am creating am creating a batch apex class that goes through Opportunities and their activity histories to find the average number of days between touches for the specific opportunities. I am creating a test class and am getting stumped on getting the completed task to appear in this type of query 'SELECT id (SELECT activitydate FROM activityHistories) FROM Opportunity' Commented Nov 14, 2019 at 19:28
  • 1
    @MatthewMetros Hmm. So this question was actually an X-Y Problem. You'll want to ask a new question that includes your unit test code and relevant batch code.
    – sfdcfox
    Commented Nov 14, 2019 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .