• Cherry Shen 1
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
@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?
We have an active "Invoice Manager" in production, now we need to make some changes in it, so we're ready to modify the relevant Apex class in Sandbox first, but before that, we execute the apex test without any modification, and get an error with the origianl apex files. How does it happen as they are active in production in the meantime? Can anybody tell me if something important we've missed?
User-added image

Here is the test file, the log message above shows the error occurs in the line "insert new List<OpportunityLineItem>{testProduct1,testProduct2};", is it the problem of OpportunityLineItem? What should it be?
@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='Test',CloseDate=System.today());
        testEmptyOpportunity=new Opportunity(Name='testEmptyOpportunity',stageName='Test',CloseDate=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,Ready_to_Invoice__c=true,Invoice__c=null);
        testProduct2 = new OpportunityLineItem(TotalPrice=2,Quantity=2,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Ready_to_Invoice__c=true,Invoice__c=null);
        insert new List<OpportunityLineItem>{testProduct1,testProduct2};
       
        
    }
    
    /** 
    *   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();     
              
     }   

}

 
@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?
We have an active "Invoice Manager" in production, now we need to make some changes in it, so we're ready to modify the relevant Apex class in Sandbox first, but before that, we execute the apex test without any modification, and get an error with the origianl apex files. How does it happen as they are active in production in the meantime? Can anybody tell me if something important we've missed?
User-added image

Here is the test file, the log message above shows the error occurs in the line "insert new List<OpportunityLineItem>{testProduct1,testProduct2};", is it the problem of OpportunityLineItem? What should it be?
@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='Test',CloseDate=System.today());
        testEmptyOpportunity=new Opportunity(Name='testEmptyOpportunity',stageName='Test',CloseDate=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,Ready_to_Invoice__c=true,Invoice__c=null);
        testProduct2 = new OpportunityLineItem(TotalPrice=2,Quantity=2,PriceBookEntryId=pbe1.Id, OpportunityId=testOpportunity.Id,Ready_to_Invoice__c=true,Invoice__c=null);
        insert new List<OpportunityLineItem>{testProduct1,testProduct2};
       
        
    }
    
    /** 
    *   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();     
              
     }   

}