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
Samruddhi GokhaleSamruddhi Gokhale 

Getting invalid cross reference key error

Hi,

I have a custom object and Case object has lookup to that custom object. In 'After Insert' trigger of custom object, I call a static method of a class (which runs in without sharing mode) that inserts a case for every custom object record and populates the lookup relationship. It also sets dml options for firing case assignment rules. 
Sharing settings of Case and Custom object is Private. If I do not give View All permission for Cases, then this code does not work and gives invalid cross reference key error. The same code works in other organizations, hence I am really confused what is happenning. The code for that class is as follows:

AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];   
List<Case> caseList = new List<Case>();  
//clist contains Trigger.New
    for(Custom_Object__c cust:cList)
    {
        case c = new Case();
        c.Status = 'New'; 
        c.Custom_Object__c = cust.Id;
         //Setting the DMLOption on Case instance
                    Database.DMLOptions dmlOpts = new Database.DMLOptions();
                    dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
                    dmlOpts.EmailHeader.triggerUserEmail=true;
                    c.setOptions(dmlOpts);
                    caseList.add(c);
    }
    insert caseList;
Anirudh SinghAnirudh Singh
Hi Samruddhi,
Please can you check if in the other Orgs where the code is working, what is the access for Cases, i.e. whether it is 'View All'? Or the same access permissions are for all the Orgs.
Samruddhi GokhaleSamruddhi Gokhale
OWD and all other profile access is same. 
Anirudh SinghAnirudh Singh
Hi Samruddhi,

I think your code must be similar to below:

Trigger:

trigger TicketTrigger on Ticket__c(after insert)
{
    TicketHandler.createCases(Trigger.New);
}

Class:

public without sharing class TicketHandler
{
    public static void createCases(List<Ticket__c> ticketRecords)
    {
        AssignmentRule assignmentRuleRecord=new AssignmentRule();
        assignmentRuleRecord=[SELECT Id FROM AssignmentRule WHERE Name='Standard' AND SobjectType='Case' AND Active=true];
        
        List<Case> newCasesList=new List<Case>();  
        
        for(Ticket__c ticketRecord: ticketRecords)
        {
            Case newCase=new Case();
            newCase.Status='New'; 
            newCase.Ticket__c=ticketRecord.Id;
            
            Database.DMLOptions dmlOpts=new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.assignmentRuleId=assignmentRuleRecord.Id;
            dmlOpts.EmailHeader.triggerUserEmail=true;
            newCase.setOptions(dmlOpts);
            newCasesList.add(newCase);
        }
        
        insert newCasesList;
    }
}

I cannot comment on what is causing the issue without checking the Org or the code which is not possible.

Though I can see you are using Assignment Rules, which might be causing the error as you have not specified the Assignment Rule name in the query.

Please try something like this, which I have used in the above code:

AssignmentRule assignmentRuleRecord=new AssignmentRule();
assignmentRuleRecord=[SELECT Id FROM AssignmentRule WHERE Name='Standard' AND SobjectType='Case' AND Active=true];

Using the Name to query specific Assignment Rule may solve the problem. As it may happen that when you are querying the Assignment Rule and using limit 1, the result record may vary in different Orgs according to the number of Assignment Rules present in the Orgs.

Please let me know if it works.

Also, if it doesnt work, please let me know the following:

1. Which profile you are using?
2. Are you using the same profile in the other orgs?
3. Are there record types available for Case object? If yes, then check that the profile has same permission for that record type in all the orgs.
4. Are there record types available for Custom object (in my example Ticket__c)? If yes, then check that the profile has same permission for that record type in all the orgs.

Thanks and Regards,
Anirudh Singh