Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller

Be First!

System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller

In your  StandardController extension, we were using the StandardController.getRecord() method to fetch the record supplied by the Standard Controller

Consider in the following apex class we were used to overcome the following issue:

the error “SObject row was retrieved via SOQL without querying the requested field” can be thrown

so in this class to overcome above error we can add the fields (addFields) in the standard controller extension

public ContactExt(ApexPages.StandardController sc) {
        sc.addFields(new List<String> {'Account.BillingCity'});
        record = (Contact)sc.getRecord();
}

but while executing the Apex Test Method, its throwing the error like:
System.SObjectException: You cannot call addFields when the data is being passed into the 
controller by the caller.

so the solution, simply add the if condition isRunningTest like
public ContactExt(ApexPages.StandardController sc) {
        if (!Test.isRunningTest()) { 
            sc.addFields(new List<String> {'Account.BillingCity'});
        }           
        record = (Contact)sc.getRecord();
}

(2723)

Previous
Next

Leave a Reply