You need to sign in to do that
Don't have an account?
Abraham kumar
Test Class Code Coverage problem
Hi All.
I have the below trigger and test class and with this test class i can get only 69% coverage, can you please help to increase code coverage for below trigger.
Trigger:-
Test Class:-
Many Thanks for your help in advance
Abraham
I have the below trigger and test class and with this test class i can get only 69% coverage, can you please help to increase code coverage for below trigger.
Trigger:-
trigger Contactcallout on Contact (after insert, after update, before delete) { Map<Id, String> m = new Map<Id, String>(); list<Contact> validContacts = new list<Contact>(); set<ID> accIds = new set<ID>(); if(Trigger.isDelete) { for (contact c : Trigger.old) { if(c.RecordTypeId == '012D8798757BaFA' && c.Registered__c == TRUE) c.status__c='inactive'; { validContacts.add(c); accIds.add(c.accountid); } } } else { for (contact c : Trigger.new) { if(c.RecordTypeId == '012D9876545BaFA' && c.Reg__c == TRUE){ if(Trigger.isUpdate){ contact old = Trigger.oldMap.get(c.Id); if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Registered__c == TRUE) // if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID) { validContacts.add(c); accIds.add(c.accountid); } }else{ validContacts.add(c); accIds.add(c.accountid); } } } } map<ID, Account> accMap; if(!accIds.IsEmpty()) // guard condition for SOQL accMap = new map<ID, Account>([select name from account where id in :accIds]); for (contact c : validContacts) { Account acc = accMap.get(c.AccountID); string accName = acc == null ? null : acc.Name; WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c); } }
Test Class:-
@istest public class ContactcalloutTestClass{ @istest static void contactcreate(){ contact c =new contact(); c.Email = 'abc@gmail.com'; c.Salutation = 'Mr'; c.FirstName = 'Abraham'; c.LastName = 'Daniel'; c.type__c = 'Agency'; c.status__c= 'Active'; insert c; c.firstname= 'Daniel'; c.lastname= 'Abraham'; c.Email = 'def@gmail.com'; c.Status__c = 'active'; update c; try { Delete c; // c1.Status__c = 'Inactive'; } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } } }
Many Thanks for your help in advance
Abraham
I recomened you to avoid hardcoded id in code.
You can use Developer console for view uncovered lines and write additional tests for it.
Thanks, Alex
Change 'MyType' into your record type name
Updated Test class
Check this Trailhead module to learn about apex test class - https://developer.salesforce.com/trailhead/module/apex_testing
i added this line c.RecordTypeId = getRecordTypeId(Contact,commercial); in the test class and it gives the error Compile Error: Variable does not exist: Contact at line 20 column 34
And also where do i add this line in Schema.SObjectType.Object__c.RecordTypeInfosByName.get('MyType').RecordTypeId in my test class?
Thanks
Abraham