Tasks in UI aren’t Querying correctly

Author: Bruce Tollefson Published: May 13, 2022; Modified: May 13, 2022

If you have tried to query a Salesforce Task that you can see in the UI but can’t query via Developer Console or Visual Studio Code. More than likely you are trying to query an archived Task. Don’t worry I have done this many times and keep forgetting about it. Luckily there still is a way to query archived Tasks. In order to query you will need to use the ALL ROWS SOQL statement such as:

Select Id from Task where Id = ALL ROWS

The above will show how to get the Task. If you are looking for a specific field on the Task. Maybe you are looking for the CallDurationInSeconds for the archived Salefsorce Task. You can use the following:

Task t = [Select Id, CallDurationInSeconds from Task where Id ALL ROWS]; System.debug(t.CallDurationInSeconds);

However what if you are looking to investigate the Task. You were looking at the Task in the UI you weren’t able to query the Task where it returned null and you really wanted to get a wide range of fields to see how it was populated back then. Maybe a process has changed since then. You can use the following to view all of the populated fields:

Map<String,Schema.SObjectField> fieldMap = Schema.Task.getSObjectType().getDescribe().fields.getMap(); String queryFieldsStr = ''; for(SObjectField sField :fieldMap.values()){ queryFieldsStr += s.getDescribe().getName()+','; } queryFieldsStr = queryFieldsStr.left(queryFieldsStr.length()-1);//removes the last ',' sObject sObj = Database.query('Select '+queryFieldsStr+' from Task where Id = \'\' ALL ROWS'); system.debug(JSON.serializePretty(sObj));

To make it easier to view the object field values use JSON.serializePretty(), if you aren’t familiar with that method check out this post.

Leave a Reply

Your email address will not be published. Required fields are marked *