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
SFDC-DMGSFDC-DMG 

Code Coverage Failures - Update Failed

Hi,

 

We're running into code coverage issues. This is something that was written for our org in the past and now it is causing errors that are not allowing us to do a number of functions in our system. Can someone please help me?

 

I am getting only 60% coverage and failed tests for the following. Here are the two reasons given for the failure:

 

appnetaCMRRrollupTest.testTrigger1387.0System.DmlException: Update failed. First exception on row 0 with id 0064000000P2aKkAAJ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, License Delivery Contact field is required to move an Opportunity to closed won and submit to finance.: [Contact_Name__c]Class.appnetaCMRRrollupTest.testTrigger: line 62, column 1
appnetaCMRRrollupTest.testBatch2234.0System.DmlException: Update failed. First exception on row 0 with id 0064000000P2aKiAAJ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, License Delivery Contact field is required to move an Opportunity to closed won and submit to finance.: [Contact_Name__c]Class.appnetaCMRRrollupTest.testBatch: line 76, column 1

 

@isTest

//
// Test class for the trigger, batch and methods that perform a recalculation of the  
//   Total_Active_CMRR__c field from all the opportunities associated with those accounts.  
//  
//
//  Walt Thomas - Harvest Solutions - 2012
//

public with sharing class appnetaCMRRrollupTest {
  static Account acct = new Account();
  static Opportunity opp = new Opportunity();
    
  static{
    Product2 prod = new Product2();
    prod.name = 'test product';
    prod.Term__c = '1 year';
    prod.Subcription_Years__c = 1;
    prod.SubscriptionTerm_Months__c = 12;
    prod.Product_Type__c = 'PV Cloud';
    prod.Product_Class__c = 'Product';
    prod.Product_Listing__c = 'Subscription';
    prod.IsActive = true;
    insert prod;
    
    Pricebook2 pricebook = [SELECT id FROM pricebook2 WHERE name = 'Standard Price Book'];
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = pricebook.id;
        pbe.Product2Id = prod.id;
        pbe.UnitPrice = 100;
        pbe.IsActive = true;
        insert pbe;
        
    acct = new Account();
    acct.name = 'Test Account';
    insert acct;
    
    opp = new Opportunity();
    opp.name = 'test opp 1';
    opp.AccountId = acct.id;
    opp.StageName = 'Opportunity';
    opp.CloseDate = date.today();
    opp.Subscription_Start_Date__c = date.today() - 30;
    opp.Subscription_End_Date__c = date.today() + 30;
    insert opp;
    
    OpportunityLineItem oppLI = new OpportunityLineItem();
    oppLI.Quantity = 3;
        oppLI.UnitPrice = 250.00;
        oppLI.Opportunityid = opp.id;
        oppLI.pricebookentryid = pbe.id;
        insert oppLI;
    
  }
    
  static testMethod void testTrigger(){
    acct = [select id, Total_Active_CMRR__c from Account where id = :acct.id];
    system.debug('Total CMR ' + acct.Total_Active_CMRR__c);
    system.assert(acct.Total_Active_CMRR__c == 0);
    opp.stagename = 'Invoiced';
    update opp;
    opp = [select id, Opportunity_CMRR__c, stagename, iswon, Subscription_Term__c,Amount,
          Subscription_Start_Date__c, Subscription_End_Date__c   
      from Opportunity where id = :opp.id];
    system.debug('Opp  ' + opp);
    acct = [select id, Total_Active_CMRR__c, Total_Active_Contract_Value__c, Total_Active_Opp_Count__c 
      from Account where id = :acct.id];
    system.debug('Total CMR ' + acct.Total_Active_CMRR__c);
    system.assert(acct.Total_Active_CMRR__c <> 0);
    system.assert(acct.Total_Active_Opp_Count__c == 1);
    system.assert(acct.Total_Active_Contract_Value__c == opp.Amount);
  }
  static testMethod void testBatch(){ 
    opp.stagename = 'Invoiced';
    update opp;
    // there will be no change from the batch because the trigger already did the work.  
    Test.StartTest();
       appnetaCMRRbatch batch = new appnetaCMRRbatch();
      ID batchprocessid = Database.executeBatch(batch);
       Test.StopTest();
    acct = [select id, Total_Active_CMRR__c from Account where id = :acct.id];
    system.debug('Total CMR ' + acct.Total_Active_CMRR__c);
    system.assert(true);
  }
  static testMethod void testSchedule(){
    Test.startTest();
    appnetaCMRRschedule testSched = new appnetaCMRRschedule();
        String jobId = System.schedule(  'testSchedule', '20 30 8 10 2 ?', testSched);
       // Get the information from the CronTrigger API object 
         CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, 
           NextFireTime
           FROM CronTrigger WHERE id = :jobId];
      Test.stopTest();
      System.assertEquals(0, ct.TimesTriggered);
    System.assert(ct.NextFireTime <> null);
    }
  
}

Can someone please help? I've been struggling to try and fix this and haven't been able to do so. If you need the original classes that they are testing, please let me know, and I'll be happy to paste them.

 

Thank you!!

 

Naidu PothiniNaidu Pothini

License Delivery Contact field is required to move an Opportunity to closed won and submit to finance.: [Contact_Name__c]

 

 

 

Did you try adding the Contact_Name__c field on the Opportunity inside the test class?