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
xNamelessonexxNamelessonex 

Email Service Class, works under sandbox environment, generates INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY in Production

I have the following Class which was created under a sandbox environment, following the tutorial from this guide https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com:
 
global  class CaseCommentReply implements Messaging.InboundEmailHandler {

    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                           Messaging.Inboundenvelope envelope )
        
    {
       
    
      Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();  
        
        try
    {
        
        
        
        Id csId = Cases.getCaseIdFromEmailThreadId(email.subject.substringBetween('ref:', ':ref'));
        
        System.debug('CaseId Value'+ csId);
        
        if (csId != null)
        {
            
            String TextStrip = email.plainTextBody;
            
            
            
           CaseComment comment = new CaseComment(ParentId = csId,CommentBody = TextStrip.substringBefore('--Reply above this line--'),isPublished = true); 
            
            insert comment; 
            
            System.debug('The comment of the case with the Id '+comment.ParentId+ 'has been updated \n The text: \n'+comment.CommentBody);
            result.success = true; 
            
            
        }
        
    }   
      catch(Exception e)
      {
          System.debug('An exception ocurred while attempting to create a comment from Email  '+e);
           System.debug('There were errors while processing the function');
          result.success = false; 
          
      }
        
        
        if (result.success == true)
        {
        System.debug('The function has processed correctly');
        }
         
            
         
        return result; 
        
    }
    
  
    
    
    
    
    
}

And the Test Class:
 
@isTest
public class CaseCommentReplyTest {

    
   
    static testMethod void TestEmailToComment()
    {    
    
        
        Case testCase = new Case();
         insert testCase; 
        String caseId = testCase.Id;
        System.debug('Value of the case id'+caseId);
       
        
        string Case_ThreadID = '_'  + UserInfo.getOrganizationId().left(4)  + '0'  + UserInfo.getOrganizationId().mid(11,4) + '._'  + caseId.left(4) + '0'  + caseId.mid(10,5);
        
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        email.plainTextBody = 'Test';
        email.subject = 'Re: Sandbox: Case. [ ref:'+Case_ThreadID+':ref ]'; 
        
        Messaging.InboundEmail exceptionEmail = new Messaging.InboundEmail();
        
        
        
       Test.startTest();
       CaseCommentReply crep = new CaseCommentReply();
        
        crep.handleInboundEmail(email,env); 
        crep.handleInboundEmail(exceptionEmail, env);
        
    Test.stopTest();
    
    }
    
}

When I created the Email Service, the user for the Context User, under the Email address associated to the Email Service is a Super User, with full priviledges on the organization. It is the same user as the Email Service from the Sandbox. And the configuration of the Email service is the same. Is there any configuration I might be missing under the production environment which triggers the Error:

"INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY" When Inserting the Comment into Salesforce? 

 
xNamelessonexxNamelessonex
I have made a comparison with the Unit tests in production. Despite it being the exact same code, the unit test in production claims there is no code coverage for the line 30 from the CaseCommentReply class which I copied above. Is there any particular set of restrictions which differ from a sandbox environment, that could be unrelated to the User, which is exactly the same on both instances? 
xNamelessonexxNamelessonex
I have tested it again with a recently updated (just today) Sandbox and the error now seems to be primarily on the way the ThreadId Is generated.

This is the line of code used to generate said ThreadId: 
 
string Case_ThreadID = '_'  + UserInfo.getOrganizationId().left(4)  + '0'  + UserInfo.getOrganizationId().mid(11,4) + '._'  + caseId.left(4) + '0'  + caseId.mid(10,5);

I do wonder will our BELOVED </sarcasm> overlords ever give us an official way to get the ThreadId from apex?