When building public-facing functionality in Salesforce, it’s common to send an automated email as soon as a record is created — especially through a form on a Salesforce Experience Cloud (Community) site. However, if this record is created by a Guest User, and the email uses a Visualforce email template, unexpected errors can occur. One such error is:
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_ID
This blog covers a common but tricky issue — how to fix the “Invalid Cross Reference Id” error when sending a Visualforce email from a Guest User in Salesforce. We’ll walk through the scenario, the root cause, and the exact fix using the renderUsingSystemContextWithoutSharing attribute.
The Scenario: Sending Email from a Guest User in Salesforce
We wanted to send a Visualforce-based email as soon as a custom object record was created by a Salesforce Guest User through a public website form.
Our setup included:
- A Visualforce email template
- The email was triggered from Apex when a record was inserted
- The record was created by a Guest User
- All necessary access (Org-Wide Email Address, template, objects) appeared to be correct
At first glance, this appeared to be a basic Salesforce automation task.
The Problem: “Invalid Cross Reference Id” Error
When the record was inserted, the Apex code attempted to send the email using the Visualforce template. However, it failed and threw the following error:
INVALID_CROSS_REFERENCE_ID
This error usually points to a permissions or access issue — such as a user trying to reference a record or object they don’t have access to.
Since the Guest User has limited access in Salesforce by design, our first assumption was that it could be related to:
- Contact access
- Custom object permissions
- Visualforce template permissions
- Missing Org-Wide Email Address
After double-checking all these areas and confirming everything was configured correctly, the error still persisted.
Root Cause: Visualforce Email Rendering in User Context
By default, Salesforce Visualforce email templates render using the current user’s context. In this case, the user is the Guest User, who lacks access to most objects and fields.
When the template attempts to merge data or render content related to objects the Guest User cannot access, it causes a silent failure — ultimately resulting in the Invalid Cross Reference Id error.
The solution? Instruct Salesforce to render the email in system context, bypassing the Guest User’s limited access.
The Fix: Use renderUsingSystemContextWithoutSharing in Your Email Template
Salesforce provides an attribute called renderUsingSystemContextWithoutSharing in the <messaging:emailTemplate> component.
What it does:
- It tells Salesforce to render the email content using system-level access.
- It ignores sharing rules and field-level permissions that would normally apply to the user.
- This is essential when sending emails from Guest Users or restricted users where field visibility is an issue.
Updated Visualforce Email Template Code
<messaging:emailTemplate subject=”New Record Created”
recipientType=”Contact”
relatedToType=”CustomObject__c”
renderUsingSystemContextWithoutSharing=”true”>
<!– Your Visualforce content goes here –>
</messaging:emailTemplate>
Once this attribute was added, the error disappeared. The email sent successfully, and the correct template content was rendered — even though the action was triggered by a Guest User.
Why This Fix Works in Salesforce
This fix works because renderUsingSystemContextWithoutSharing forces Salesforce to render the email content outside the Guest User’s restricted context. It is especially helpful when the template:
- Uses merge fields from related objects
- References custom objects or fields not visible to the Guest User
- Runs in public-facing sites, such as Salesforce Communities or Experience Cloud
Key Points to Remember
Here’s what every Salesforce developer or admin should know when using Visualforce emails with Guest Users:
Issue | Resolution |
Sending email from Guest User | Ensure the Guest User has the right object/field access (or use system context) |
Visualforce template fails silently | Add renderUsingSystemContextWithoutSharing=”true” |
“Invalid Cross Reference Id” error | Likely due to object or record access problems in user context |
Visualforce merge fields not populating | Caused by restricted access in Guest User context |
Best Practices When Using Salesforce Visualforce Email Templates
- Always test as the actual user who will trigger the automation (e.g., Guest User).
- Use System Context rendering when referencing objects or fields outside Guest User access.
- Keep templates modular and avoid unnecessary references to inaccessible data.
- Log or debug the email rendering process during Apex testing.
- Use standard email templates if Visualforce isn’t essential — they often avoid this problem entirely.
Conclusion
Sending a Salesforce Visualforce email from a Guest User isn’t as simple as it looks. While the setup may appear correct, the rendering context of the template often causes hidden issues. The Invalid Cross Reference Id error is a common symptom of this.
Adding renderUsingSystemContextWithoutSharing=”true” to your Visualforce email template is the key to ensuring that Salesforce renders it with the necessary system-level permissions.
Need Help With Salesforce Guest User Issues?
If you’re running into permission errors, email delivery failures, or template rendering issues in Salesforce Experience Cloud, our team specializes in solving complex Salesforce automation problems — including Guest User flows, Apex email triggers, and Visualforce development.
Let us iBirds help streamline your Salesforce automation the right way.