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
IanM1963IanM1963 

Trouble with WhatId in SOQL query within Apex trigger.

Hi,

 

Can anyone help me with this simple query?

 

When I hard code the WhatId it works perfectly - here's the code...

 

Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id =  limit 1];

 However, I want it to be (unsurprisingly) dynamic. I thought something like this should work:

 

for(Task task : trigger.new){
		
		


		//Id wId = task.WhatId;
		Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id = task.WhatId  limit 1];


		task.Primary_Service__c = acc.Primary_Service__r.Name;
			task.Primary_Service_ID__c = acc.Primary_Service__r.Id;
			task.Source_of_Data__c = acc.Source_of_Data__c;
			task.Reason_for_Contact__c = acc.Reason_for_Contact__c;
			task.People_Status__c = acc.People_Status__c;
			task.Street__c = acc.PersonMailingStreet;
			task.City__c = acc.PersonMailingCity;
			task.State__c  = acc.PersonMailingState;
			task.PostCode__c = acc.PersonMailingPostalCode;
			task.Country__c = acc.PersonMailingCountry;
			task.Mobile__c = acc.PersonMobilePhone;
			task.Home_Phone__c = acc.PersonHomePhone;
			task.Gender__c = acc.Gender__c;
			task.Age__c = acc.Age__c;
			task.DOB_Estimated__c = acc.DOB_Estimated__c;
			task.Marital_Status__c = acc.Marital_Status__c;
			task.Commitment_Type__c = acc.Commitment_Type__c;
			task.Commitment_Date__c = acc.Commitment_Date__c;

	}

 Sorry, I left the hardcoded Id out of the first example but I'm sure you get the idea.

 

Why does a hard coded Id work and a bind variable not work?

 

Thanks in anticipation.

 

Ian

Best Answer chosen by Admin (Salesforce Developers) 
IanM1963IanM1963

Not entirely sure what happened here. When I added debugs the WhatId was coming up blank. I decided to simlify things by addressing the Trigger.new[] collection directly and taking any for loops out because I'm always only updating one thing at a time. When I did this, the WhatId was there! This allowed me to run the query to retrieve Account information to load into the custom fields on the task. Works perfectly so thought I'd post it as the solution.

 

trigger taskUpdateFieldsFromAccount on Task (before insert) 
{
	Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id =: Trigger.new[0].WhatId limit 1];
	Trigger.new[0].Primary_Service__c = acc.Primary_Service__r.Name;
	Trigger.new[0].Primary_Service_ID__c = acc.Primary_Service__r.Id;
	Trigger.new[0].Source_of_Data__c = acc.Source_of_Data__c;
	Trigger.new[0].Reason_for_Contact__c = acc.Reason_for_Contact__c;
	Trigger.new[0].People_Status__c = acc.People_Status__c;
	Trigger.new[0].Street__c = acc.PersonMailingStreet;
	Trigger.new[0].City__c = acc.PersonMailingCity;
	Trigger.new[0].State__c  = acc.PersonMailingState;
	Trigger.new[0].PostCode__c = acc.PersonMailingPostalCode;
	Trigger.new[0].Country__c = acc.PersonMailingCountry;
	Trigger.new[0].Mobile__c = acc.PersonMobilePhone;
	Trigger.new[0].Home_Phone__c = acc.PersonHomePhone;
	Trigger.new[0].Gender__c = acc.Gender__c;
	Trigger.new[0].Age__c = acc.Age__c;
	Trigger.new[0].DOB_Estimated__c = acc.DOB_Estimated__c;
	Trigger.new[0].Marital_Status__c = acc.Marital_Status__c;
	Trigger.new[0].Commitment_Type__c = acc.Commitment_Type__c;
	Trigger.new[0].Commitment_Date__c = acc.Commitment_Date__c;
}

 Thanks for the help everyone. :-)

All Answers

Richie DRichie D

Hi,

 

Change your code from

Account where Id = task.WhatId

 

  to

Account where Id =: task.WhatId

 

 and you should be fine.

sharrissharris

Add a debug (see below) and see if the task.WhatId contains a value. Maybe this will provide a clue.

 

for(Task task : trigger.new){

	//Id wId = task.WhatId;
	Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id = task.WhatId  limit 1];

System.debug(task.WhatId);

	task.Primary_Service__c = acc.Primary_Service__r.Name;
	task.Primary_Service_ID__c = acc.Primary_Service__r.Id;
	task.Source_of_Data__c = acc.Source_of_Data__c;
	task.Reason_for_Contact__c = acc.Reason_for_Contact__c;
	task.People_Status__c = acc.People_Status__c;
	task.Street__c = acc.PersonMailingStreet;
	task.City__c = acc.PersonMailingCity;
	task.State__c  = acc.PersonMailingState;
	task.PostCode__c = acc.PersonMailingPostalCode;
	task.Country__c = acc.PersonMailingCountry;
	task.Mobile__c = acc.PersonMobilePhone;
	task.Home_Phone__c = acc.PersonHomePhone;
	task.Gender__c = acc.Gender__c;
	task.Age__c = acc.Age__c;
	task.DOB_Estimated__c = acc.DOB_Estimated__c;
	task.Marital_Status__c = acc.Marital_Status__c;
	task.Commitment_Type__c = acc.Commitment_Type__c;
	task.Commitment_Date__c = acc.Commitment_Date__c;

}

 

IanM1963IanM1963

Thanks, now changed it to this as you suggested:

 

Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id =: task.WhatId limit 1];

 

And it won't let me deploy to the production org despite having test coverage! :-(

 


 

 

IanM1963IanM1963

Not entirely sure what happened here. When I added debugs the WhatId was coming up blank. I decided to simlify things by addressing the Trigger.new[] collection directly and taking any for loops out because I'm always only updating one thing at a time. When I did this, the WhatId was there! This allowed me to run the query to retrieve Account information to load into the custom fields on the task. Works perfectly so thought I'd post it as the solution.

 

trigger taskUpdateFieldsFromAccount on Task (before insert) 
{
	Account acc =  [Select Id, PersonContactId, IsPersonAccount, Primary_Service__r.Name, Primary_Service__r.Id, Source_of_Data__c, Reason_for_Contact__c, People_Status__c, PersonMailingStreet, PersonMailingCity, PersonMailingState , PersonMailingPostalCode, PersonMailingCountry, PersonMobilePhone, PersonHomePhone, Gender__c, Age__c, DOB_Estimated__c, Marital_Status__c, Commitment_Type__c, Commitment_Date__c From Account where Id =: Trigger.new[0].WhatId limit 1];
	Trigger.new[0].Primary_Service__c = acc.Primary_Service__r.Name;
	Trigger.new[0].Primary_Service_ID__c = acc.Primary_Service__r.Id;
	Trigger.new[0].Source_of_Data__c = acc.Source_of_Data__c;
	Trigger.new[0].Reason_for_Contact__c = acc.Reason_for_Contact__c;
	Trigger.new[0].People_Status__c = acc.People_Status__c;
	Trigger.new[0].Street__c = acc.PersonMailingStreet;
	Trigger.new[0].City__c = acc.PersonMailingCity;
	Trigger.new[0].State__c  = acc.PersonMailingState;
	Trigger.new[0].PostCode__c = acc.PersonMailingPostalCode;
	Trigger.new[0].Country__c = acc.PersonMailingCountry;
	Trigger.new[0].Mobile__c = acc.PersonMobilePhone;
	Trigger.new[0].Home_Phone__c = acc.PersonHomePhone;
	Trigger.new[0].Gender__c = acc.Gender__c;
	Trigger.new[0].Age__c = acc.Age__c;
	Trigger.new[0].DOB_Estimated__c = acc.DOB_Estimated__c;
	Trigger.new[0].Marital_Status__c = acc.Marital_Status__c;
	Trigger.new[0].Commitment_Type__c = acc.Commitment_Type__c;
	Trigger.new[0].Commitment_Date__c = acc.Commitment_Date__c;
}

 Thanks for the help everyone. :-)

This was selected as the best answer