+ Start a Discussion
Scott Janis 6Scott Janis 6 

I need help with a simple trigger in which I can't seem to get coverage for.

This is the trigger :
Trigger CreatingAutoRecords on OrderItem(After Insert, After Update)
{
    
    List<Screens__c> ScreensRecordsFinalListToInsert = New List<Screens__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(OrderItem con : Trigger.New)
        {
            If(con.Quantity != null)
            {
                List<Screens__c> fetchingAlreadyExixtsedRecords = [Select Id FROM Screens__c WHERE  Order_Product__c =:con.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no Screen Record records exixts.
                    For(Integer I = 0; I < con.Quantity; I++)
                    {
                        Screens__c crd = New Screens__c();
                        
                        crd.Order_Product__c = con.Id;
                          crd.Name = 'Screen';
                          crd.Account__c = con.Account__c;
                   
                        ScreensRecordsFinalListToInsert.add(crd);
                    }
                    
                   
                }
                
            }
            
            try{
                If(!ScreensRecordsFinalListToInsert.IsEmpty()){
                    insert ScreensRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    }

This is the test class:
@isTest

private class CreateAutoRecordsTest {
    @istest
    private static void insertRecord()


{




List<Screens__c> ScreensRecordsFinalListToInsert = New List<Screens__c>();

    
                   
                 Screens__c crd = New Screens__c();
                        
                          crd.Name = 'Screen';
                          
                           ScreensRecordsFinalListToInsert.add(crd);
                          
                            insert ScreensRecordsFinalListToInsert;
   
   } 
   }


I deploy them together. Then I run try to validate in production and I keep getting a code coverage error. 

 
Best Answer chosen by Scott Janis 6
Rahul.MishraRahul.Mishra
Hi Scott,

Please use the following test class:
 
@isTest(seeAllData = true)
private class TestTotalPiecesSum {
    
    static testMethod void myUnitTest() {
        
        //Test Account Insert
        
        Account a = new Account();
        a.Name = 'Test Account';
        insert a;
        
        Product2 p = new Product2();
        p.Name = ' Test Product ';
        p.Description='Test Product Entry 1';
        p.productCode = 'ABC';
        p.isActive = true;
        insert p;
        
        
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = standardPb.Id;
        standardPrice.Product2Id = p.Id;
        standardPrice.UnitPrice = 1;
        standardPrice.IsActive = true;
        standardPrice.UseStandardPrice = false;
        insert standardPrice ;
        
        //Test Order Insert
        
        Order o = new Order();
        o.Name = 'Test Order ';
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        o.EndDate = system.today() + 4;
        o.AccountId = a.id;
        o.Pricebook2Id =  standardPb.Id ;
        
        insert o;

        
        OrderItem i = new OrderItem();
        i.OrderId = o.id;
        i.Quantity = 24;
        i.UnitPrice = 240;
        i.Product2id = p.id;
        i.PricebookEntryId=standardPrice.id;
        insert i;
        
        
        List<Screens__c> ScreensRecordsFinalListToInsert = New List<Screens__c>();
        
        Screens__c crd1 = New Screens__c();
        
        crd1.Name = 'Screen';
        
        ScreensRecordsFinalListToInsert.add(crd1);
        
        
        Screens__c crd2 = New Screens__c();
        
        crd.Name = 'Screen';
        crd.Order_Product__c  = i.Id;
        
        ScreensRecordsFinalListToInsert.add(crd2);
        
        insert ScreensRecordsFinalListToInsert;

Please mark my answer as best if it does solves your problem. 

All Answers

Rahul.MishraRahul.Mishra
Hi Scott,

Please use the following test class:
 
@isTest(seeAllData = true)
private class TestTotalPiecesSum {
    
    static testMethod void myUnitTest() {
        
        //Test Account Insert
        
        Account a = new Account();
        a.Name = 'Test Account';
        insert a;
        
        Product2 p = new Product2();
        p.Name = ' Test Product ';
        p.Description='Test Product Entry 1';
        p.productCode = 'ABC';
        p.isActive = true;
        insert p;
        
        
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
        
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = standardPb.Id;
        standardPrice.Product2Id = p.Id;
        standardPrice.UnitPrice = 1;
        standardPrice.IsActive = true;
        standardPrice.UseStandardPrice = false;
        insert standardPrice ;
        
        //Test Order Insert
        
        Order o = new Order();
        o.Name = 'Test Order ';
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        o.EndDate = system.today() + 4;
        o.AccountId = a.id;
        o.Pricebook2Id =  standardPb.Id ;
        
        insert o;

        
        OrderItem i = new OrderItem();
        i.OrderId = o.id;
        i.Quantity = 24;
        i.UnitPrice = 240;
        i.Product2id = p.id;
        i.PricebookEntryId=standardPrice.id;
        insert i;
        
        
        List<Screens__c> ScreensRecordsFinalListToInsert = New List<Screens__c>();
        
        Screens__c crd1 = New Screens__c();
        
        crd1.Name = 'Screen';
        
        ScreensRecordsFinalListToInsert.add(crd1);
        
        
        Screens__c crd2 = New Screens__c();
        
        crd.Name = 'Screen';
        crd.Order_Product__c  = i.Id;
        
        ScreensRecordsFinalListToInsert.add(crd2);
        
        insert ScreensRecordsFinalListToInsert;

Please mark my answer as best if it does solves your problem. 
This was selected as the best answer
Scott Janis 6Scott Janis 6
Thank you!!!!! Worked