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
Steve KucklincaSteve Kucklinca 

How to increase code coverage on this trigger?

My trigger is firing perfectly in sandbox. However when I added lines 17-19 to limit when to fire the trigger (bold text) my coverage dropped from 100% (12/12) down to 68% (11/16) How do I edit the class to increase my coverage back to 75% +?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); 
    }
    
  
    for(Opportunity opp : [Select Id, Name, AccountId from Opportunity where AccountId = :accountIds]){
        accountOpportunityMap.put(opp.AccountId,opp);
    }
    
    List<MerchOpps__c> mOps = new List<MerchOpps__c>();
     
    for(Merchant_Application__c ma : Trigger.new){
         Merchant_Application__c oldma = Trigger.oldMap.get(ma.Id);
         if (ma.Completed__c !=oldma.Completed__c) {
             if (ma.Completed__c == 'Completed')

        if(accountOpportunityMap.containsKey(ma.Account_Name__c)){
            MerchOpps__c mo = new MerchOpps__c(ChildofOpp__c = accountOpportunityMap.get(ma.Account_Name__c).Id, ChildofMA__c = ma.ID);
            mo.Name = ma.Name + '_Notification';
            mOps.add(mo); 
        }
    }
   
    insert mOps;  
    
}
}

Related class to test coverage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
  Contact c=new contact ();
  c.FirstName='Bruce';
  c.LastName='Wayne';
  c.AccountId=a.id;
  c.email='Bruce.Wayne@DarkKnight.com';
  
  insert c;
  
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_Name__c=c.id;
  ma.Completed__c='';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.accountId=a.id;
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
  update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}
David "w00t!" LiuDavid "w00t!" Liu
Here's what you need to do in your test class to hit lines 17-19 in your trigger:
  1. Create a Merchant_Application__c where the Completed__c field is NOT 'Completed'
  2. Update the record in step 1 so that the Completed__c field is 'Completed'
Right now you have step 1 down but you're not doing step 2.  You should probably do this after line 59 in your test class:
ma.Completed__c = 'Completed';


AshlekhAshlekh
Hi,

Below is the code which help you to understand 

@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
  
  //put all other mandatory fields on account
  insert a;
  
  Contact c=new contact ();
  c.FirstName='Bruce';
  c.LastName='Wayne';
  c.AccountId=a.id;
  c.email='Bruce.Wayne@DarkKnight.com';
  
  insert c;
  
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_Name__c=c.id;
  ma.Completed__c='';
  
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.accountId=a.id;
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
  
  insert o;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
  
  ma = [select name from merchant_application__c where id = :ma.id];
  ma.Completed__c ='Completed';
  ma.Account_Name__c=a.id;
  update ma;

  o = [select name from Opportunity where id =:o.id];
  update o;
}
}

Ramakrishnan AyyanarRamakrishnan Ayyanar
@IsTest
public class MerchOppsRecordsTest {

    public static testmethod void MO() {

  Account a=new account();
  a.name='testaccount';
  a.website='www.test.com';
  a.Base_Rate_Program__c='2.9%';
  a.socpl__Billing_Country__c='USA';
  a.Company_Description__c='testing';
  a.Incumbent_Processer__c='Startup';
  a.Current_Integration_Type__c='Hosted Pages';
  a.Primary_Investor_s_VC_s_Name_s__c='Self funded';
  a.Approval_Stage__c='Approved';
 
  //put all other mandatory fields on account
  insert a;
 
  Contact c=new contact ();
  c.FirstName='Bruce';
  c.LastName='Wayne';
  c.AccountId=a.id;
  c.email='Bruce.Wayne@DarkKnight.com';
 
  insert c;
 
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Approved';
  ma.MA__c='null';
  ma.Principal_Name__c=c.id;
  ma.Completed__c='';
 
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
 
  Opportunity o=new opportunity ();
  o.name='opp1';
  o.accountId=a.id;
  o.stagename='Signed';
  o.closedate=date.parse('06/23/2014');
  o.Go_Live_Date__c=date.parse('07/16/2014');
  o.P_T_Date__c=date.parse('10/31/2014');
  o.Next_Step__c='test';
  o.type='New Business';
  o.leadsource='Get Started';
 
  insert o;
 
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
 
  insert mo; 

 //make chage try this
  Merchant_Application__c  updatema= [select name,id,Completed__c,Account_Name__c  from merchant_application__c where id = :ma.id];
updatema.Completed__c ='Completed';
updatema.Account_Name__c=a.id;
  update updatema;


  o = [select name from Opportunity where id =:o.id];
  update o;
}
}