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
MAITREYEE DINGAREMAITREYEE DINGARE 

0% test coverage for trigger to insert record

Hello All,
I am new to development. I have written a trigger to add a newly created product into the standard price book. But while deployment to PROD, it is failing because of 0% code coverage.
Could you please help me here?
Following is my trigger:
trigger createPriceBookEntry on Product2 (after insert) {
    
    List<priceBookEntry> priceBookEntryList= new List<priceBookEntry>();
    
    String pricebkId= [SELECT id from Pricebook2 where isStandard=true][0].Id;
    
    for(Product2 prod: trigger.new){      
        priceBookEntry priceBkEntry= new priceBookEntry();
        priceBkEntry.Product2Id= prod.Id;
        priceBkEntry.Pricebook2Id= pricebkId;
        priceBkEntry.IsActive= True;
        priceBkEntry.UnitPrice= 0; // Dummy Value
        priceBkEntry.UseStandardPrice= False;
        priceBkEntry.External_ID__c = prod.MSOFT_Artikelnummer__c;
        //Add more fields as per requirement
        
        priceBookEntryList.add(priceBkEntry);
    }
    
    if(priceBookEntryList.size()>0)
    {
        insert priceBookEntryList;
    }
}
Following is the test class:
@isTest
public class TestQuoteLineItem
{  
    Static testmethod void insertRecord()
   	{
        priceBookEntry priceBkEntry= new priceBookEntry();
       
        priceBkEntry.Pricebook2Id= '01s09000003qN9eAAE';
        priceBkEntry.IsActive= True;
        priceBkEntry.UnitPrice= 0; // Dummy Value
        priceBkEntry.UseStandardPrice= False;
        priceBkEntry.External_ID__c = 'test';
        priceBkEntry.Product2Id = '01t1l000005CY2aAAG';
        //Add more fields as per requirement
            
        insert priceBkEntry;
       
       //system.assertequals(priceBkEntry.Pricebook2Id,priceBkEntry.Pricebook2Id);
       //System.debug('My debug message: ' + priceBkEntry.Product2Id);
   }
}
Thank you so much for your help in advance!
Best Answer chosen by MAITREYEE DINGARE
ravi soniravi soni
Hi ,
try following test class with 100%  code coverage.
@isTest
public class createPriceBookEntryTest {
    @isTest
    static void test(){
        List<Product2> prodList=new List<Product2>();
        prodList.add(new Product2( Name='Test 1', IsActive=true,MSOFT_Artikelnummer__c = 'test 1'));
        prodList.add(new Product2( Name='Test 2', IsActive=true,MSOFT_Artikelnummer__c = 'test 2'));
        
        // get Pricebook2 Records
        Pricebook2 standardPricebook = new Pricebook2(
            Id = Test.getStandardPricebookId(),
            IsActive = true  );
        Update standardPricebook;
        
        Test.startTest();
        if(prodList.size() > 0){
            insert prodList;   
        }
        Test.stopTest();
        
        
        System.assertEquals(2,prodList.size());
        
        
    }
    
}

let me know if it helps you and don't forget to marking it as best so that it can help to others in future.
Thank you

All Answers

Malika Pathak 9Malika Pathak 9
Hi MAITREYEE,

Greetings!
trigger ProductTrigger on Product2 (after insert) {
    if(Trigger.isAfter && Trigger.isInsert){
        
        List<PricebookEntry> priceBookEntryList= new List<PricebookEntry>();
        String pricebkId= [SELECT id from Pricebook2 where isStandard=true][0].Id;
        
        if(Test.isRunningTest()){
           pricebkId = Test.getStandardPricebookId();
        }
        
        
        for(Product2 prod: trigger.new){      
            priceBookEntry priceBkEntry= new priceBookEntry();
            priceBkEntry.Product2Id= prod.Id;
            priceBkEntry.Pricebook2Id= pricebkId;
            priceBkEntry.IsActive= True;
            priceBkEntry.UnitPrice= 1; // Dummy Value
            priceBkEntry.UseStandardPrice= False;
            //priceBkEntry.External_ID__c = prod.MSOFT_Artikelnummer__c;
            //Add more fields as per requirement
            
            priceBookEntryList.add(priceBkEntry);
        }
        
        if(!priceBookEntryList.isEmpty())
        {
            insert priceBookEntryList;
        }
    }
    
}


Test Class
@isTest
public class TestProductTrigger {
    @isTest
    static void test(){
        List<Product2> prodList=new List<Product2>();
        prodList.add(new Product2( Name='Test 1', IsActive=true));
        prodList.add(new Product2( Name='Test 2', IsActive=true));
        
        
        Test.startTest();
        if(prodList.isEmpty()){
            insert prodList;   
        }
        Test.stopTest();

        System.assertEquals(2,[SELECT Id FROM priceBookEntry].size());
        
        
    }

}



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

Thank you!

Regards,
Malika Pathak
ravi soniravi soni
Hi ,
try following test class with 100%  code coverage.
@isTest
public class createPriceBookEntryTest {
    @isTest
    static void test(){
        List<Product2> prodList=new List<Product2>();
        prodList.add(new Product2( Name='Test 1', IsActive=true,MSOFT_Artikelnummer__c = 'test 1'));
        prodList.add(new Product2( Name='Test 2', IsActive=true,MSOFT_Artikelnummer__c = 'test 2'));
        
        // get Pricebook2 Records
        Pricebook2 standardPricebook = new Pricebook2(
            Id = Test.getStandardPricebookId(),
            IsActive = true  );
        Update standardPricebook;
        
        Test.startTest();
        if(prodList.size() > 0){
            insert prodList;   
        }
        Test.stopTest();
        
        
        System.assertEquals(2,prodList.size());
        
        
    }
    
}

let me know if it helps you and don't forget to marking it as best so that it can help to others in future.
Thank you
This was selected as the best answer
MAITREYEE DINGAREMAITREYEE DINGARE
Hello Malika and Veer, 
Thank you for your help. Veer's solution helped me. Now I have 100% test coverage. 
Have a nice day. 
Thank you!
KOUSHIK SARKARKOUSHIK SARKAR
Thank you for sharing an interesting post. IPL Status Video (https://pagalstatus.com/ipl-status-video.html)
Keyur VermaKeyur Verma
Thank you for providing this informative article. check also WhatsApp Status Video Download HD (https://www.thekipkopbeats.com/status/category/whatsapp-status-video-download/)