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 get proper code coverage to deploy a trigger? Please help I'm lost and confused

I have the following trigger firing correctly and with community help I built a test class that I thought would apply. However when I check code coverage on the trigger it reads 0% (0/12) even though I ran the test for the class which passed (1/1) What am I missing? How do I edit class to get at least 75% coverage? I'm lost!

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
trigger MerchOppsRecords on Merchant_Application__c (after update){

    Set<Id> accountIds = new Set<Id>();
    Map<Id,Opportunity> accountOpportunityMap = new Map<Id,Opportunity>();
    // Get the parent Account Ids from all the Merchant Application records,
    for(Merchant_Application__c ma : Trigger.new){
        accountIds.add(ma.Account_Name__c); // Assuming Account__c is the fields that maps to the Account Object
    }
    
    // Query the Opportunity using AccountId and build a map of AccountId to the Opportunity record
    // Here the assumption is that One account is tied to only one Opportunity. But if you have multiple opportunities for a single account, the logic must identify the right opportunity
    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>();
    
    // Iterate again to build the Junction Object Records, 
    for(Merchant_Application__c ma : Trigger.new){
        
        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);
            mOps.add(mo); // Add the records to the list
        }
    }
    
    // Note that since this trigger is on both After Insert, After Update, and there is no validation to check if the child records already exists for that Opportunity & Merchant Application combination, it inserts child records everytime something gets changed on Merchant Application
    
    insert mOps; // Insert the junction object records. 
    
}

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
@IsTest
public class MerchOppsRecordsTest { 
 
    public static testmethod void Test() {

  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';
  
  
  //put all other mandatory fields on account
  insert a;
  
  Opportunity o=new opportunity ();
  o.name='opp1';
  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';
  
  
  insert o;
   
  Merchant_Application__c  ma=new Merchant_Application__c ();
  ma.name='testaccount-1';
  ma.Account_Name__c=a.id;
  ma.Merchant_Application_Status__c='Open';
  ma.MA__c='null';
  ma.Principal_First_Name__c='Bruce';
  ma.Principal_Last_Name__c='Wayne';
  //put all other mandatory fields on Merchant_Application__c object
  insert ma;
  
  MerchOpps__c mo=new MerchOpps__c ();
  mo.name='Work';
  mo.ChildofMA__c=ma.id;
  mo.ChildofOpp__c=o.id;
  
  insert mo;  
}
}
kaustav goswamikaustav goswami
Please change the test method name. Do not use a test method name as test.

Thanks,
Kaustav
Steve KucklincaSteve Kucklinca
Is that Line 2 or Line 4 (and if line 4 change 'Test')?
Steve KucklincaSteve Kucklinca
I changed Test to another name still runs at 1/1 and no code coverage on trigger 0/12
Jim JamJim Jam
Your trigger is an after update trigger, but in your test class you are only doing inserts. You need to update the record after it has been inserted.
Steve KucklincaSteve Kucklinca
Jim Jam, thanks for the observation. Now how do I go about correcting that? If I had more experience with this I wouldn't ask as many uninformed questions. Believe me this self training through asking is not easy.
Jim JamJim Jam
at the end of your test method do something like ...

ma = [select name from merchant_application__c where id = :ma.id];
update ma;