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
Jayesh Deo 2Jayesh Deo 2 

Error Encountered: system.SObjectException: SObject row was retrieved via SOQL without querying the requested field

If (trigger.isAfter && trigger.isUpdate)
    {
        //Get the related contact updated based on updated account details.
        List<Contact> lstcontact=[Select id, firstName, LastName, Fax, Phone, MailingCity, Mailingstate, MailingCountry,MailingPostalcode,MailingStreet
                                                 From Contact
                                                  Where Accountid in:Trigger.newMap.KeySet()];
        
        for(contact con:lstcontact)
        {
            Account acc= Trigger.newMap.get(con.AccountId);
            con.Phone=acc.Phone;
            con.fax=acc.Fax;
            con.MailingCity=acc.BillingCity;
            con.MailingStreet=acc.BillingStreet;
            con.MailingPostalCode=acc.BillingPostalCode;
            con.MailingCountry=acc.BillingCountry;
            
            
        }
        update lstcontact;
        
    }
Best Answer chosen by Jayesh Deo 2
SwethaSwetha (Salesforce Developers) 
HI Jayesh,

The error "SObject row was retrieved via SOQL without querying the requested field" occurs when a query retrieves an SObject row without querying for a specific field that is later referenced in the code.

In your provided code, you are querying the Contact object and then attempting to access fields from related Account records. If you want to access fields from the related Account records, you need to include those fields in your initial SOQL query. For example:
 
List<Contact> lstcontact = [
    SELECT Id, FirstName, LastName, Fax, Phone, MailingCity, MailingState, MailingCountry, MailingPostalCode, MailingStreet, Account.Phone, Account.Fax, Account.BillingCity, Account.BillingStreet, Account.BillingPostalCode, Account.BillingCountry
    FROM Contact
    WHERE AccountId IN :Trigger.newMap.keySet()
];

Related: https://developer.salesforce.com/forums/?id=906F00000008vfYIAQ
https://help.salesforce.com/s/articleView?id=000385722&type=1
https://salesforce.stackexchange.com/questions/367971/why-i-am-getting-error-system-sobjectexception-sobject-row-was-retrieved-via
https://www.sfdcpoint.com/salesforce/sobject-row-was-retrieved-via-soql-without-querying-the-requested-field/

If this information helps, please mark the answer as best. Thank you