Long ago, I added a field
Contact on our production org. Now, I'm using SFDX to develop an unlocked package with code that refers to that custom field, like
someContact.Type__c = 'Student'.
With a newly-created scratch org, using
force:source:push on this package gives the error
Variable does not exist: Type__c
because the custom field hasn't been defined in the scratch org. That makes sense, and I can fix it by retrieving the metadata for
Contact from production and using
force:mdapi:deploy to deploy that metadata into the scratch org. Then
force:source:push works and my tests run successfully.
The problem comes when I try to create a package version. When I use
force:package:version:create I get the
Variable does not exist: Type__c error again. The field is defined in both production and the scratch org, so I don't understand "where" the package version is being created such that it doesn't see that field.
Accessing a custom field from an SFDX-based package seems like a pretty common use case, so I have a feeling I'm misunderstanding something basic. Can anyone help me figure out what I'm doing wrong, and offer an approach that works? Thanks very much in advance for your help.