You need to sign in to do that
Don't have an account?
IanM1963
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
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.
Thanks for the help everyone. :-)
All Answers
Hi,
Change your code from
to
and you should be fine.
Add a debug (see below) and see if the task.WhatId contains a value. Maybe this will provide a clue.
Thanks, now changed it to this as you suggested:
And it won't let me deploy to the production org despite having test coverage! :-(
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.
Thanks for the help everyone. :-)