7

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.

@istest
public class Testclasstoinsertattachemnt{


    static Account acc;
    static Contact con;
    static User newUser;
    static Profile p;

    Static testmethod void firstTestMethod()
    {
        loadata();
        System.runAs(new User(Id = newUser.id))
        {

         RecordType rt = [select id,Name from RecordType where SobjectType='Case' and Name='General Customer Service' Limit 1];   

         Case cse=new Case();
         cse.RecordTypeId=rt.id;
         cse.AccountId=acc.id;
         cse.ContactId=con.id;
         insert cse;

         System.assertNotEquals(null, cse.id);

       ***Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentid=cse.Id;
        insert attach;***


        }

    }

    static void loadata()
    {
        insertAccount();
        insertcontact();
        insertUser();
    }

1 Answer 1

11

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.
1
  • thank you for the clue (#2) - solving what had been an ongoing mystery after we added a managed package to our org
    – cropredy
    Dec 9, 2017 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .