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
iSqFt_ADiSqFt_AD 

Quick Help on Failed Deployment of Updated Test Class from SB to Prod

This class already exists and is functioning in our Production instance. I made one change to it and am attempting to deploy it into Production and I get an error.

 

Test Class:

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testTriggerSet {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        //test of leadDuplicatePreventer
        Lead l = new Lead();
        l.LastName = 'tester smith';
        l.Company = 'tester company';
        l.Email = 'tester@tester.com';
        l.Phone = '(513) 888-8888';
        l.Status = 'Open';
        l.LeadSource = 'Kiosk';
        insert l;
            
        //test of 
        
        Task t = new task();
        t.Subject = 'test task';
        t.ActivityDate = Date.today();
        t.Type = 'Phone Conversation';
        t.Focus__c = 'Sales Activity';
        t.WhatId = '001P000000Wfluj';
        insert t;
        
        Account a = new account();
        a.Name = 'tester account';
        a.Phone = '(513) 333-3321';
        insert a;
        
        Opportunity o = new Opportunity();
        o.Name = 'tester opp';
        o.StageName = 'Proposal Made';
        o.CloseDate = Date.today();
        o.Amount = 444.23;
        o.Contract_Length_in_Months__c = 12;
        insert o;
        
        o.StageName = 'Proposal Made';
        update o;
        
        Contact c = new Contact();
        c.LastName = 'tester';
        c.Email = 'joe@sss.com';
        c.Phone = '(513) 332-3321';
        insert c;
        
        
    }
}

 

Update Made:

 

o.Contract_Length_in_Months__c = 12;

 

Error when attempting to Validate:

 

testTriggerSet.myUnitTest()Class311

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []", Failure Stack Trace: "Class.testTriggerSet.myUnitTest: line 31, column 1"

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testTriggerSet {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        //test of leadDuplicatePreventer
        Lead l = new Lead();
        l.LastName = 'tester smith';
        l.Company = 'tester company';
        l.Email = 'tester@tester.com';
        l.Phone = '(513) 888-8888';
        l.Status = 'Open';
        l.LeadSource = 'Kiosk';
        insert l;
            
        //test of 
                
        Account a = new account();
        a.Name = 'tester account';
        a.Phone = '(513) 333-3321';
        insert a;

        Task t = new task();
        t.Subject = 'test task';
        t.ActivityDate = Date.today();
        t.Type = 'Phone Conversation';
        t.Focus__c = 'Sales Activity';
        t.WhatId = a.Id;
        insert t;
        
        Opportunity o = new Opportunity();
        o.Name = 'tester opp';
        o.StageName = 'Proposal Made';
        o.CloseDate = Date.today();
        o.Amount = 444.23;
        o.Contract_Length_in_Months__c = 12;
        insert o;
        
        o.StageName = 'Proposal Made';
        update o;
        
        Contact c = new Contact();
        c.LastName = 'tester';
        c.Email = 'joe@sss.com';
        c.Phone = '(513) 332-3321';
        insert c;
        
        
    }
}

 Try this.

All Answers

Naidu PothiniNaidu Pothini
 t.WhatId = '001P000000Wfluj';

 I think, this might be the problem. You are using hardcoded Id. Can you please check whether you have this id in production?

Naidu PothiniNaidu Pothini
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class testTriggerSet {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        
        //test of leadDuplicatePreventer
        Lead l = new Lead();
        l.LastName = 'tester smith';
        l.Company = 'tester company';
        l.Email = 'tester@tester.com';
        l.Phone = '(513) 888-8888';
        l.Status = 'Open';
        l.LeadSource = 'Kiosk';
        insert l;
            
        //test of 
                
        Account a = new account();
        a.Name = 'tester account';
        a.Phone = '(513) 333-3321';
        insert a;

        Task t = new task();
        t.Subject = 'test task';
        t.ActivityDate = Date.today();
        t.Type = 'Phone Conversation';
        t.Focus__c = 'Sales Activity';
        t.WhatId = a.Id;
        insert t;
        
        Opportunity o = new Opportunity();
        o.Name = 'tester opp';
        o.StageName = 'Proposal Made';
        o.CloseDate = Date.today();
        o.Amount = 444.23;
        o.Contract_Length_in_Months__c = 12;
        insert o;
        
        o.StageName = 'Proposal Made';
        update o;
        
        Contact c = new Contact();
        c.LastName = 'tester';
        c.Email = 'joe@sss.com';
        c.Phone = '(513) 332-3321';
        insert c;
        
        
    }
}

 Try this.

This was selected as the best answer
iSqFt_ADiSqFt_AD

Good eye, but what is really odd is that the class in Production does not have that in it, and I did not just add it. What is really odd about that is that I am the only Admin with access to the Sandbox.

 

No idea how this happened but I believe you are correct.