You need to sign in to do that
Don't have an account?
Jayesh 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;
}
{
//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;
}
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:
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