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
Gopal ChatGopal Chat 

In my trigger testing the test is not going inside if trigger.new

trigger Setpricebookentry1 on Product2 (after insert) 
{
    Set<ID> prodIdSet = Trigger.newMap.keySet();
    list<Pricebookentry> listpbe=new list<Pricebookentry>();
    Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
         system.debug('++++++>'+prbook);
        for(Product2 po:Trigger.New)
        {
            system.debug('=====>'+trigger.new);
            Pricebookentry pbe=new Pricebookentry();
            pbe.UnitPrice=10;
            pbe.Pricebook2Id=prbook.id;
            pbe.Product2Id=po.id;
            listpbe.add(pbe);
        }
        if(listpbe.size()>0)
        {
            insert listpbe;
            system.debug('------>'+listpbe);
        }
}   

this is my test code
@istest
public class TestSetpricebookentry1 
{
    @istest
    public static void mytest()
    {
        Product2 po=new Product2();
        po.Name='Raghav';
        po.IsActive=true;
        insert po;
        Id pricebookId = Test.getStandardPricebookId();
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = pricebookId; 
        standardPrice.Product2Id =po.Id;
        standardPrice.UnitPrice = 10000; 
        standardPrice.IsActive = true;
        insert standardPrice;
    }
}
Best Answer chosen by Gopal Chat
Amit Chaudhary 8Amit Chaudhary 8
Hi Raghav Chaturvedi,

Issue is coming because of below code in your Trigger.
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];

You need to create Pricebook2 record in your test class. Dnt use SeeAllData = true in your test class.

Update your code like below
@istest
public class TestSetpricebookentry1 
{
    @istest
    public static void mytest()
    {
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 customPB = new Pricebook2(id=pricebookId , isActive=true);
        update customPB ;
    
        Product2 po=new Product2();
        po.Name='Raghav';
        po.IsActive=true;
        insert po;
        
    }
}

Let us know if this will help you
 

All Answers

Raj VakatiRaj Vakati
test.startTest() and test.stopTest() should be there .please use the below code 

 
@isTest (seeAllData=true)
public class TestSetpricebookentry1 
{
    @istest
    public static void mytest()
    {
        Test.startTest();
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 producto =new Product2();
        producto.Name='test';
        producto.productCode='1234';
        producto.isActive = true;
        insert producto;
        
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = pricebookId; 
        standardPrice.Product2Id =producto.Id;
        standardPrice.UnitPrice = 10000; 
        standardPrice.IsActive = true;
        insert standardPrice;
        
        Test.stopTest();
        
    }
    
    public static PricebookEntry createPricebookEntry (Pricebook2 standard, Pricebook2 newPricebook, Product2 prod) {
        System.debug('***** starting one');
        PricebookEntry one = new PricebookEntry();
        one.pricebook2Id = standard.id;
        one.product2id = prod.id;
        one.unitprice = 1249.0;
        one.isactive = true;
        insert one;
        System.debug('***** one complete, ret next');
        PricebookEntry ret = new PricebookEntry();
        ret.pricebook2Id = newPricebook.id;
        ret.product2id = prod.id;
        ret.unitprice = 1250.0;
        ret.isactive = true;
        insert ret;
        return ret;
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Raghav Chaturvedi,

Issue is coming because of below code in your Trigger.
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];

You need to create Pricebook2 record in your test class. Dnt use SeeAllData = true in your test class.

Update your code like below
@istest
public class TestSetpricebookentry1 
{
    @istest
    public static void mytest()
    {
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 customPB = new Pricebook2(id=pricebookId , isActive=true);
        update customPB ;
    
        Product2 po=new Product2();
        po.Name='Raghav';
        po.IsActive=true;
        insert po;
        
    }
}

Let us know if this will help you
 
This was selected as the best answer