You need to sign in to do that
Don't have an account?
Abraham kumar
Test Class Coverage not exceeding 69%
HI All,
I have the below trigger and test class and with this test class i can get only 69% code coverage, can you pls help me increase code coverage for below trigger so i will be able to deploy this to production.
I have checked in the developer console and see the lines 21 to lines 32 of the trigger are not being covered. PLease help me increase code coverage to deploy this code.
Many Thanks
Abraham
I have the below trigger and test class and with this test class i can get only 69% code coverage, can you pls help me increase code coverage for below trigger so i will be able to deploy this to production.
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 == '012D0000000BaFA' && c.Reg__c == TRUE) c.status__c='inactive'; { validContacts.add(c); accIds.add(c.accountid); } } } else { for (contact c : Trigger.new) { if(c.RecordTypeId == '012D0000000BaFA' && 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.Reg__c == TRUE) { 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 Classs:-
@istest public class ContactcalloutTestClass{ @istest static void contactcreate(){ Account acc = new Account(); acc.Name = 'Test'; acc.ShippingStreet = 'test'; acc.ShippingCity = 'test'; acc.ShippingPostalCode = 'tw31aq'; insert acc; 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; Contact c1=[Select id from contact Where Id =:c.id]; c1.firstname= 'Daniel'; c1.lastname= 'Abraham'; c1.Email = 'def@gmail.com'; c1.Status__c = 'inactive'; update c1; try { Delete c1; // c1.Status__c = 'Inactive'; } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } } @istest static void contactcreate2(){ Account acc2 = new Account(); acc2.Name = 'Test'; acc2.ShippingStreet = 'test'; acc2.ShippingCity = 'test'; acc2.ShippingPostalCode = 'tw31aq'; insert acc2; contact c3 =new contact(); c3.Email = 'abc@gmail.com'; c3.Salutation = 'Mr'; c3.FirstName = 'Abraham'; c3.LastName = 'Daniel'; c3.type__c = 'Agency'; c3.status__c= 'Active'; insert c3; Contact c4=[Select id from contact Where Id =:c3.id]; c4.firstname= 'Daniel'; c4.lastname= 'Abraham'; c4.Email = 'def@gmail.com'; c4.Status__c = 'inactive'; update c4; if(c4.email !=c3.email||c4.firstname != c3.firstname||c4.lastname != c3.lastname){ Test.setMock(WebServiceMock.class, new WebServiceMockImpl()); } } }
I have checked in the developer console and see the lines 21 to lines 32 of the trigger are not being covered. PLease help me increase code coverage to deploy this code.
Many Thanks
Abraham
if(c.RecordTypeId == RecordType1 && c.Registered_on_ITV_Media__c == TRUE)
c.Status__c=Inactive';
That completes the if statement. The if statement is completed after the simi-colon. (at least I think). Are you wanting line 11,13,14 to all be part of the if statement? I think as it stands - 13 & 14 part just part of the For statement started on 08.
For lines 22+ - I think that it's the same type of issue. When you're creating the contact record - you're not setting the Reg__c field to true.
Quite often in times like this - I'll use the debug statement to verify what I'm testing on. For example - just before line #21 - I'd do statements like this...
system.debug('RecordType1='+RecordType1);
system.debug('c.RecordTypeId='+c.RecordTypeId);
system.debug('c.Reg__c='+c.Reg__c);
then look at your log - and you'll see why line #22 is NOT getting executed.
Good luck
All Answers
so - let's say that you're Contact record type name (now - you should use develoer_name) - is 'Agency' - then I'd code it like this...
id contactAgencyRecordTypeId = [SELECT id FROM RecordType WHERE sObject = 'Contact' AND Developer_Name = 'Agency'].id;
then - in your trigger at lines #9 and #20 - instead of hardcoding the ID use the field contactAgencyRecordTypeId.
HTH
So changing the hard coding of the record type id will increase the code coverage am i right?...
Many Thanks
Abraham
I have done the changes as below, Its just my test class is failing im getting a error msg System.StringException: Invalid id: RecordType1 at Class.ContactcalloutTestClass.contactcreate: line 20, column 1
in developername im using the actual record type name which is "commercial" in my test class...is this correct?
I initially used record type ids becuse the record type id is same in production and sandbox.
Many Thanks
Abraham
This is my trigger :-
Test class:-
Many Thanks in advance
Abraham
The record type name is "Commercial" caps of the first letter. I changed my code also still getting 70%
Lines not being covered are
Line no 11 and
Line no 22 to 32..
Many Thanks
Abraham
if(c.RecordTypeId == RecordType1 && c.Registered_on_ITV_Media__c == TRUE)
c.Status__c=Inactive';
That completes the if statement. The if statement is completed after the simi-colon. (at least I think). Are you wanting line 11,13,14 to all be part of the if statement? I think as it stands - 13 & 14 part just part of the For statement started on 08.
For lines 22+ - I think that it's the same type of issue. When you're creating the contact record - you're not setting the Reg__c field to true.
Quite often in times like this - I'll use the debug statement to verify what I'm testing on. For example - just before line #21 - I'd do statements like this...
system.debug('RecordType1='+RecordType1);
system.debug('c.RecordTypeId='+c.RecordTypeId);
system.debug('c.Reg__c='+c.Reg__c);
then look at your log - and you'll see why line #22 is NOT getting executed.
Good luck
Thanks soo much Jeffrey yes
c.Registered_on_ITV_Media__c == TRUE in the test class did the trick..
Many Thanks
Abraham