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
Irene ShureIrene Shure 

Validation error upon deployment of apex trigger.

Hello.

I have a test class for a trigger that has a 100% code coverage when I run it in Sandbox. However, when I try to validate it in live, I get the following error:
System.DmlException: Update failed. First exception on row 0 with id a07A000000d5UuvIAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CCPSSupEvent: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 1; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Trigger.CCPSSupEvent: line 118, column 1: [] 
Stack Trace: Class.Test_CCPSSupEvent.myTestMethod1: line 30, column 1


Here is my trigger:
trigger CCPSSupEvent on CCP__c (after insert, after update) {

  if(checkRecursive.runOnce()) {   
        String calendarSubject;
        string ssName = '';
        
        if (System.Trigger.isInsert) {
            List<Event> events = new List<Event>();
            
            for (CCP__c c: Trigger.New) {
                
                if(c.Is_Rep_Required_to_Attend__c == true) {
                    calendarSubject = '@@@ CCP1 - ' + c.Name;
                } else  {
                    calendarSubject = 'CCP1 - ' + c.Name;
                } 
                
                if (c.Must_Deliver_On_Datetime__c != null) {
                
                    // add to the Sales Support calendar
                    if (c.Additional_Support__c != null) {  
                        events.add( new Event(
                            OwnerID = c.Additional_Support__c,
                            WhatID = c.ID,
                            ActivityDate = c.Must_Deliver_on_Date__c,
                            ActivityDateTime = c.Must_Deliver_on_DateTime__c,
                            Subject = calendarSubject,
                            Description = 'CCP Event - ' + c.Name,
                            DurationInMinutes = 120,
                            ShowAs = 'Free',
                            Type = 'CCP',
                            CCP_Master_Event__c = True,
                            Location = c.Customer_Name__c)
                        );
                    }
                
                    // add to the Sales Support public calendar
                    if (c.Additional_Support__c != null) {                
                        User u = [Select Full_Name__c from User where id = :c.Additional_Support__c];
                        ssName = u.Full_Name__c; 
                        
                        events.add( new Event(
                            OwnerID = '0234B000000V3cX',
                            WhatID = c.ID,
                            ActivityDate = c.Must_Deliver_on_Date__c,
                            ActivityDateTime = c.Must_Deliver_on_DateTime__c,
                            Subject = ssName + '-' + calendarSubject,
                            Description = 'CCP Event - ' + c.Name,
                            DurationInMinutes = 120,
                            ShowAs = 'Free',
                            Type = 'CCP',
                            CCP_Master_Event__c = True,
                            Location = c.Customer_Name__c)
                        );
                    }   
                }         
            }
            insert events;
        } 
        
        else if (System.Trigger.isUpdate) {
        
            Set<Id> OldWhatIds = new Set<Id>(); 
            Set<ID> OldOwnerIds = new Set<Id>();                
            List<Event> events = new List<Event>();
            
            for (CCP__c c: Trigger.New) {
                
                if(c.Is_Rep_Required_to_Attend__c == true) {
                    calendarSubject = '@@@ CCP2 - ' + c.Name;
                } else  {
                    calendarSubject = 'CCP2 - ' + c.Name;
                } 
                
                if (c.Must_Deliver_On_Datetime__c != null
                    && c.Additional_Support__c!= System.Trigger.oldMap.get(c.Id).Additional_Support__c) 
                {        
                    if (c.Additional_Support__c!= null) 
                    {    
                        events.add( new Event(
                            OwnerID = c.Additional_Support__c,
                            WhatID = c.ID,
                            ActivityDate = c.Must_Deliver_on_Date__c,
                            ActivityDateTime = c.Must_Deliver_on_DateTime__c,
                            Subject = calendarSubject + '--1',
                            Description = 'CCP Event - ' + c.Name,
                            DurationInMinutes = 120,
                            ShowAs = 'Free',
                            Type = 'CCP',
                            CCP_Master_Event__c = True,
                            Location = c.Customer_Name__c)
                        );
                        
                        User u = [Select Full_Name__c from User where id = :c.Additional_Support__c];
                        ssName = u.Full_Name__c;
                
                        events.add( new Event(
                            OwnerID = '0234B000000V3cX',
                            WhatID = c.ID,
                            ActivityDate = c.Must_Deliver_on_Date__c,
                            ActivityDateTime = c.Must_Deliver_on_DateTime__c,
                            Subject = ssName + '-' + calendarSubject + '--1',
                            Description = 'CCP Event - ' + c.Name,
                            DurationInMinutes = 120,
                            ShowAs = 'Free',
                            Type = 'CCP',
                            CCP_Master_Event__c = True,
                            Location = c.Customer_Name__c)
                        );
                    }                    
                }
                OldWhatIds.add(c.ID);
                OldOwnerIds.add(System.Trigger.oldMap.get(c.Id).Additional_Support__c);
            } 
            List<Event> OldEvents = [select Id, WhatId, OwnerId from Event where WhatID IN : OldWhatIds AND OwnerID IN : OldOwnerIds];
            delete OldEvents; 
                  
            insert events;
        }                                
    }
}

