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
Mark Thomas 49Mark Thomas 49 

Error when updating Apex Class to include a new field

I'm getting the following error when I run a debug log on an update to an APEX Class, FATAL_ERROR|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Account_Owner_For_Jiva__c
The field is a formula text field that brings in the account owners email address. We use this to pass the email to our external system. I added the field the existing APEX Class like this:

 //json container for Account
    private class AccountCompany{
        private Id SalesforceID; //required
        private String Name; //required
        private String AccountOwner; //required
        private String PrimaryCountry; //required
        //private String EAGRelationshipID;
        private String UnicodeName;
        private String IndustrySectorID;
        private String ManufacturingTypeID;
       

        public AccountCompany(Sobject record)
        {
            Account account = (Account)record;

            this.SalesforceID = account.Id;
            this.Name = account.Name;
            this.PrimaryCountry = account.Primary_Country__c;
            this.AccountOwner = account.Account_Owner_For_Jiva__c;
            //this.EAGRelationshipID = EAGRelationshipID; //no related field in salesforce
            this.UnicodeName = account.Account_Name_Unicode__c;
            this.IndustrySectorID = account.Industry_Sector_ID__c;
            this.ManufacturingTypeID = account.Manufacturing_Type_ID__c;
            
        }
    }


The updated Class saves with no errors, but was not communicating correctly which is when I ran the code debug log. I saw where I need to add it to the query, but I don't see anything else in the class that includes the existing fields.

 

VinayVinay (Salesforce Developers) 
Hi Mark,

Kindly review below mentioned workaround to fix 'SObject row retrieved via SOQL without querying the requested field' error.

https://help.salesforce.com/articleView?id=000328927&language=en_US&type=1&mode=1

Thanks,
Vinay Kumar
SUCHARITA MONDALSUCHARITA MONDAL
Hi Mark,

Please check your SOQL, you may not querying the field (Account_Owner_For_Jiva__c) and trying to access that in your code.  

Hope this helps!

Thanks,
Sucharita
Mark Thomas 49Mark Thomas 49
Thanks for the input. I've been playing around with some things, but nothing has worked. This is a look at a section of the Apex Class that needs the new field. I don't see anywhere else in the class that queries the list of fields.

    //retrieves list of fields for the specified object type that 
    //should be included in the web request
    private static String retrieveObjectFields(String objectType)//, Map<String, String> sforceToWebFieldMap)
    {
        String selectFields = '';

        //TO DO: WILL REMOVE Webservice_Parameter_Name__c field before deploy
        JIVA_Field__mdt [] jivaFieldsForObject = [Select MasterLabel, isStandardField__c /*, Webservice_Parameter_Name__c*/
                                                    From JIVA_Field__mdt Where Object_API_Name__c =: objectType]; 

        for(JIVA_Field__mdt field: jivaFieldsForObject)
        {
            String fieldAPIName = field.isStandardField__c ? field.MasterLabel : field.MasterLabel + '__c';

            if(field.MasterLabel != 'Id')
            {
                selectFields += ', ' + fieldAPIName;
            }

            //sforceToWebFieldMap.put(fieldAPIName, field.Webservice_Parameter_Name__c);
        }

        return selectFields;
    }

    private static Boolean runningInASandbox() {
        return [SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox;
    }

    //json container for Account
    private class AccountCompany{
        private Id SalesforceID; //required
        private String Name; //required
        private String AccountOwner; //required
        private String PrimaryCountry; //required
        //private String EAGRelationshipID;
        private String UnicodeName;
        private String IndustrySectorID;
        private String ManufacturingTypeID;
       

        public AccountCompany(Sobject record)
        {
            Account account = (Account)record;

            this.SalesforceID = account.Id;
            this.Name = account.Name;
            this.PrimaryCountry = account.Primary_Country__c;
            this.AccountOwner = account.Account_Owner_For_Jiva__c;
            //this.EAGRelationshipID = EAGRelationshipID; //no related field in salesforce
            this.UnicodeName = account.Account_Name_Unicode__c;
            this.IndustrySectorID = account.Industry_Sector_ID__c;
            this.ManufacturingTypeID = account.Manufacturing_Type_ID__c;