INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference

  • While inserting attachment I am getting this error :

    System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

    I double Checked my user is active and case is inserted properly.

    public class Testclasstoinsertattachemnt{
        static Account acc;
        static Contact con;
        static User newUser;
        static Profile p;
        Static testmethod void firstTestMethod()
            System.runAs(new User(Id =
             RecordType rt = [select id,Name from RecordType where SobjectType='Case' and Name='General Customer Service' Limit 1];   
             Case cse=new Case();
             insert cse;
           ***Attachment attach=new Attachment();       
            attach.Name='Unit Test Attachment';
            Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
            insert attach;***
        static void loadata()
  • Two possible scenarios:

    1. The user do not have access to case ( That would be possible if the user profile does not have read access to case, even if user owns the record).
    2. There is a assignment on case that changes the owner of the case upon insertion. Then you have to explicitly set the owner of the attachment to be the new owner of the case.

    thank you for the clue (#2) - solving what had been an ongoing mystery after we added a managed package to our org

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used