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
Mahesh Babu 187Mahesh Babu 187 

Test Classes Failing due to a custom validation rule

Hi Team,

My test classes are failing due to a custom validation rule.

VALIDATION RULE:
(Price_Book_Exist__c = false) && NOT(ISNEW())

I am getting below error: 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Deactivate Product if changed” process failed. Give your Salesforce admin these details. This error occurred when the flow tried to update records: FIELD_CUSTOM_VALIDATION_EXCEPTION: Custom Price book must be added to the product to proceed further. You can look up ExceptionCode values in the SOAP API Developer Guide.: []

Please find my one APEX CLASS and it's TEST CLASS.

APEX CLASS:

global with sharing class CreatePriceBookClass {
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
    
    public static void createPriceBook(Product2 product){
        List<Pricebook2> pbs = [SELECT Id,IsStandard FROM Pricebook2 WHERE IsStandard = TRUE];
        
        for(Pricebook2 pb : pbs) { PriceBookEntry pbe1 = new PriceBookEntry();
            pbe1.Pricebook2Id = pb.Id;
            pbe1.Product2Id = product.Id;
            pbe1.UnitPrice = 0;
            pbe1.CurrencyIsoCode = product.CurrencyIsoCode;
            pbe1.IsActive = true;
            insert pbe1;
        }
        List<Pricebook2> pb2s = new List<Pricebook2>();
        if(product.CurrencyIsoCode!= null && product.CurrencyIsoCode.indexOf('USD') >= 0){
            pb2s = [SELECT Id,IsStandard FROM Pricebook2 WHERE Name = 'USD'];
        } else if(product.CurrencyIsoCode!= null && product.CurrencyIsoCode.indexOf('CAD') >= 0){
            pb2s = [SELECT Id,IsStandard FROM Pricebook2 WHERE Name = 'CAD'];
        } else if(product.CurrencyIsoCode!= null && product.CurrencyIsoCode.indexOf('GBP') >= 0){
            pb2s = [SELECT Id,IsStandard FROM Pricebook2 WHERE Name = 'GBP'];
        }
        for(Pricebook2 pb2 : pb2s) {
            PriceBookEntry pbe2 = new PriceBookEntry();
            pbe2.Pricebook2Id = pb2.Id;
            pbe2.Product2Id = product.Id;
            pbe2.UnitPrice = 0;
            pbe2.CurrencyIsoCode = product.CurrencyIsoCode;
            pbe2.IsActive = true;
            pbe2.UseStandardPrice = false;
            insert pbe2;
        }
    }
}

TEST CLASS:

@isTest(seeAllData=false)
public class CreatePriceBookClassTest {

    @TestSetup 
    static void doSetup(){
        TestDataUtility.createTriggerFlag();        

        List<Account> accs = TestDataUtility.createAccounts(3);
        accs[1].vertical__c = 'Mortgage';
        accs[2].vertical__c = 'Personal Loans';
        update accs;
        
        List<Product2> prods = TestDataUtility.createProducts(1,accs[0].Id);
        Id pricebookId  =Test.getStandardPricebookId();
       }
    
    @isTest
    public static void basicTest(){
        List<Product2> prods = [SELECT Id,Lightbox_Pricing__c,CurrencyIsoCode FROM Product2];
       
        for(Product2 prod : prods) {
            Id pricebookId  =Test.getStandardPricebookId();
            PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = prod.Id;
            standardPrice.UnitPrice = 1; 
            standardPrice.IsActive = true;
            standardPrice.UseStandardPrice = false;
            insert standardPrice;
            
            Pricebook2 customPB = new Pricebook2();
            customPB.Name='Custom Pricebook';
            customPB.isActive=true;
            insert customPB;
            
            Pricebook2 customPB1 = new Pricebook2();
            customPB1.Name='USD';
            customPB1.isActive=true;
            insert customPB1;
            
            PriceBookEntry pbe = new PriceBookEntry();
            pbe.Pricebook2Id = customPB.Id;
            pbe.Product2Id = prod.Id;
            pbe.UnitPrice = 0;
            pbe.UseStandardPrice = false;
            pbe.CurrencyIsoCode = prod.CurrencyIsoCode;
            pbe.IsActive = true;
            insert pbe;
        
        
            prod.Activated_End_Date__c = System.today();
            update prod;
            CreatePriceBookClass.createPriceBook(prod);
        }
    }
    
