I am building a typical help site where there are a lot of unauthenticated pages that the user can view like product catalog, help guides ...etc and couple of authenticated pages (My Cases, Case View). The Case View page allows the user to view the latest email on the case and provides a textarea where they can enter any information. Due to an existing design, whatever is entered should be stored in the EmailMessage object linked to the Case.
When I insert a new record into EmailMessage object, I get an error saying Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
Interestingly, I have an unauthenticated page that allows anyone browsing the website to create a case and that piece of code also inserts into EmailMessage object. The unauthenticated page inserts without any errors. So I couldn't help but wonder why an authenticated page gets the insufficient access error. Any insights would help. Here is a much trimmed down version of the page and class where the error is replicable.
I have checked the usual suspects - Object access, FLS, Page access. I am able to update the case but it is the EmailMessage that am not able to insert.
public class PortalCaseView {
public PortalCaseView(){ }
public void insertMsg(){
EmailMessage msg = new EmailMessage(
FromAddress = 'aaa@bbb.com',
FromName = 'Stackoverflow',
Subject = 'Problem',
TextBody = 'Description about the problem',
ParentId = '500e00000036va1',
Incoming = true,
Status = '0'
);
insert msg;
}
}
VF Page
<apex:page controller="PortalCaseView">
<apex:form >
<apex:commandButton action="{!insertMsg}" value="Insert" />
</apex:form>
</apex:page>