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
James SnavelyJames Snavely 

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"

firechimpfirechimp
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.

Gary
James SnavelyJames Snavely
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 );

James SnavelyJames Snavely
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);
                }


James SnavelyJames Snavely
Line 21 is if(c.Role__c == 'Purchaser')

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
James SnavelyJames Snavely
Sorry for the multiple comments and poorly written code. I'm fairly new to programming, no comp. sci. background at all!
firechimpfirechimp
Hey James,
That looks right to me, is there any more code in that method that tries to reference a field on the child records?

Gary
James SnavelyJames Snavely
That's it, the rest of the method's actions are related to manipulating the parent object... I am confused.
firechimpfirechimp
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