SFDX - Have System Fields Gone Missing?

When developing using an SFDX Package Development Model there may be situations where valid code generates an error. We’ll take one small Apex/SOQL example today and use a simple workaround to get this code into the package.

Take this basic SOQL query

List<Contact> contacts = [
    SELECT FirstName, MiddleName, LastName, Suffix
    FROM Contact
];

Looks harmless enough. It will deploy to a Scratch Org and fetch the expected records when executed. Your functional code will work and unit tests will pass. But when it’s time to build a package for deployment to your test Sandbox, here’s what happens:

$ sfdx force:package:version:create -p MyProject MyDevHub

ERROR at Row:2:Column:22
No such column 'MiddleName' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c'
after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

ERROR at Row:2:Column:22
No such column 'Suffix' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c'
after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Since MiddleName & Suffix are already enabled fields in both my Scratch Org and Dev Hub, it seems like the only way to get this package built is to bypass the compile time check on this SOQL query. Let’s try the Dynamic SOQL approach and format the query as a String, then call Database.query() to execute the query.

List<Contact> contacts = Database.query('SELECT FirstName, MiddleName, LastName, Suffix FROM Contact');

Now we can successfully build this query into a package. This odd bug is presumably related to a complex history of these fields. They belong to both Contact object as well as Person Accounts, and at one time required contacting Salesforce support in order to enable them in your org.

A final note, It’s always advisable to use inline SOQL for added compile checking and protection against SOQL injection. This is an example where implementing Dynamic SOQL could not be avoided.

John Rountree Senior Technical Consultant