Here is my test class:
@isTest (SeeAllData = true)
public class Test_CCPSSupEvent {
    static testmethod void myTestMethod1() {
        User u1    = [select Id from User where alias='lyogi' LIMIT 1];
        
        String OrderNumberX  = '9991111';
        String OrderNameX    = OrderNumberX + '-Test CCP Record x';
        String OrderNumberY = '9992222';
        String OrderNameY   = OrderNumberY + '-Test CCP Record y';
        User SSupID1              = [select Id from User where Alias = 'NTarb' LIMIT 1]; 
        User SSupID2              = [select Id from User where Alias = 'KMurp' LIMIT 1];
        String SSup1              = SSupID1.Id;
        String SSup2              = SSupID2.Id;
                       
        RecordType RecRetailID    = [select Id from RecordType where Name = 'Retail Assistance'];
        String RecRetail          = RecRetailID.Id;
        
        System.RunAs(u1) {
        
            CCP__c c1 = new CCP__c(Name = OrderNameX, Order_Number__c = OrderNumberX, RecordTypeID = RecRetail, Must_Deliver_On_Datetime__c = System.today().addDays(2), Is_Rep_Required_to_Attend__c = false);
            insert c1;
            
            checkRecursive.resetAll();
            List<CCP__c> c2 = new List<CCP__c>();
            for(CCP__c c2u:[SELECT Id, Must_Deliver_On_Datetime__c, Additional_Support__c, Is_Rep_Required_to_Attend__c FROM CCP__c WHERE Order_Number__c = :OrderNumberX]) {
                c2u.Additional_Support__c = SSup1;
                c2u.Is_Rep_Required_to_Attend__c = true;
                c2.add(c2u);
            }
            update c2;
            
            checkRecursive.resetAll();
            List<CCP__c> c3 = new List<CCP__c>();
            for(CCP__c c3u:[SELECT Additional_Support__c, Is_Rep_Required_to_Attend__c FROM CCP__c WHERE Order_Number__c = :OrderNumberX]) {
                c3u.Additional_Support__c = SSup2;
                c3u.Is_Rep_Required_to_Attend__c = false;
                c3.add(c3u);
            }
            update c3;                
            
            checkRecursive.resetAll();
            List<CCP__c> c4 = new List<CCP__c>();        
            for(CCP__c c4u:[SELECT Id FROM CCP__c WHERE Order_Number__c = :OrderNumberX]) {
                c4.add(c4u);
            }
            delete c4;
    
            checkRecursive.resetAll();
            CCP__c c6 = new CCP__c();
            c6.Name = OrderNameY;
            c6.Order_Number__c = OrderNumberY;
            c6.RecordTypeID = RecRetail;
            c6.Is_Rep_Required_to_Attend__c = true;
            c6.Must_Deliver_On_Datetime__c = System.today().addDays(3);
            c6.Additional_Support__c = SSup2;
            insert c6; 
            
            checkRecursive.resetAll();
            List<CCP__c> c7 = new List<CCP__c>();
            for(CCP__c c7u:[SELECT Must_Deliver_On_Datetime__c FROM CCP__c WHERE Order_Number__c = :OrderNumberY]) {
                c7u.Additional_Support__c = null;
                c7u.Must_Deliver_On_Datetime__c = null;
                c7.add(c7u);
            }
            update c7;  
            
            checkRecursive.resetAll();
            List<CCP__c> c9 = new List<CCP__c>();        
            for(CCP__c c9u:[SELECT Id FROM CCP__c WHERE Order_Number__c = :OrderNumberY]) {
                c9.add(c9u);
            }
            delete c9;
        }
    }
}
I would appreciate any help. Thanks.
Irene
Best Answer chosen by Irene Shure
Irene ShureIrene Shure
I figured this one out. I was using the OwnerId of the public calendar in Sandbox. The error went away after I changed the OwnerId to the one in production.

All Answers

deepak balur 19deepak balur 19
Do you have the OrderNums X and Y in Prodn? You have hard coded them and I don't think you are retrieving any records in 25 causing update errors in line 30.
Shobit GuptaShobit Gupta
(SeeAllData = true) makes all the data of the org available to be used in test class. In this case, test class is not able to find the User and events records which you are trying to insert in the line 118 of your trigger.

You need to create the records in your test class to use them as test data.
Irene ShureIrene Shure
Thank you for your help deepak and Shobit,
OrderNumberX and OrderNumberY are test data. I am inserting a new test record in line 20 and updating it in line 30. Am I doing it wrong? Should I not use (SeeAllData=true)?
 
deepak balur 19deepak balur 19
SeeAllData is only for data within that org; so it worked successfully in your DEV org but when you try to run it in Prodn it cannot this data from the DEV org. Makes sense? 
Irene ShureIrene Shure
I figured this one out. I was using the OwnerId of the public calendar in Sandbox. The error went away after I changed the OwnerId to the one in production.
This was selected as the best answer