Assigned nested query results to a List. Passed list to another method and see error: "SObject row was retrieved via SOQL without querying the requested field"
Please provide the query and someone can identify the issue. But basically you would get this error when trying to access a field in your method that you didn't query for in the nested query. This is a easy fix, just add the missing field to your nested query.
List<Transaction__c> transactionsToUpdate = [ SELECT Id,
Dentists__c,
(SELECT Dentist__c,
Role__c
FROM Transaction_Owner_Dentist__r
ORDER BY CreatedDate ASC)
FROM Transaction__c
WHERE Id
IN :transactionIds
];
if( transactionsToUpdate.size() > 0 ) {
TransactionTriggerHelper.updateDentistsOnTransactions( transactionsToUpdate );
That's the code that generates the list. The error occurs in the next method the list is passed to...
public static void updateDentistsOnTransactions( List<Transaction__c> transactions )
{
Set<Id> relatedContacts = new Set<Id>();
Map<Id, String> contactIdToName = new Map<Id, String>();
//get a list of all contact Ids from the children transaction/dentist objects
for ( Transaction__c t : transactions ) {
for(Transaction_Dentist__c c: t.Transaction_Owner_Dentist__r) {
if(c.Role__c == 'Purchaser') {
relatedContacts.add(c.Dentist__c);
}
Here's the error: Error:Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Transaction_Dentist__c.Role__c: Class.TransactionTriggerHelper.updateDentistsOnTransactions: line 21, column 1
Hi James, I assume this code is running in the Transaction trigger right? If so we may be looking in the wrong place for the issue, does this code then call an update on the related contacts later in this transaction? Because the error you shared is from in the ContactTrigger.
Gary
Here's the error: Error:Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Transaction_Dentist__c.Role__c: Class.TransactionTriggerHelper.updateDentistsOnTransactions: line 21, column 1
That looks right to me, is there any more code in that method that tries to reference a field on the child records?
Gary
I assume this code is running in the Transaction trigger right?
If so we may be looking in the wrong place for the issue, does this code then call an update on the related contacts later in this transaction? Because the error you shared is from in the ContactTrigger.
Gary