2

I'm a new developer who's never done this before.
There is a test class with 100% code coverage in the sandbox org.
I put test class and original class in the sandbox outbound change set and validated it, but an error related to access rights occurred.
No matter how many times I change the settings and change the test class, the same error occurs.
Is there anyone who knows?

This is my test class


@isTest
  public class BB_GetMissionObjectControllerTest {


    @isTest
      static void getMissionDelayRecordTest() {

        Id userId = UserInfo.getUserId();
        Date myDate = Date.newInstance(2023, 1, 27);

        BB_Mission__c m = new BB_Mission__c();
        m.OwnerId = '0055j000000XmeWAAS';
        m.Name = '가게 경쟁력 강화 타겟 영업 - 0000101010 - System Admin - 2023-1-27';
        m.BB_MissionType__c = '업주 순증';
        m.BB_CreatedDate__c = myDate;
        m.StageName__c = '대기';
        m.AccountId__c = '0011s00001ftt14AAA';
        m.BB_MissionDetails__c = '가게 경쟁력 강화 타겟 영업';
        m.RecordTypeId = '0125j000001NIAxAAO';
        
        insert m;
        

        System.Test.startTest();
        BB_GetMissionObjectController.getMissionDelayRecord();

        System.Test.stopTest();

      }


This is error message from production

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: 0011s00001YiSFA: [] Stack Trace: Class.BB_GetMissionObjectControllerTest.getMissionDelayRecordTest: line 18, column 1

1 Answer 1

4

You should not reference Ids in the test class. Instead you have to create the record and assign that fields . When you reference the ids in most scenarios that the data may not be present in other sandboxes or production.

In your Scenario you are referring to some AccountId__c field with the id 0011s00001ftt14AAA

You have to create an Account and assign the Id with that field like below.

Account acc= new Account();
acc.name='sample account';
insert acc;

use this Account id in that field as below.

m.AccountId__c = acc.id;

Also you need to remove the references of OwnerId in the same way and for recordtypeId you need to get recordtype Id using the below syntax.

`Id recordtypeid =    Schema.SObjectType. SobjectName.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();`
3
  • 2
    It's not just a chance that the Id won't exist, it's nearly a guarantee that it won't exist. The exceptions being data in a full-copy or partial-copy sandbox (which will have the same Ids as the records in your production org) and records for "setup objects" (User, Profile, Group, RecordType, and a few others) after they are available in production and you refresh your sandbox. Getting rid of the hard-coded Id for the RecordType is also a good idea. Hard-coding the OwnerId is probably the least problematic here (though creating a specific user for the test is probably best practice).
    – Derek F
    Commented Jan 31, 2023 at 13:34
  • 1
    @DerekF, Yes I agree with you. I have updated the answer accordingly Commented Jan 31, 2023 at 13:45
  • SaiPraveenKakkirala, DerekF thanks a lot!!!! It works!!!
    – Yeonbi Ji
    Commented Jan 31, 2023 at 15:14

You must log in to answer this question.

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