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
Jason Kuzmak 12Jason Kuzmak 12 

Why am I able to save a class that references non-existent field "customObject.Notes"?

This is a screenshot of a saved apex class. 

User-added image

The bare bones essentials: 

public without sharing class WebOrderMethods {

    public static Map<String,String> webOrderMap = new Map<String,String>();
    public static List<Map<String,String>> lineItemListMap = new List<Map<String,String>>();
    
    public static Web_Order__c buildWebOrder() {
        Web_Order__c thisOrder = new Web_Order__c();

        if(webOrderMap.size() > 0){
            for(String key : webOrderMap.keySet()){
                String value = webOrderMap.get(key);
                
                if(key == null || value == null){
                    continue;
                }
    
                switch on key {
                    when 'Email' {
                        thisOrder.Contact_Email__c = value;
                    }
                    when 'Notes' {
                        if(thisOrder.Notes == null){
                            thisOrder.Notes__c = value;
                        }
                        else{
                            thisOrder.Notes__c += '\r\n'+value;
                        }
                    }
                    when 'ShipmentMethod' {
                        thisOrder.Shipment_Method__c = value;
                    }
                }
            }
        }

        return thisOrder;
    }
}
 



I found an issue with my code in which I was referencing a custom field improperly. Here I'm iterating through a list of my custom object records "Web_Order__c", and I noticed I forgot to include the "__c" affix. 

Just to test, I tried to query this field thru Anon Apex and got the expected error:
User-added image

Why did this save properly if there's no such field? 

 

Best Answer chosen by Jason Kuzmak 12
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The resaon it saved is because you can have notes related list on the object so the code is reffering to that when saving. If you give some other variable it will not allow to save it.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The resaon it saved is because you can have notes related list on the object so the code is reffering to that when saving. If you give some other variable it will not allow to save it.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Jason Kuzmak 12Jason Kuzmak 12
@sai praveen
Ok thanks! That makes perfect sense; feels obvious in hindsight!