    @isTest
    public static void basicCADTest(){
        List<Product2> prods = [SELECT Id,Lightbox_Pricing__c,CurrencyIsoCode FROM Product2];
       
        for(Product2 prod : prods) {
            prod.CurrencyIsoCode = 'CAD';
            Id pricebookId  =Test.getStandardPricebookId();
            PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = prod.Id;
            standardPrice.UnitPrice = 1; 
            standardPrice.IsActive = true;
            standardPrice.UseStandardPrice = false;
            standardPrice.CurrencyIsoCode = prod.CurrencyIsoCode;
            insert standardPrice;
            
            Pricebook2 customPB = new Pricebook2();
            customPB.Name='Custom Pricebook';
            customPB.isActive=true;
            insert customPB;
            
            Pricebook2 customPB1 = new Pricebook2();
            customPB1.Name='CAD';
            customPB1.isActive=true;
            insert customPB1;
            
            PriceBookEntry pbe = new PriceBookEntry();
            pbe.Pricebook2Id = customPB.Id;
            pbe.Product2Id = prod.Id;
            pbe.UnitPrice = 0;
            pbe.UseStandardPrice = false;
            pbe.CurrencyIsoCode = prod.CurrencyIsoCode;
            pbe.IsActive = true;
            insert pbe;
        
        
            prod.Activated_End_Date__c = System.today();
            update prod;
            CreatePriceBookClass.createPriceBook(prod);
        }
    }
    
    @isTest
    public static void basicGBPTest(){
        List<Product2> prods = [SELECT Id,Lightbox_Pricing__c,CurrencyIsoCode FROM Product2];
       
        for(Product2 prod : prods) {
            prod.CurrencyIsoCode = 'GBP';
            Id pricebookId  =Test.getStandardPricebookId();
            PricebookEntry standardPrice = new PricebookEntry();
            standardPrice.Pricebook2Id = pricebookId;
            standardPrice.Product2Id = prod.Id;
            standardPrice.UnitPrice = 1; 
            standardPrice.IsActive = true;
            standardPrice.UseStandardPrice = false;
            standardPrice.CurrencyIsoCode = prod.CurrencyIsoCode;
            insert standardPrice;
            
            Pricebook2 customPB = new Pricebook2();
            customPB.Name='Custom Pricebook';
            customPB.isActive=true;
            insert customPB;
            
            Pricebook2 customPB1 = new Pricebook2();
            customPB1.Name='GBP';
            customPB1.isActive=true;
            insert customPB1;
            
            PriceBookEntry pbe = new PriceBookEntry();
            pbe.Pricebook2Id = customPB.Id;
            pbe.Product2Id = prod.Id;
            pbe.UnitPrice = 0;
            pbe.UseStandardPrice = false;
            pbe.CurrencyIsoCode = prod.CurrencyIsoCode;
            pbe.IsActive = true;
            insert pbe;
        
        
            prod.Activated_End_Date__c = System.today();
            update prod;
            CreatePriceBookClass.createPriceBook(prod);
        }
    }
 
}

Please help in solving this issue.

Thank You,
Mahesh
ShirishaShirisha (Salesforce Developers) 
Hi Mahesh,

Greetings!

I can see that you have already created the new test record of PriceBook2 Object in the test class.However,I would suggest you to meet all the criteria in validation rule and the process/flow which is causing the issue here.

In order to troubleshoot further,please capture the debug logs which will help you to narrow down the code.

Reference:https://help.salesforce.com/articleView?id=code_add_users_debug_log.htm&type=5

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri