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
Cherry Shen 1Cherry Shen 1 

Insert failed...FIELD_CUSTOM_VALIDATION_EXCEPTION, Can't close an opportunity with inactive product, please review and update the products

@isTest (SeeAllData=true)
private class InvoiceManagerTest {
    
    private static Account testAccount;
    private static Opportunity testOpportunity;
    private static Opportunity testEmptyOpportunity;
    private static OpportunityLineItem testProduct1;
    private static OpportunityLineItem testProduct2;
    
    /*
    * init test data
    */
    private static void initData(){
        //init account
        testAccount=new Account(Name='TestAccount', BillingCountry='France');
        insert testAccount;
        //init opportunity
        testOpportunity=new Opportunity(Name='TestOpportunity',stageName='Won',CloseDate=System.today(),Order_date__c=System.today());
        testEmptyOpportunity=new Opportunity(Name='testEmptyOpportunity',stageName='Won',CloseDate=System.today(),Order_date__c=System.today());
        insert new List<Opportunity>{testEmptyOpportunity,testOpportunity};
        
        // create  products
        Product2 p1 = new Product2(Name='Test Product');
        Product2 p2 = new Product2(Name='Test Product');
        insert new List<Product2>{p1,p2};
        
       
        
        // Add product to standard pricebook
        // Id taken from Salesforce.com
        String standardPriceBookId ='';
        for(Pricebook2 p:[Select id From Pricebook2 p where isStandard=true limit 1]){
            standardPriceBookId=p.id;
        }
        //System.debug(standardPriceBookId+':'+p1.id);
        PricebookEntry pbe1 = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p1.Id,UnitPrice=2,isActive=true);
        insert pbe1;
        PricebookEntry pbe2 = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id,UnitPrice=2,isActive=true);
        insert pbe2;
        
        
        testProduct1 = new OpportunityLineItem(TotalPrice=2 ,Quantity=2 ,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Inactive_product__c=false,Ready_to_Invoice__c=true,Invoice__c=null);
        testProduct2 = new OpportunityLineItem(TotalPrice=2,Quantity=2,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Inactive_product__c=false,Ready_to_Invoice__c=true,Invoice__c=null);
        insert new List<OpportunityLineItem>{testProduct1,testProduct2};
        update testOpportunity;
        update testEmptyOpportunity;
        
    }
    
    /** 
    *   Test method : testGenerateInvoice method covers the logic and expected results
    *   @category Testing Method
    */
    public static testMethod void testGenerateInvoice(){
        initData();
        test.startTest();
          String result1=InvoiceManager.generateInvoice(testEmptyOpportunity.id);
          System.assertNOTEquals(result1,testEmptyOpportunity.id);
          String result2=InvoiceManager.generateInvoice(testOpportunity.id);
        
          System.assertEquals(result2,testOpportunity.id);
          //check invoice
          List<Invoice__c> lstInvoice=[select id, Invoice_Date__c  from Invoice__c where Opportunity__c=:testOpportunity.id];
          System.assertEquals(lstInvoice.isEmpty(),false);
          List<OpportunityLineItem> lstProduct=[select Opportunity.Billing_Entity__C,Ready_to_Invoice__c,Invoice_Date__c,Invoice__c from OpportunityLineItem where id=:testProduct1.id OR id=:testProduct2.id];  
          for(OpportunityLineItem item:lstProduct){
            System.assertEquals(item.Invoice_Date__c,System.today());
            System.assertEquals(item.Invoice__c,lstInvoice.get(0).id);
          }
          
       test.stopTest();     
              
     }
    
    
    



    

}

I got below error when I run apex test.

"Insert failed...FIELD_CUSTOM_VALIDATION_EXCEPTION, Can't close an opportunity with inactive product, please review and update the products. In exceptional cases please contact your Salesforce administrator."

So I added "Inactive_product__c=false" in the code to meet the criteria, but the error still appears. Our sandbox have a related validation rule as follows: 
User-added image

and the related fields are as below:
1. In Opportunity:
User-added image
2. In OpportunityLineItem:
User-added image

I have no idea what should I do in test class now, can anybody help me?
Cherry Shen 1Cherry Shen 1
I added "Inactive_product__c=false" in Line 42 and Line 43, just for your reference.