• riaz sf
  • NEWBIE
  • 5 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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;

}
}
  • February 22, 2017
  • Like
  • 0