Back to LinkedIn posts

LinkedIn post 359

SCENARIO:

SCENARIO:

๐Ÿ˜‡ Suppose you're working on a Salesforce trigger - let's say on Case - and in it you create and insert a Note record related to the case.

After testing and verifying that the Case trigger is working properly, you go work on some unrelated Invocable Apex to make API requests and display results in a flow.

๐Ÿ˜ฑ Suddenly the Case trigger stops working and prevents Email-to-Case from working! The error is on the line that populates the Body of the new Note.

"Variable does not exist: Body"

Can you think of what would cause this?

๐Ÿšจ Stop here and try to guess before I tell you what was the cause and some solutions.
๐Ÿ˜

=======================================

=======================================

๐Ÿคฏ The reason was that the API returned a data structure that contained an object named "Note". So a "Note" class was created to store it and make it available to the flow with the annotations @AuraEnabled @InvocableVariable.

In doing so, it interfered with the Case trigger that was referencing the standard object Note. When the trigger executed, it instantiated the Apex class "Note" instead of the standard object Note. Then it attempted to populate the field "Body" in the Note class, which didn't exist, hence the error "Variable does not exist: Body".

๐Ÿ› ๏ธ There are at least 2 solutions for this:
1) replace all "Note" references in the trigger class with "Schema.Note"
2) rename the class "Note" to something else (I've added a prefix)

๐Ÿ‘‰๐Ÿผ So when creating an Apex class remember to not use the same name as a standard object.

Usually we already know to not create a class with the obvious sObject name "Account" (Salesforce won't prevent you and it will potentially wreck the entire code).
However, some other names may be more difficult to remember to avoid.

For example: Solution, History, Login, Approval, Domain, Site, Audience, BuyerGroup, Calendar, CallCenter, Coupon, CreditMemo, CustomBrand, Customer, Image, Incident, LegalEntity, Period, Problem, Promotion, Prompt, Publisher, Refund, Seller, Shipment, TimeSlot, Vote, Waitlist.

I think I saw someone else posting about the same situation a few months ago but I couldn't locate it.