Working on a test class for PendingServiceRouting to be used invocable methods for a custom apex class. Although while inserting the PendingServiceRouting I am getting Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Is it required to have see all data true in doing the same? Found no reference regarding the same. Adding the test class below

public class CustomScenarioTest {
static void setup()
Account acc = new account(name='Lazy Account');
        insert acc;

    Contact oContact = new contact(firstname='John',lastname='Flick',email='[email protected]',accountid=acc.id);
    insert oContact;

    Entitlement entl = new entitlement(name='Test Entilement',accountid=acc.id,type=' Support', StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));
    insert entl;

    String contactId;
    if (oContact != null)
        contactId = oContact.Id;

    String entlId;
    if (entl != null)
        entlId = entl.Id;

    List<Case> cases = new List<Case>{};
    if (entlId != null){
        Case c = new Case(Subject = 'Test Case with Entitlement ', AccountId = acc.id, ContactId = oContact.id, EntitlementId=entl.id, slaStartDate=system.now());

    // Insert the Account records that cause the trigger to execute.
    if (cases.isEmpty()==false){
        insert cases;
        List<Id> caseIds = new List<Id>();
        for (Case cL : cases){
            system.debug('Case Entitlement:'+cL.entitlementid);

     PendingServiceRouting psrObj = new PendingServiceRouting(
            CapacityWeight = 1,
            IsReadyForRouting = FALSE,
            RoutingModel  = 'MostAvailable',
            RoutingPriority = 1,
            RoutingType = 'SkillsBased',
            ServiceChannelId = null,
            WorkItemId = cases[0].Id,
            PreferredUserId = UserInfo.getUserId(),
            PushTimeout = 0
        insert psrObj;  // Causing the error

static testMethod void testBar()
List<Case> caseList=[select id from case];


1 Answer 1


The issue is related to the fact that you do not have a valid ServiceChannelId.

However, if you try to create the ServiceChannel record on the test class you will run against a DML operation Insert not allowed on ServiceChannel message. So you can either try to use the @SeeAllData annotation here or explore DML Mocking so that your PendingServiceRouting has a valid ServiceChannelId related.

Best regards!

You must log in to answer this question.

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