function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kelly KKelly K 

SOQL Not Returning Requested Fields

Hi All, 

I'm currently writing a test batch class and I'm scratching my head trying to figure out why my SOQL query is not returning the requested fields:

Here's the condensed version of the class:

@isTest static void testTaskUpdateActivityInfoBatchTest() {
		DateTime olderTime = system.now().addMinutes(-5);
		DateTime newestTime = system.now().addMinutes(-3);
		//Pull a random active sales guy
		User testUser = [SELECT Id FROM User WHERE ProfileId ='00e30000001Ghun' AND IsActive = true LIMIT 1];
		List<Task> tasks = new List<Task>();

		//Create test Leads
		Lead[] testLeads = new Lead[]{};
		for(Integer i = 0;  i < 10; i++) {
			testLeads.add(new Lead(LastName = 'kktest-'+i, Company='test', Date_Requested_Sales_Follow_Up__c = system.now()));
		}
		insert testLeads;

		//Create 60 tasks associated to these leads
		for(Integer i = 0; i < 30; i++) {
			Integer j = math.mod(i, 10);
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testLeads[j].id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = newestTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testLeads[j].id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = olderTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
		}
		system.assertEquals(60, tasks.size());

		Test.startTest();
			Database.executeBatch(new TaskUpdateActivityInfoBatch());
		Test.stopTest();

		//Asserts
		List<Lead> leadsToCheck = [SELECT Id, First_Activity_Id__c, First_Activity_Date_Time__c, First_Connection_Date_Time__c, Last_Connection_Date_Time__c, Second_to_Last_Activity_Date_Time__c, Last_Activity_Date_Time__c FROM Lead WHERE Id =:testLeads];
		system.debug('leadsToCheck: ' + leadsToCheck);
		for(Lead lead : leadsToCheck) {
			//Cannot assert Id with this set up
			system.assertEquals(olderTime, lead.First_Activity_Date_Time__c);
			system.assertEquals(olderTime, lead.First_Connection_Date_Time__c);
			system.assertEquals(newestTime, lead.Last_Connection_Date_Time__c);
			system.assertEquals(olderTime, lead.Second_to_Last_Activity_Date_Time__c);
			system.assertEquals(newestTime, lead.Last_Activity_Date_Time__c);
		}
	}


The last query

List<Lead> leadsToCheck = [SELECT Id, First_Activity_Id__c, First_Activity_Date_Time__c, First_Connection_Date_Time__c, Last_Connection_Date_Time__c, Second_to_Last_Activity_Date_Time__c, Last_Activity_Date_Time__c FROM Lead WHERE Id =:testLeads];


When I observe it in the debug logs is only returning the Ids:

16:22:42:080 USER_DEBUG [74]|DEBUG|leadsToCheck: (Lead:{Id=00Q8A000001HaZ6UAK}, Lead:{Id=00Q8A000001HaZ7UAK}, Lead:{Id=00Q8A000001HaZ8UAK}, Lead:{Id=00Q8A000001HaZ9UAK}, Lead:{Id=00Q8A000001HaZAUA0}, Lead:{Id=00Q8A000001HaZBUA0}, Lead:{Id=00Q8A000001HaZCUA0}, Lead:{Id=00Q8A000001HaZDUA0}, Lead:{Id=00Q8A000001HaZEUA0}, Lead:{Id=00Q8A000001HaZFUA0})

Why are the other fields not returning and how do I fix it? 

Thanks,
Kelly

 
Paul_BoikoPaul_Boiko
I believe System.debug does not print fields with null value. Looks like the issue with your code is that it's missing inserting tasks. I see that you have a list of tasks that you populate, but I don't see where you insert them. You forgot to insert tasks before running your batch .  Add insert statement (line 22): 
insert tasks;
 and I think your code will work.
 
Kelly KKelly K

When did they change the behavior of system.debug to not show  null values? It makes troubleshooting irritating during testing/debugging. Is there an alternative method to quickly pull back all of it?

