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
PamelaMPamelaM 

change set error due to test class

I have created a new app for my org, with new record types, page layouts, etc and deployed it between sandboxes (dev to full) to test the additions. This worked fine but now I'm trying to deploy it in my production org and the change set has failed the validation test.

Basically my problem is I'm not a developer and all the code in our org was written by a consultant before I joinedthe company so I don't want to change things in production when I'm not sure about them. Therefore I would appreciate any help that could be offered!

The new record type won't be using the funnel camp tracker so I'm not sure if that is part of the issue. Would it be possible to add something into the trigger to exclude the new record type I'm trying to deploy? 

 

 

Change set deployment error:

TestClass.testOpportunityTrigger() Class 49 1

Failure Message: "System.AssertException: Assertion Failed: Expected: 3, Actual: 0", Failure Stack Trace: "Class.TestClass.testOpportunityTrigger: line 49, column 1"

 

@isTest
private class TestClass {

    //Test Opportunity Trigger
    static testMethod void testOpportunityTrigger() {
        
        //* Create Test Account, make sure all required fields for Account are populated here:
        Account testAccount = new Account(name = 'TestAccount01');
        insert (testAccount);
        
        test.startTest();
        
        //*Create Opportunities
        Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
        list<Opportunity> testListOpty = new list<Opportunity>();
        
        date d = system.Today();
        d.addMonths(1);
        
        //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
        testListOpty.add(new Opportunity(AccountId = testAccount.Id, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
        testListOpty.add(new Opportunity(AccountId = testAccount.Id, Name = 'TestOpty02', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
        testListOpty.add(new Opportunity(AccountId = testAccount.Id, Name = 'TestOpty03', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
        
        insert(testListOpty);
                
        //* Check Data, 3 opty therefore we should have 3 funnel trackers.
        system.assertEquals(3, [select Id from Funnel_Camp_Tracker__c where Opportunity__r.Account.Id = :testAccount.Id].size());
        
        test.stopTest();
    }
    
    static testMethod void testGamePlanController(){
      
      //* Create Test Data
      //* Create Test Account, make sure all required fields for Account are populated here:
        Account testAccount = new Account(name = 'TestAccount01');
        insert (testAccount);
        Contact testContact = new Contact(FirstName = 'Bruce', LastName = 'Wayne', Phone = '123', Email = 'batman@batcave.com', MobilePhone = '321');
        insert(testContact);
        
        //*Create Opportunities
        Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
        list<Opportunity> testListOpty = new list<Opportunity>();
        
        date d = system.Today();
        d.addMonths(1);
        
        //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
        testListOpty.add(new Opportunity(AccountId = testAccount.Id, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
        insert(testListOpty);
        
        list<OpportunityContactRole> TestlistOCR = new list<OpportunityContactRole>();
        for(Integer i=0;i<5;i++){
          TestlistOCR.add(new OpportunityContactRole(Role = 'testRole' + i, OpportunityId = testListOpty[0].Id, ContactId = testContact.Id));
        }
        
        insert (TestlistOCR);
        
      test.startTest();
      
      pageReference p = Page.GenerateGamePlan;
      p.getParameters().put('id', testListOpty[0].Id);
        test.setCurrentPage( p );
        GamePlanController GPC = new GamePlanController();
        GPC.oOpty = testListOpty[0];
              
      test.stopTest();
      
    
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini

Can you check if there is anything to do with the default record type?

 

I see that the test class is using default recordtype during opportunity creation and i think when you are trying to deploy new recordtype, it might be taking this new record type as default record type...

 

 

or you might try deploying the test class with the following changes

 

@isTest
private class TestClass{

  //Test Opportunity Trigger
  static testMethod void testOpportunityTrigger() {
        
    //* Create Test Account, make sure all required fields for Account are populated here:
    Account testAccount = new Account(name = 'TestAccount01');
    insert (testAccount);
    
    test.startTest();
    
    //*Create Opportunities
    Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
    list<Opportunity> testListOpty = new list<Opportunity>();
    
    date d = system.Today();
    d.addMonths(1);
    
    //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty02', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty03', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    
    insert(testListOpty);
            
    //* Check Data, 3 opty therefore we should have 3 funnel trackers.
    system.assertEquals(3, [select Id from Funnel_Camp_Tracker__c where Opportunity__r.Account.Id = :testAccount.Id].size());
        
    test.stopTest();
  }
    
  static testMethod void testGamePlanController(){
      
    //* Create Test Data
    //* Create Test Account, make sure all required fields for Account are populated here:
    Account testAccount = new Account(name = 'TestAccount01');
    insert (testAccount);
    Contact testContact = new Contact(FirstName = 'Bruce', LastName = 'Wayne', Phone = '123', Email = 'batman@batcave.com', MobilePhone = '321');
    insert(testContact);
        
    //*Create Opportunities
    Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
    list<Opportunity> testListOpty = new list<Opportunity>();
        
    date d = system.Today();
    d.addMonths(1);
        
    //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    insert(testListOpty);

    list<OpportunityContactRole> TestlistOCR = new list<OpportunityContactRole>();
    for(Integer i=0;i<5;i++)
    {
      TestlistOCR.add(new OpportunityContactRole(Role = 'testRole' + i, OpportunityId = testListOpty[0].Id, ContactId = testContact.Id));
    }
        
    insert (TestlistOCR);
      
    test.startTest();
    
    pageReference p = Page.GenerateGamePlan;
    p.getParameters().put('id', testListOpty[0].Id);
    test.setCurrentPage( p );
    GamePlanController GPC = new GamePlanController();
    GPC.oOpty = testListOpty[0];
            
    test.stopTest();
  }
}

  I havent changed  anything in the class except adding the RecordTypeId.

 

give a try... i am not sure what might be causing this error..

 

Gud luck.

All Answers

Vinit_KumarVinit_Kumar

The issue is that Funnel_Camp_Tracker__c are not getting inserted.This might be because the trigger for which this Trigger is written is not working properly or some Validation rule which is not getting honoured.

 

The way I would approach is by running this Test class in your Production org and see what is going on in the debug logs.

 

 

PamelaMPamelaM

I ran a test on the TestClass and it showed no errors. Is there something specific I should look for in the debug logs? 

 

Also, if it was a problem with the trigger or a validation rule would I not have experienced the error when I tried to deploy the change set in my full sandbox?

Vinit_KumarVinit_Kumar

No,you won't get any error message while deploying from Sandbox to Sandbox because the Test don't run during that.

 

The Test only run when you deploy from Sandbox to Prod only.

 

I am sure you will see the same error message when you run this class(i.e.Testclass) in your prod independently.Then,you can check the error message in your debug logs which would help you understanding it.

PamelaMPamelaM

I ran the test in production and it passed. However the issue might be with the Opportunity Trigger. I thought this only applied to the "Opportunity Europe" record type but could my new record type be getting stopped by this trigger?

 

trigger Opportunity_Trigger on Opportunity (after delete, after insert, after undelete, 
after update, before delete, before insert, before update) {

    if(trigger.isAfter){
        if(!trigger.isDelete){
             //CHEP_Opportunity_Revenue_Calculation.CreateOptyRev(trigger.new); 
            CHEP_Opportunity_Forcast_Creation.CreateForecast(trigger.new);
            }
        if(trigger.isInsert){
            
            
            //* if Opportuntiy is created link to respective revenues
           
            
            //* Create funnel camp trackers
            list<Opportunity> listOpty = trigger.new; 
            list<Funnel_Camp_Tracker__c> listFCT= new list<Funnel_Camp_Tracker__c>();
            Id recId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
            
            for(Opportunity o : listOpty ){
                if(o.RecordTypeId == recId){
                    listFCT.add(new Funnel_Camp_Tracker__c(Opportunity__c = o.Id, CurrencyIsoCode = o.CurrencyIsoCode));
                }
            }
            insert(listFCT); 
        }
        
       
    }
}

 

Vinit_KumarVinit_Kumar

Are you inserting Opportunity Records with RecordType other than 'Opportunity Europe '

PamelaMPamelaM

I'm not actually inserting any records at the moment. I'm trying to deploy a new record type called "Opportunity Aerospace" through a change set.

Vinit_KumarVinit_Kumar

That seems weird to me as why the Metadata Deployment is causing the Tests to run.

 

Can you try using Force.com IDE or ANT to make the deployment.

PamelaMPamelaM

I'll give IDE a go and see if that makes a difference. 

Naidu PothiniNaidu Pothini

Can you check if there is anything to do with the default record type?

 

I see that the test class is using default recordtype during opportunity creation and i think when you are trying to deploy new recordtype, it might be taking this new record type as default record type...

 

 

or you might try deploying the test class with the following changes

 

@isTest
private class TestClass{

  //Test Opportunity Trigger
  static testMethod void testOpportunityTrigger() {
        
    //* Create Test Account, make sure all required fields for Account are populated here:
    Account testAccount = new Account(name = 'TestAccount01');
    insert (testAccount);
    
    test.startTest();
    
    //*Create Opportunities
    Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
    list<Opportunity> testListOpty = new list<Opportunity>();
    
    date d = system.Today();
    d.addMonths(1);
    
    //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty02', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty03', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    
    insert(testListOpty);
            
    //* Check Data, 3 opty therefore we should have 3 funnel trackers.
    system.assertEquals(3, [select Id from Funnel_Camp_Tracker__c where Opportunity__r.Account.Id = :testAccount.Id].size());
        
    test.stopTest();
  }
    
  static testMethod void testGamePlanController(){
      
    //* Create Test Data
    //* Create Test Account, make sure all required fields for Account are populated here:
    Account testAccount = new Account(name = 'TestAccount01');
    insert (testAccount);
    Contact testContact = new Contact(FirstName = 'Bruce', LastName = 'Wayne', Phone = '123', Email = 'batman@batcave.com', MobilePhone = '321');
    insert(testContact);
        
    //*Create Opportunities
    Id OptyRecId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Opportunity Europe').getRecordTypeId();
    list<Opportunity> testListOpty = new list<Opportunity>();
        
    date d = system.Today();
    d.addMonths(1);
        
    //* Insert a list of opportunities, make sure all required fields for Opportunity are populated here:
    testListOpty.add(new Opportunity(AccountId = testAccount.Id, RecordTypeId = OptyRecId, Name = 'TestOpty01', CurrencyIsoCode = 'EUR', StageName = '02. Positioned in Category', CloseDate = d));
    insert(testListOpty);

    list<OpportunityContactRole> TestlistOCR = new list<OpportunityContactRole>();
    for(Integer i=0;i<5;i++)
    {
      TestlistOCR.add(new OpportunityContactRole(Role = 'testRole' + i, OpportunityId = testListOpty[0].Id, ContactId = testContact.Id));
    }
        
    insert (TestlistOCR);
      
    test.startTest();
    
    pageReference p = Page.GenerateGamePlan;
    p.getParameters().put('id', testListOpty[0].Id);
    test.setCurrentPage( p );
    GamePlanController GPC = new GamePlanController();
    GPC.oOpty = testListOpty[0];
            
    test.stopTest();
  }
}

  I havent changed  anything in the class except adding the RecordTypeId.

 

give a try... i am not sure what might be causing this error..

 

Gud luck.

This was selected as the best answer
PamelaMPamelaM

And the gold star goes to Naidu!

 

I forgot I had changed my default record type to Aerospace during testing in the sandbox. I changed it back to Europe and the change set has deployed with no problems! :)

 

 

 

Additional thanks to Vinit_Kumar for your suggestions. Kudos to you both!