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
Nayana Pawar 5Nayana Pawar 5 

Apex trigger zero code coverage

Can anyone please help me to solve my issue.
I have created apex trigger with test classes. I am not getting any error after test run but unable to getting any code coverage. Please check below is my apex trigger and test class.

                trigger getAllContacts on Scheme__c (after insert,after update)
                {
                    List<ANZSIC_Contact__c> acc = new List<ANZSIC_Contact__c>();
                    for(Scheme__c scheme :trigger.new)
                    {
                        Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Underwriter').getRecordTypeId();
                        if(scheme.Account__c != null && scheme.Account__r.RecordTypeId== devRecordTypeId )
                        {
                            List<Contact> con = new List<Contact>();
                            con = [select Id,Email,Phone from contact where Account.Id =:scheme.Account__c];
                            for(Contact c:con)
                            {
                                acc = [select Id from ANZSIC_Contact__c where Contact__c =:c.Id and Scheme__c =:scheme.Id];
                                if(acc ==Null)
                                {
                                    ANZSIC_Contact__c ac = new ANZSIC_Contact__c();
                                    ac.Contact__c = c.Id;
                                    ac.Scheme__c = scheme.Id;
                                    ac.Email__c = c.Email;
                                    ac.Phone__c = c.Phone;
                                    acc.add(ac);
                                }
                                else
                                {
                                
                                }
                            }
                            insert acc;
                        }
                    }
                }

                @isTest(seeAllData=false)
                private class Test_getAllContacts
                {
                    static testMethod void getAllContacts()
                    {
                        test.startTest();
                        RecordType businessAccountRecordType = [SELECT Id FROM RecordType WHERE SobjectType='Account' AND Name = 'Underwriter'];
                        Account acc = new Account();
                        acc.Name = 'Test Account';
                        acc.RecordTypeId = businessAccountRecordType.Id;
                        insert acc;Contact con = new Contact();
                        con.LastName = 'Test data';
                        con.AccountId = acc.Id;
                        con.Email ='n@yahoo.in';
                        con.Phone = '987654321';
                        insert con; Scheme__c  sh = new Scheme__c();
                        sh.Account__c = acc.Id;
                          test.stopTest();
                    }
                }
 
Tarun_KhandelwalTarun_Khandelwal
Hi Nayana,

You didn't insert sh record of Scheme__c object. Please write -

insert sh;

just before  test.stopTest(); so that your trigger can fire. Let me know in case of any query.
 
Nayana Pawar 5Nayana Pawar 5
That line was deleted still it shows 0 % code coverage
Tarun_KhandelwalTarun_Khandelwal
Please write that line. Because your trigger will fire on after insert and after update event of Scheme__c object.
Nayana Pawar 5Nayana Pawar 5
Actually I missed that line here only. Then also it's not working for me.
Tarun_KhandelwalTarun_Khandelwal
Can you please copy your test class code here?
Nayana Pawar 5Nayana Pawar 5
@isTest(seeAllData=true)
private class Test_getAllContacts
{
    static testMethod void getAllContacts()
    {
        test.startTest();
        
        RecordType businessAccountRecordType = [SELECT Id FROM RecordType WHERE SobjectType='Account' AND Name = 'Underwriter'];
       
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.RecordTypeId = businessAccountRecordType.Id;
         System.debug('Insert Account');
        insert acc;
       
        Contact con = new Contact();
        con.LastName = 'Test data';
        con.AccountId = acc.Id;
        con.Email ='n@yahoo.in';
        con.Phone = '987654321';
        insert con;
       
        Scheme__c  sh = new Scheme__c();
        sh.Account__c = acc.Id;
        insert sh;
        test.stopTest();
    }
}