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
CushtyCushty 

Test class only 60%

Hi,

What am I missing fro this test class to get the % code coverage up.

I am an admin and have the below trigger which after an opportunity is set to won will update the contact role.  Its a bit long winded but what it does is count the type of product thats selected on the opportunity and then update the corresponding checkbox on the contact role once the opportunity is set to won

TRIGGER
trigger UpdateContactProduct on Opportunity (after update) {

   Set<Id> st_OppId = new Set<Id>();
List<Contact> con_List;

for(Opportunity opp :Trigger.new)
  {
     if(opp.StageName=='Won' )
         st_OppId.add(opp.id);
 }

if(st_OppId!=null && !st_OppId.isEmpty())
    {

         for(OpportunityContactRole iterating_oppConRole : [SELECT Role, 
                                                                      OpportunityId, 
                                                                      IsPrimary, 
                                                                      Id, 
                                                                      ContactId,
                                                                      Contact.Lead_Lifecycle_Stage__c ,
                                                                      Contact.X1Edit_bought__c, 
                                                                      Contact.X1Exchange_bought__c,         
                                                                      Opportunity.Count_1Edit__c,
                                                                      Opportunity.Count_1Exchange__c,
                                                                   FROM OpportunityContactRole  where OpportunityId IN : st_OppId])
                               {

                    if(iterating_oppConRole.Opportunity.Count_1Edit__c > 0)
                        {
                              iterating_oppConRole.Contact.X1Edit_bought__c = true;
                        }
                                             
                    if(iterating_oppConRole.Opportunity.Count_1Exchange__c> 0)
                        {
                              iterating_oppConRole.Contact.X1Exchange_bought__c = true;
                        }
                                           if(con_List == null)
                                           con_List = new List<Contact>();

                                          con_List.add(iterating_oppConRole.Contact);
                                }

               
           }
        
        if(con_List!=null && !con_List.isEmpty())
            update con_List;
}

TEST CLASS
@IsTest

Public Class testAddProductsBought
 {
    static testMethod void ValidateContactProduct()
    {
    
    Account a = new Account();
    a.Name ='demo';
    a.Industry = 'Education';
    a.BillingCountry = 'United Kingdom';
    insert a;
    
    
    Contact c = new Contact();
    c.FirstName = 'Lord';
    c.LastName = 'Test2';
    c.Accountid = a.id; 
    c.RecordTypeid = '012w000000063q6AAA';
    insert c;
    
    
    Contact ci = new Contact();
    ci.FirstName = 'Bob';
    ci.LastName  = 'Test';
    ci.AccountId = a.id;
    ci.RecordTypeid = '012w000000063q6AAA';
    insert ci;
    
    
    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.StageName = 'Won';
    o.RecordTypeid = '012w0000000ic3mAAA';
    o.Final_Price_Number__c = 2000;
    o.Business_Unit__c = '1Spatial United Kingdom';
    o.Name = 'Test';
    o.Opportunity_Type__c = 'Contract Renewal';
    O.CurrencyIsoCode ='GBP';
    o.Contract_Type__c = 'Framework';
    o.Estimated_Value_Number__c = 10000;
    o.Opportunity_Submission_Date__c = Date.TODAY();
    o.CloseDate = Date.TODAY() +2;
    o.Probability__c = '80-99%';
    o.Purchase_Order__c = '2L3454';
    o.Solution_Summary__c = 'Hardware';
    o.Description = 'Update Contact role';
    o.Opportunity_Process_Stage__c= 'Opportunity Submitted to Customer: Approved`';
    o.Ownerid = '005w0000004ExSiAAK';
    insert o;
    
        
     OpportunityContactRole cr = new OpportunityContactRole();
     cr.Opportunityid = o.id;
     cr.Role = 'Administrator';
     cr.Contactid = c.id;
     cr.IsPrimary = true;
     insert cr;
     
     OpportunityContactRole ocr1 = new OpportunityContactRole();
     ocr1.ContactId = ci.Id;
     ocr1.OpportunityId = o.Id;
     ocr1.IsPrimary = FALSE;
     ocr1.Role = 'Decision Maker';
     insert ocr1;
     
    
     Id pricebookId = Test.getStandardPricebookId();
     Product2 prod = new Product2();
     prod.Name = 'Product 2';
     prod.ProductCode = 'Pro-2';
     prod.isActive = true;
     prod.Revenue_Type__c = 'Services';
     prod.Product_Heading__c = '1Edit';
     insert prod;

     PricebookEntry pbEntry = new PricebookEntry();
     pbEntry.Pricebook2Id = pricebookId;
     pbEntry.Product2Id = prod.Id;
     pbEntry.UnitPrice = 100.00;
     pbEntry.IsActive = true;
     insert pbEntry;

     OpportunityLineItem oli = new OpportunityLineItem();
     oli.OpportunityId = o.Id;
     oli.quantity = 1;
     oli.PricebookEntryId = pbEntry.Id;
     oli.TotalPrice = oli.quantity * pbEntry.UnitPrice;
     insert oli;

    o.StageName = 'Won';
    update o;
    update cr;

}
}
Best Answer chosen by Cushty
AnjunaAnjuna
Hi Cushty,
You haven't specified the field values for Count_1Edit__c,Count_1Exchange__c in your test class. It will cover the class only if the value of these fields are greater than 0. So update these fields with values greater than zero while inserting opportunity record.
 

All Answers

riaz sfriaz sf
Please Login into the Developer Console and Check which Line is not covered, In other words it is red .
Try to cover those red lines through your Test Classes.

Hope this will help.
AnjunaAnjuna
Hi Cushty,
You haven't specified the field values for Count_1Edit__c,Count_1Exchange__c in your test class. It will cover the class only if the value of these fields are greater than 0. So update these fields with values greater than zero while inserting opportunity record.
 
This was selected as the best answer
CushtyCushty
Done and 100%
Thanks
I never kw that about the Developer Console being a newby Admin, learnt somethign new again!
 
riaz sfriaz sf
welcome @Anjuna
AnjunaAnjuna
@riaz Thank you : )