To reproduce this behavior in your Salesforce organization:
The trigger runs in the before insert context, which means it executes before the Case record is committed to the database. At this point, Salesforce has identified the matching Contact (hence ContactId is populated), but has not yet finalized the relationship between the Case, Contact, and Account.
The debug logs show that the AccountId field is null in the before insert context, even though the ContactId is populated. For example:
The Case shows: Case Id = null (pre-insert), ContactId = 003... (populated), AccountId = null (not yet resolved).
This occurs because Salesforce performs internal post-processing to associate the Account after the record is committed — not during the before insert phase.
When a Case is created through Salesforce Email-to-Case, developers may notice that the AccountId field is null in a before insert trigger, even when the ContactId is populated. This is expected behavior in Salesforce.
Salesforce performs internal post-processing to associate the Case with the Account from the related Contact. This post-processing occurs after the Case record has been committed to the database — that is, during the after insert phase. The AccountId field is not available during the before insert trigger because the Salesforce platform has not yet finalized the relationship between the Case, the Contact, and the Account at that execution point.
If you need to access the AccountId in trigger logic for Email-to-Case cases, use an after insert trigger instead of a before insert trigger. In the after insert context, the platform has completed post-processing and the AccountId field is populated.
Example approach:
005132457

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.