+ Start a Discussion
shakila Gshakila G 

Code coverage issue?

Hi,

I wrote a trigger to update the Project Item from Project (Parent to Child update.)

For the same, I wrote a test class also. I covered all the things in test class even though my code coverage is 50% only.

Kindly let me know where am doing the mistake.


Trigger:

trigger OI_Update_ProjItem_Status on Project__c (after update) {
    
    set<id>setprojid = new set<id>();
    list<Sale_Products__c>lstsp = new list<Sale_Products__c>();
    list<Project_Item__c>lstpi = new list<Project_Item__c>();
    
    for(Project__c proj : Trigger.New){
        if((proj.Nature_of_Case__c == 'Project Complete / Delivered - Payment Due' || proj.Nature_of_Case__c == 'Project Completed - Payment Recieved') && (proj.Project_Type__c.Contains('Indoor')
            || proj.Project_Type__c.Contains('Furniture'))){
             setprojid.add(proj.id);
        }
            
    }
    if(setprojid.size() > 0){
     
        for(Project_Item__c pi : [SELECT id, Status__c FROM Project_Item__c WHERE Project__c IN :setprojid]){
            pi.Status__c = 'Delivered';
            lstpi.add(pi);
        } 
           
        for(Sale_Products__c sp : [SELECT id, Status__c , Work_Order__c FROM Sale_Products__c WHERE Work_Order__c IN : setprojid]){
            sp.Status__c = 'Delivered';
            lstsp.add(sp);
        } 
   } 
   if(lstpi.size() > 0){
       update lstpi;
   } 
   if(lstsp.size() > 0){
       update lstsp;
   }            
}

Test Class:

@isTest(seealldata=true)
Public class TestWorkorder{
    static testMethod void TestWorkorder(){
    
       Test.startTest();  
        
        
        
        
             Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
             
            User u = new User(Alias = 'Test', Email='Test@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='xyz@gmail.com');
            
            insert u;
            
            
        
       Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
      insert pb;
    
    
        Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
        insert prod;
    
    
// First insert a price for the standard price book

            Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
            
            PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
                insert standardPBE;
                
            PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
                insert pbe;
        
        Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=u.ID);
        insert a; 
       
        
        
        Opportunity opp =New Opportunity(Name='Test', accountId = a.id, StageName='Closed Won',CloseDate=Date.newInstance(2018, 04, 30));
        insert Opp; 
        
       
            
             Contact con = new COntact();
            con.email='test@fmail.co';
            con.ACCOUNTID=a.ID;
            con.ownerid=a.ownerid;
            con.lastname='test' ;
            
            insert con;
                         
            
        
       
           
            
        
        Opp.Pricebook2Id = pb.id;
        update Opp;
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp.id, Quantity=1, unitprice=8000,  
            pricebookentryid=pbe.id);
        insert OL;  
            
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp.id; 
        Q.Quote_Count__c = 00000000;
        Q.Approved__c = true;
        Q.Quote_Number__c ='000000';
        Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = pb .id;
        
        insert Q;
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id, Quantity =1, Unitprice=8000,pricebookentryid=pbe.id);
        insert QL; 
        
        Opp.SyncedQuoteId = Q.id;
        update Opp;
        
        
         
        Project__c Proj = new Project__c(name='OrbitProj', Opportunity__c = Opp.id, Quote__c = Q.id, Project_Type__c = 'New PE', Date_of_Installation__c = date.newinstance(2013,4,5),
            Nature_of_Case__c ='No Adv, Dspch & Erection Pending',Contact__C=con.ID,Customer_Name__c=a.id,Special_Instructions__c ='Test for Work order Pdf to be generated');    
            
        insert Proj;  
        
        Proj .Nature_of_Case__c = 'Project Completed - Payment Recieved';
        Proj .Nature_of_Case__c ='Project Complete / Delivered - Payment Due';
        Proj .Despatch_Date__c=Date.newInstance(2018, 04, 30);
        Proj .Delivery_Date__c=Date.newInstance(2018, 04, 30);
        
         update Proj ;
        
        
        
       
                
       
        
       
                
        Project_Item__c pi = new Project_Item__c(Project__c = Proj .id,Status__c = 'Pending',Product__c = prod.id,
                                 Technician_Member__c = 'suren; lakshmanan');
        insert pi;
        
        Project_Item__c pitm=[select ID, Status__c  from Project_Item__c  where ID=:Pi.ID];
        
        
        
        pitm.Status__c = 'Delivered' ;      
        
        update pitm;    
       
                
     
         
        
        Product_Inventory__c PInv=new Product_Inventory__c  (Product__c=prod.id);
        insert PInv;
        
        
     
        
        Sale_Products__c sp = new Sale_Products__c  (Status__c ='pending' ,Work_Order__c=Proj .id,Product_Inventory__c=PInv.ID,Quantity__c=1);
        insert sp ;
        
        Sale_Products__c Spr= [select Status__c  from Sale_Products__c  where ID=:Sp.id];
        
        Spr.Status__c ='Delivered';    
        
        update Spr;
        
       
        
         Test.stopTest();
 
        
    }
    
}

 
Best Answer chosen by shakila G
Raj R.Raj R.
Your trigger is based on after a Project__c  record is updated. However you only update Proj variable one time in your test class and it is down before you insert and update a Sales_Products__c record.
 
