Schema design and modification impact on Apex development

Previously, we have learned how we can define our data model through the user interface (UI), such as creating new objects and new fields and linking them to each other by creating specific relationships.

When you define objects, fields, and relationships, they become instantly available for you to use in declarative automation features and also in Apex code. Everything defined through the UI can be used programmatically with Apex and Salesforce Object Query Language (SOQL). I'll explain this in more detail in Chapter 4, Apex Basics, under the Working with data in Salesforce section, but, for now, you need to know when objects and/or fields are used/referenced elsewhere in Salesforce.

You won't be able to delete those fields or objects, as Salesforce won't allow that, and it will come up as a big error message telling you where the field or object you are trying to delete is referenced in your code (like in the following screenshot):

This is a good thing, because you could easily delete a field and the existing logic that is in your production org would break if you didn't pay attention.

So, if you want to delete or modify a field or object that is used in the code or elsewhere in Salesforce, you need to follow these steps:

  1. You need to remove the reference.
  2. Then, delete the field or object.
  3. Finally, refactor your code or formulas so that everything can continue to work.