In regards to insert task -  must've deleted it when I pasted into the window above. Regardless though - there may be something else going and it's not properly spitting out an error like I would expect. Doing some prodding around a custom Apex_Errors__c object I have and it's returning someting like this:

16:48:46:770 USER_DEBUG [76]|DEBUG|apex errors: (Apex_Error__c:{Id=a2i8A00000003GMQAY, Apex_Class__c=TaskUpdateActivityInfoBatch, Error_Record__c=00T8A000002zf4AUAQ, Error_Details__c=Attempt to de-reference a null object}, ......


I'm trying to see if I can figure out exactly what it's considering null because I can extract the code and run it in execute anonymous and it does what it's supposed to.

But it's there - here's the full beast atm if you want to verify my insert statement is in there.

@isTest static void testTaskUpdateActivityInfoBatchTest() {
		DateTime olderTime = system.now().addMinutes(-5);
		DateTime newestTime = system.now().addMinutes(-3);
		//Pull a random active sales guy
		User testUser = [SELECT Id FROM User WHERE ProfileId ='00e30000001Ghun' AND IsActive = true LIMIT 1];
		List<Task> tasks = new List<Task>();

		//Create test Leads
		Lead[] testLeads = new Lead[]{};
		for(Integer i = 0;  i < 10; i++) {
			testLeads.add(new Lead(LastName = 'kktest-'+i, Company='test', Date_Requested_Sales_Follow_Up__c = system.now()));
		}
		insert testLeads;

		//Create 60 tasks associated to these leads
		for(Integer i = 0; i < 30; i++) {
			Integer j = math.mod(i, 10);
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testLeads[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = newestTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testLeads[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = olderTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
		}
		system.assertEquals(60, tasks.size());

		//Create test Accounts
		Account[] testAccounts = new Account[]{};
		for(Integer i = 0;  i < 10; i++) {
			testAccounts.add(new Account(Name = 'kktest-'+i, Status__c = 'Suspect', Specialty__c = 'Test'));
		}
		insert testAccounts;

		//Create 60 tasks associated to these accounts
		for(Integer i = 0; i < 30; i++) {
			Integer j = math.mod(i, 10);
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhatId = testAccounts[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = newestTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhatId = testAccounts[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = olderTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
		}
		system.assertEquals(120, tasks.size());

		//Create test Contacts
		Contact[] testContacts = new Contact[]{};
		for(Integer i = 0;  i < 10; i++) {
			testContacts.add(new Contact(LastName = 'kktest-'+i, AccountId = testAccounts[i].Id, Date_Requested_Sales_Follow_Up__c = system.now()));
		}
		insert testContacts;

		//Create 60 tasks associated to these contacts
		for(Integer i = 0; i < 30; i++) {
			Integer j = math.mod(i, 10);
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testContacts[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = newestTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
			tasks.add(new Task(OwnerId = testuser.Id, Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed', WhoId = testContacts[j].Id, In_Queue_for_Processing__c = true, Date_Time_Processed__c = olderTime, Prospecting_call_affirmative__c = true, Prospecting_call_connected__c = true, Email_Connection__c = true));
		}
		system.assertEquals(180, tasks.size());

		//Create 30 tasks not associated to either
		for(Integer i = 0; i < 30; i++) {
			tasks.add(new Task(Subject = 'Apex Test '+i, ActivityDate = system.today(), Status = 'Completed'));
		}
		insert tasks;
		system.assertEquals(210, tasks.size());

		List<Task> taskCheck = [SELECT Id FROM Task WHERE Id =: tasks];
		system.debug('task list size: ' + tasks.size());
		system.assertEquals(210, taskCheck.size());

		//Check Current Count of Apex Errors
		List<Apex_Error__c> apexErrorsBefore = [SELECT Id FROM Apex_Error__c];

		//210 tasks should be pushed in - verify that it's handled in batch correctly
		Test.startTest();
			Database.executeBatch(new TaskUpdateActivityInfoBatch());
		Test.stopTest();

		List<Apex_Error__c> apexErrorsAfter = [SELECT Id, Apex_Class__c, Error_Record__c, Error_Details__c FROM Apex_Error__c];
		system.debug('apex errors: ' + apexErrorsAfter);
		Integer errorCount = apexErrorsAfter.size() - apexErrorsBefore.size();
		system.assertEquals(0, errorCount);

		//Asserts
		List<Lead> leadsToCheck = [SELECT Id, First_Activity_Id__c, First_Activity_Date_Time__c, First_Connection_Date_Time__c, Last_Connection_Date_Time__c, Second_to_Last_Activity_Date_Time__c, Last_Activity_Date_Time__c FROM Lead WHERE Id =:testLeads];
		system.debug('leadsToCheck: ' + leadsToCheck);
		for(Lead lead : leadsToCheck) {
			//Cannot assert Id with this set up
			system.assertEquals(olderTime, lead.First_Activity_Date_Time__c);
			system.assertEquals(olderTime, lead.First_Connection_Date_Time__c);
			system.assertEquals(newestTime, lead.Last_Connection_Date_Time__c);
			system.assertEquals(olderTime, lead.Second_to_Last_Activity_Date_Time__c);
			system.assertEquals(newestTime, lead.Last_Activity_Date_Time__c);
		}

		//accountTasks = [SELECT Id FROM ]
		//purposely throw an error to get coverage
	}
 



 

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Dear kkorynta,

Did you check Field Level Security for those fields ?

 
Kelly KKelly K
I'm also a system admin, I have full rights to those fields. It's not an FLS issue.
Kelly KKelly K
Narrowing it down - it's something to do with the tasks created for the account.
Paul_BoikoPaul_Boiko
Add system.debug in for loop to print field values to see what values are in these fields. I think they are null, that's why they not showing up when you do system.debug('leadsToCheck: ' + leadsToCheck);
Niket SFNiket SF
write asserion to check if fields are null or not if system debug not printing those. 

 
Paul_BoikoPaul_Boiko
Did you check to see if you have exceptions thrown in debug log?
Kelly KKelly K

Found it - the error was coming out of the supporting apex class, not the batch class itself, which is why I wasn't seeing an error in the debug logs.

This was causing an error because the WhoId is null.

if(String.valueOf(task.WhoId).startsWith('003')) {
					List<Task> contactTaskList = new List<Task>();

					if(tasksOnContacts.containsKey(task.WhoId))
						contactTaskList = tasksOnContacts.get(task.WhoId);

					contactTaskList.add(task);
					tasksOnContacts.put(task.WhoId, contactTaskList);			}

				//If Lead
				else if(String.valueOf(task.WhoId).startsWith('00Q')) {
					system.debug('clear 4');
					List<Task> leadTaskList = new List<Task>();

					if(tasksOnLeads.containsKey(task.WhoId))
						leadTaskList = tasksOnLeads.get(task.WhoId);

					leadTaskList.add(task);
					tasksOnLeads.put(task.WhoId, leadTaskList);
				}

I just wrapped it with an if statement to check if it's null 1st and it looks like it's good:

if(task.WhoId != null) {
				if(String.valueOf(task.WhoId).startsWith('003')) {
					List<Task> contactTaskList = new List<Task>();

					if(tasksOnContacts.containsKey(task.WhoId))
						contactTaskList = tasksOnContacts.get(task.WhoId);

					contactTaskList.add(task);
					tasksOnContacts.put(task.WhoId, contactTaskList);			}

				//If Lead
				else if(String.valueOf(task.WhoId).startsWith('00Q')) {
					system.debug('clear 4');
					List<Task> leadTaskList = new List<Task>();

					if(tasksOnLeads.containsKey(task.WhoId))
						leadTaskList = tasksOnLeads.get(task.WhoId);

					leadTaskList.add(task);
					tasksOnLeads.put(task.WhoId, leadTaskList);
				}
			}

Thanks for the helpful line of questioning folks. Good to know system debug will no longer show me null fields. I'll spend less time troubleshooting that next time.