Sale_Products__c sp = new Sale_Products__c  (Status__c ='pending' ,Work_Order__c=Proj .id,Product_Inventory__c=PInv.ID,Quantity__c=1);
        insert sp ;
        
        Sale_Products__c Spr= [select Status__c  from Sale_Products__c  where ID=:Sp.id];
        
        Spr.Status__c ='Delivered';    
        
        update Spr;

// After this but before Test.stopTest() you need to update the Proj or another Project__c record to cause the trigger to fire after. See example

Proj.Project_Type__c = 'Indoor';

update Proj;

Test.stopTest();

 

All Answers

Raj R.Raj R.
Your trigger is based on after a Project__c  record is updated. However you only update Proj variable one time in your test class and it is down before you insert and update a Sales_Products__c record.
 
Sale_Products__c sp = new Sale_Products__c  (Status__c ='pending' ,Work_Order__c=Proj .id,Product_Inventory__c=PInv.ID,Quantity__c=1);
        insert sp ;
        
        Sale_Products__c Spr= [select Status__c  from Sale_Products__c  where ID=:Sp.id];
        
        Spr.Status__c ='Delivered';    
        
        update Spr;

// After this but before Test.stopTest() you need to update the Proj or another Project__c record to cause the trigger to fire after. See example

Proj.Project_Type__c = 'Indoor';

update Proj;

Test.stopTest();

 
This was selected as the best answer
Sampath SuranjiSampath Suranji
Hi Shakila,
Please check below,
@isTest(seealldata=true)
Public class TestWorkorder{
    static testMethod void TestWorkorder(){
        
        Test.startTest();  
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
        User objUser = new User(Alias = 'Test', Email='Test1@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='user123b@gmail.com');
        
        insert objUser;
        
        
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
        insert pb;
        
        
        Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
        insert prod;
        
        
        // First insert a price for the standard price book
        
        Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
        
        PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
        insert standardPBE;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
        insert pbe;
        
        Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=objUser.ID);
        insert a;
        
        Opportunity opp =New Opportunity(Name='Test', accountId = a.id, StageName='Closed Won',CloseDate=Date.newInstance(2018, 04, 30));
        insert Opp;
        
        Contact con = new COntact();
        con.email='test@fmail.co';
        con.ACCOUNTID=a.ID;
        con.ownerid=a.ownerid;
        con.lastname='test' ;
        
        insert con;
        
        Opp.Pricebook2Id = pb.id;
        update Opp;
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp.id, Quantity=1, unitprice=8000,  
                                                         pricebookentryid=pbe.id);
        insert OL;  
        
        Quote Q = new Quote();
        Q.Name = 'QuoteOrbit';
        Q.OpportunityId = Opp.id; 
        // Q.Quote_Count__c = 00000000;
        // Q.Approved__c = true;
        // Q.Quote_Number__c ='000000';
        // Q.Quotation_Date__c = date.newinstance(2013,3,5);
        Q.Pricebook2Id = pb .id;
        
        insert Q;
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id, Quantity =1, Unitprice=8000,pricebookentryid=pbe.id);
        insert QL; 
        
        Opp.SyncedQuoteId = Q.id;
        update Opp;
        
        
        
        Project__c Proj = new Project__c(name='OrbitProj', Opportunity__c = Opp.id, Quote__c = Q.id, Project_Type__c = 'indoor',
                                         Nature_of_Case__c ='No Adv, Dspch & Erection Pending',Contact__C=con.ID,Customer_Name__c=a.id);    
        
        insert Proj;  
        
        Project_Item__c pi = new Project_Item__c(Project__c = Proj .id,Status__c = 'Pending' );
        insert pi;
        
        Sale_Products__c sp = new Sale_Products__c  (Status__c ='pending', Work_Order__c=Proj .id  );
        insert sp ;
        
        // Proj .Nature_of_Case__c = 'Project Completed - Payment Recieved';
        Proj .Nature_of_Case__c ='Project Complete / Delivered - Payment Due';
        //Proj .Despatch_Date__c=Date.newInstance(2018, 04, 30);
        // Proj .Delivery_Date__c=Date.newInstance(2018, 04, 30);
        
        update Proj ;
                
        Test.stopTest();
        
        
    }
    
}

Best Regards
Raj R.Raj R.
As a best practice, don't use seealldata=true.

Instead create a DataFactory class or something similar per application. 

Example and helpful links
  1. https://github.com/rajruprai/sfdc-aws-sign4/blob/master/classes/DataFactoryAws.cls
  2. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm