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
Adam Lee 60Adam Lee 60 

Help needed for test coverage for Trigger but I think it's something simple!

HI All.

I'm only getting 70% coverage and I'm not sure how to cover the variables.

These are the lines not covered.

     Line 16 qty = o.Total_Roundtables__c;
            17 orderId = o.Id;
            18 eventId = o.Event__c;
            19 eventName = o.Event__r.Name;

            31 r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
            32 rList.add(r);

Can anyone help?

Here's my trigger
trigger CreateRoundtables on Order (after insert, after update) {

    public double qty;
    public id orderId;
    public id eventId;
    public string eventName;


    
    Id orderRecType = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Order').getRecordTypeId(); 
    
    List <OrderItem> oi = [Select id, Roundtables__c from OrderItem where Roundtables__c>0 ];
    system.debug(oi);
    for(order o:trigger.new){
        if(o.BU__c=='MTB' && o.Total_Roundtables__c >= 1 && O.Attendee_Type__c =='Sponsor' && o.Status == 'Approved'){
         	qty = o.Total_Roundtables__c;
            orderId = o.Id;
            eventId = o.Event__c;
            eventName = o.Event__r.Name;
            system.debug('Order ID '+ orderId);
            system.debug('Event Name '+ eventName);
        }
    }
    
    List <Roundtables__c> curRT = [Select id, Order__c from Roundtables__c where Order__c =: orderId];
    List <Roundtables__c> rList = new list <Roundtables__c>();
    Roundtables__c r;
    
    if(curRT.size() <> qty){
    for(Integer m=1;m<=qty;m++){
        r=new Roundtables__c (Order__c=orderId, Status__c ='Unallocated', Event__c = eventId);
        rList.add(r);
    }
    }
	insert rList;
}

Here's the test class
@istest (SeeAllData = true) 
public class CreateRoundTableTEST {
	
    static testMethod void TESTcreateRoundTables(){
        
        //Insert Test Accounts
		Account acc = new Account();
        acc.Type='Other';
        acc.name='test ';
        acc.NumberOfEmployees=10;
        acc.BillingStreet='BillingStreet'; 
        acc.BillingCity = 'BillingCity';
        acc.BillingState = 'BillingState';
        acc.BillingCountry = 'BillingCountry'; 
        acc.BillingPostalCode = 'BillingPostalCode';
        acc.NumberOfEmployees = 50; 
        insert acc;
        
        Account acc2 = new Account();
        acc2.Type='AX Company';
        acc2.name='test 1';
        acc2.BillingStreet='BillingStreet1'; 
        acc2.BillingCity = 'BillingCity';
        acc2.BillingState = 'BillingState';
        acc2.BillingCountry = 'BillingCountry'; 
        acc2.BillingPostalCode = 'BillingPostalCode';
        acc2.NumberOfEmployees = 50; 
        insert acc2;
   
        Project__c testProject = new Project__c();
        testProject.name = 'test Project';
        testProject.Project_Code__c = 'SUM123';
        insert testProject;
   
        //Insert Test Event
        Event__c testEvent = new Event__c();
        testEvent.name='testEvent';
        testEvent.Start_Date__c =system.today();
        testEvent.End_Date__c=system.today().addDays(2);
        testEvent.name='testEvent';
        testEvent.AX_Company__c = acc2.Id;
        testEvent.project__c = testProject.Id;
        Insert testEvent;
        
        //Insert Test Contacts
        Contact con = new contact();
        con.lastname='test';
        con.accountid=acc.id;
        con.FirstName = 'FirstName';
        con.MailingStreet = 'MailingStreet'; 
        con.MailingCity = 'MailingCity';
        con.MailingState = 'MailingState';
        con.MailingCountry = 'MailingCountry';
        con.MailingPostalCode = 'MailingPostalCode';  
        con.IsOpen4All__c=false;      
        insert con;
        
        Contact con2 = new contact();
        con2.lastname='test';
        con2.accountid=acc.id;
        con2.FirstName = 'FirstName';
        con2.MailingStreet = 'MailingStreet'; 
        con2.MailingCity = 'MailingCity';
        con2.MailingState = 'MailingState';
        con2.MailingCountry = 'MailingCountry';
        con2.MailingPostalCode = 'MailingPostalCode';  
        con2.IsOpen4All__c=false;      
        insert con2;
        
        //insert Test Campaign
        Campaign testCampaign1       = new Campaign();
        testCampaign1.name           = 'TestCampaign';
        testCampaign1.Member_Type__c = 'Delegate';
        testCampaign1.Type = 'Call List';
        insert testCampaign1;

       test.startTest();
         Id standardPriceBookId = Test.getStandardPricebookId();
        order o1 = new order();
        o1.accountid=acc.id;
        o1.EffectiveDate= system.today();
        o1.Attendee_Type__c='Sponsor';
        o1.pricebook2id=standardPriceBookId;
        o1.Primary_Campaign_Source__c=testCampaign1.id;
        o1.Price_List__c='test';
        o1.Status='Draft';
        o1.Sold_On_Date__c= system.today();
        o1.Main_POC__c=con2.id;
        o1.Event__c = testEvent.id;
        insert o1;
        
         Id dealOrderId = [Select Id, DeveloperName FROM RecordType where SobjectType = 'Order' and DeveloperName = 'Deal' limit 1].Id;

        o1.recordtypeid=dealOrderId;
        update o1;

        Product2 pd = new Product2(Name='MTB Prod',isActive=true, Number_of_Roundtables__c =2);
        pd.IsActive = true;
        insert pd;
        
        //Id standardPriceBookId = Test.getStandardPricebookId();
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=o1.Id, Quantity=1, UnitPrice=99);
        insert ordPd;
        o1.status='Approved';
        update o1;
        
        
       test.stopTest();
    }
}


 
Best Answer chosen by Adam Lee 60
hitesh90hitesh90
Hello Adam,

It was because In Order item insertion you haven't given value for Roundtables__c field in your test class. see below updated code for your test class.
Apex Class:
@istest
public class CreateRoundTableTEST {
    static testMethod void TESTcreateRoundTables(){
        //Insert Test Accounts
        Account acc = new Account();
        acc.Type='Other';
        acc.name='test ';
        acc.NumberOfEmployees=10;
        acc.BillingStreet='BillingStreet'; 
        acc.BillingCity = 'BillingCity';
        acc.BillingState = 'BillingState';
        acc.BillingCountry = 'BillingCountry'; 
        acc.BillingPostalCode = 'BillingPostalCode';
        acc.NumberOfEmployees = 50; 
        insert acc;
        
        Account acc2 = new Account();
        acc2.Type='AX Company';
        acc2.name='test 1';
        acc2.BillingStreet='BillingStreet1'; 
        acc2.BillingCity = 'BillingCity';
        acc2.BillingState = 'BillingState';
        acc2.BillingCountry = 'BillingCountry'; 
        acc2.BillingPostalCode = 'BillingPostalCode';
        acc2.NumberOfEmployees = 50; 
        insert acc2;
        
        Project__c testProject = new Project__c();
        testProject.name = 'test Project';
        testProject.Project_Code__c = 'SUM123';
        insert testProject;
        
        //Insert Test Event
        Event__c testEvent = new Event__c();
        testEvent.name='testEvent';
        testEvent.Start_Date__c =system.today();
        testEvent.End_Date__c=system.today().addDays(2);
        testEvent.name='testEvent';
        testEvent.AX_Company__c = acc2.Id;
        testEvent.project__c = testProject.Id;
        Insert testEvent;
        
        //Insert Test Contacts
        Contact con = new contact();
        con.lastname='test';
        con.accountid=acc.id;
        con.FirstName = 'FirstName';
        con.MailingStreet = 'MailingStreet'; 
        con.MailingCity = 'MailingCity';
        con.MailingState = 'MailingState';
        con.MailingCountry = 'MailingCountry';
        con.MailingPostalCode = 'MailingPostalCode';  
        con.IsOpen4All__c=false;      
        insert con;
        
        Contact con2 = new contact();
        con2.lastname='test';
        con2.accountid=acc.id;
        con2.FirstName = 'FirstName';
        con2.MailingStreet = 'MailingStreet'; 
        con2.MailingCity = 'MailingCity';
        con2.MailingState = 'MailingState';
        con2.MailingCountry = 'MailingCountry';
        con2.MailingPostalCode = 'MailingPostalCode';  
        con2.IsOpen4All__c=false;      
        insert con2;
        
        //insert Test Campaign
        Campaign testCampaign1       = new Campaign();
        testCampaign1.name           = 'TestCampaign';
        testCampaign1.Member_Type__c = 'Delegate';
        testCampaign1.Type = 'Call List';
        insert testCampaign1;
        
        test.startTest();
        Id standardPriceBookId = Test.getStandardPricebookId();
        order o1 = new order();
        o1.accountid=acc.id;
        o1.EffectiveDate= system.today();
        o1.Attendee_Type__c='Sponsor';
        o1.pricebook2id=standardPriceBookId;
        o1.Primary_Campaign_Source__c=testCampaign1.id;
        o1.Price_List__c='test';
        o1.Status='Draft';
        o1.Sold_On_Date__c= system.today();
        o1.Main_POC__c=con2.id;
        o1.Event__c = testEvent.id;
        insert o1;
        
        Id dealOrderId = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Deal').getRecordTypeId();
        o1.recordtypeid=dealOrderId;
        update o1;
        
        Product2 pd = new Product2(Name='MTB Prod',isActive=true, Number_of_Roundtables__c =2);
        pd.IsActive = true;
        insert pd;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=o1.Id, Quantity=1, UnitPrice=99, Roundtables__c = 10);
        insert ordPd;
        
        o1.Attendee_Type__c = 'Sponsor';
        o1.BU__c = 'MTB';
        o1.status='Approved';
        update o1;
        test.stopTest();
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 

All Answers

Manish BhatiManish Bhati
for Line 16 qty = o.Total_Roundtables__c;
            17 orderId = o.Id;
            18 eventId = o.Event__c;
            19 eventName = o.Event__r.Name;

Write another method or include another case in the same testMethod such that below condition becomes true:-
  if(o.BU__c=='MTB' && o.Total_Roundtables__c >= 1 && O.Attendee_Type__c =='Sponsor' && o.Status == 'Approved')

same for line 31, 32, make below if condition to true and also quantity should be atleast one.
if(curRT.size() <> qty)
 
hitesh90hitesh90
Hello Adam,

It was because In Order item insertion you haven't given value for Roundtables__c field in your test class. see below updated code for your test class.
Apex Class:
@istest
public class CreateRoundTableTEST {
    static testMethod void TESTcreateRoundTables(){
        //Insert Test Accounts
        Account acc = new Account();
        acc.Type='Other';
        acc.name='test ';
        acc.NumberOfEmployees=10;
        acc.BillingStreet='BillingStreet'; 
        acc.BillingCity = 'BillingCity';
        acc.BillingState = 'BillingState';
        acc.BillingCountry = 'BillingCountry'; 
        acc.BillingPostalCode = 'BillingPostalCode';
        acc.NumberOfEmployees = 50; 
        insert acc;
        
        Account acc2 = new Account();
        acc2.Type='AX Company';
        acc2.name='test 1';
        acc2.BillingStreet='BillingStreet1'; 
        acc2.BillingCity = 'BillingCity';
        acc2.BillingState = 'BillingState';
        acc2.BillingCountry = 'BillingCountry'; 
        acc2.BillingPostalCode = 'BillingPostalCode';
        acc2.NumberOfEmployees = 50; 
        insert acc2;
        
        Project__c testProject = new Project__c();
        testProject.name = 'test Project';
        testProject.Project_Code__c = 'SUM123';
        insert testProject;
        
        //Insert Test Event
        Event__c testEvent = new Event__c();
        testEvent.name='testEvent';
        testEvent.Start_Date__c =system.today();
        testEvent.End_Date__c=system.today().addDays(2);
        testEvent.name='testEvent';
        testEvent.AX_Company__c = acc2.Id;
        testEvent.project__c = testProject.Id;
        Insert testEvent;
        
        //Insert Test Contacts
        Contact con = new contact();
        con.lastname='test';
        con.accountid=acc.id;
        con.FirstName = 'FirstName';
        con.MailingStreet = 'MailingStreet'; 
        con.MailingCity = 'MailingCity';
        con.MailingState = 'MailingState';
        con.MailingCountry = 'MailingCountry';
        con.MailingPostalCode = 'MailingPostalCode';  
        con.IsOpen4All__c=false;      
        insert con;
        
        Contact con2 = new contact();
        con2.lastname='test';
        con2.accountid=acc.id;
        con2.FirstName = 'FirstName';
        con2.MailingStreet = 'MailingStreet'; 
        con2.MailingCity = 'MailingCity';
        con2.MailingState = 'MailingState';
        con2.MailingCountry = 'MailingCountry';
        con2.MailingPostalCode = 'MailingPostalCode';  
        con2.IsOpen4All__c=false;      
        insert con2;
        
        //insert Test Campaign
        Campaign testCampaign1       = new Campaign();
        testCampaign1.name           = 'TestCampaign';
        testCampaign1.Member_Type__c = 'Delegate';
        testCampaign1.Type = 'Call List';
        insert testCampaign1;
        
        test.startTest();
        Id standardPriceBookId = Test.getStandardPricebookId();
        order o1 = new order();
        o1.accountid=acc.id;
        o1.EffectiveDate= system.today();
        o1.Attendee_Type__c='Sponsor';
        o1.pricebook2id=standardPriceBookId;
        o1.Primary_Campaign_Source__c=testCampaign1.id;
        o1.Price_List__c='test';
        o1.Status='Draft';
        o1.Sold_On_Date__c= system.today();
        o1.Main_POC__c=con2.id;
        o1.Event__c = testEvent.id;
        insert o1;
        
        Id dealOrderId = Schema.SObjectType.Order.getRecordTypeInfosByName().get('Deal').getRecordTypeId();
        o1.recordtypeid=dealOrderId;
        update o1;
        
        Product2 pd = new Product2(Name='MTB Prod',isActive=true, Number_of_Roundtables__c =2);
        pd.IsActive = true;
        insert pd;
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
        insert pbe;
        
        OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=o1.Id, Quantity=1, UnitPrice=99, Roundtables__c = 10);
        insert ordPd;
        
        o1.Attendee_Type__c = 'Sponsor';
        o1.BU__c = 'MTB';
        o1.status='Approved';
        update o1;
        test.stopTest();
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 
This was selected as the best answer
Adam Lee 60Adam Lee 60
I thought it is included in the code. :-/
Adam Lee 60Adam Lee 60
All sorted now. I knew it was something easy! Thanks