0

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;
                    }
                }
            }

The test class:

            @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; 
                    insert sh;

                      test.stopTest();
                }
            }
  • No coverage whatsoever ? – Samuel De Rycke Oct 14 '15 at 10:05
  • 1
    your Trigger is on Scheme__c object (on insert/update) and you are not even inserting or updating Scheme__c record in your test method. You have to insert or update Scheme__c record in your test method to invoke Trigger code. – Rohit Patil Oct 14 '15 at 10:07
  • No 0% code coverage. – Nayana Oct 14 '15 at 10:08
  • That line was deleted Please check my code again. – Nayana Oct 14 '15 at 10:14
  • 1
    still 0% code coverage? – Rohit Patil Oct 14 '15 at 10:23
-1

You have to insert the test data for the Scheme__c object record in the test class.

It seems that in the trigger you are inserting ANZSIC_Contact__c object record as soon as the Account and contact gets inserted with the recordtype: Underwriter. What will happen you insert the scheme__c. What is the code coverage?

Please add debug statements in the trigger before the line : insert acc; so that you can check whether the trigger is getting invoked or not.

@isTest
        private class Test_getAllContacts
        {
            static testMethod void getAllContacts()
            {

                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';
                test.startTest();
                insert con; Scheme__c  sh = new Scheme__c();
                sh.Account__c = acc.Id; 
                  test.stopTest();
            }
        }
  • It seems that from the trigger you are inserting ANZSIC_Contact__c object record as soon as the Account and contact gets inserted with the recordtype: Underwriter . What will happen you you insert the scheme__c. What is the code coverage? Please add the debug statements in the trigger before the line : insert acc; so that you can check whether the trigger is getting invoked or not. – Vipul Goel Oct 14 '15 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.