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
Alex SkemprisAlex Skempris 

test class for insert trigger

Hello,

I'm trying to write a test class for a simple before insert trigger and I can't for the life of me figure out what i'm missing or if I'm in the wrong track completely (i'm an apex beginner). My code below:


trigger Update_Hol_to_Wyb_Fields on Account (before insert) {
    
     for (Account wyb:Trigger.New){
     
                if (wyb.RecordTypeId == '01258000000VBdv' && wyb.PersonLeadSource == 'Holkham'){
                    
                        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
                        
                        try{
                        wyb.DOB__c = holkhamAccount[0].DOB__c;
                        wyb.BillingStreet = holkhamAccount[0].BillingStreet;
                        wyb.BillingCity = holkhamAccount[0].BillingCity;
                        wyb.BillingState = holkhamAccount[0].BillingState;
                        wyb.BillingCountry = holkhamAccount[0].BillingCountry;
                        }
                        Catch (exception e){
                        wyb.adderror(e.getmessage()+' Please contact Alex');
                        }
                   }
            }          
     }


My (unsuccessful) test class attempt:

@isTest
public class Update_Hol_to_Wyb_FieldsTestClass 
{
    Static testMethod void myUnitTest()
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'alex';
        wyb.LastName = 'Test';
        wyb.RecordTypeId = '01258000000VBdv';
        insert wyb;

        List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];

        Account hol = new Account();
        hol.FirstName = 'alex';
        hol.LastName = 'Test';
        hol.RecordTypeId = '012580000005mIR';
        hol.BillingStreet = holkhamAccount[0].BillingStreet;
        hol.BillingCity = holkhamAccount[0].BillingCity;
        hol.BillingState = holkhamAccount[0].BillingState;
        hol.BillingCountry = holkhamAccount[0].BillingCountry;        
        hol.DOB__c = holkhamAccount[0].DOB__c;
        insert hol;
        
        Test.startTest();     
        insert wyb;
        Test.stopTest();    

        List <Account> wybAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '01258000000VBdv' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName AND PersonLeadSource =: wyb.PersonLeadSource];
        system.assertEquals(1, wybAccount.size());
        system.assertEquals(wybAccount[0].BillingCity, hol.BillingCity);
        system.assertEquals(wybAccount[0].BillingStreet, hol.BillingStreet);        
        system.assertEquals(wybAccount[0].BillingState, hol.BillingState);        
        system.assertEquals(wybAccount[0].BillingCountry, hol.BillingCountry);  
        system.assertEquals(wybAccount[0].DOB__c, hol.DOB__c); 
    }
        
}

Any help would be greatly appreciated.

Thanks
Alex
Best Answer chosen by Alex Skempris
Alex SkemprisAlex Skempris
Hi Manj

I have managed to get the coverage up to 100%. Ironically the only thing I was missing was a (seeAllData = True) statement on top of the class. That's why the query was fetching no results. 

I'm attaching the updated test class for anyone bumping on this thread.

@isTest (seeAllData = true)
public class Update_Hol_to_Wyb_FieldsTestClass 
{
    Public string FirstName2{get; set;}

    Static testMethod void myUnitTest() // test for passing the fields over
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'Alex';
        wyb.LastName = 'test2';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';
        
        Account hol = new Account();
        hol.FirstName = 'Alex';
        hol.LastName = 'test2';
        hol.RecordTypeId = '012580000005mIR';
        hol.DOB__c = Date.newInstance(1984, 12, 9);
        hol.BillingStreet = 'test Street';
        hol.BillingCity = 'test City';
        hol.BillingState = 'test state';
        hol.BillingCountry = 'test country';
        insert hol; 
        
        Test.startTest();
        insert wyb;
           test.stopTest();

    }
    
    Static testMethod void myUnitTest2() // Test for the exception - notice the change the wyb.lastname
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'Alex';
        wyb.LastName = 'test3';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';

        
        Account hol = new Account();
        hol.FirstName = 'Alex';
        hol.LastName = 'test2';
        hol.RecordTypeId = '012580000005mIR';
        hol.DOB__c = Date.newInstance(1984, 12, 9);
        hol.BillingStreet = 'test Street';
        hol.BillingCity = 'test City';
        hol.BillingState = 'test state';
        hol.BillingCountry = 'test country';
        insert hol; 

        
        Test.startTest();
        insert wyb;
           test.stopTest();
        
    }
}

All Answers

Manj_SFDCManj_SFDC
can you please help to understand what is the problem you are facing
Alex SkemprisAlex Skempris
Hi Manj,

Apologies. My problem is that the code coverage is only at 18%.

Thanks
Alex 
Manj_SFDCManj_SFDC
Thanks Alex, also can you please indicate which line aren't getting covered
Alex SkemprisAlex Skempris

From the list down pretty much... all the lines below are still on the red

 

                       List <Account> holkhamAccount = [SELECT DOB__c, BillingStreet, BillingCity, BillingState, BillingCountry from Account WHERE RecordTypeId = '012580000005mIR' AND FirstName =: wyb.FirstName AND LastName =: wyb.LastName LIMIT 1];
                        
                        try{
                        wyb.DOB__c = holkhamAccount[0].DOB__c;
                        wyb.BillingStreet = holkhamAccount[0].BillingStreet;
                        wyb.BillingCity = holkhamAccount[0].BillingCity;
                        wyb.BillingState = holkhamAccount[0].BillingState;
                        wyb.BillingCountry = holkhamAccount[0].BillingCountry;
                        }
                        Catch (exception e){
                        wyb.adderror(e.getmessage()+' Please contact Alex');
                    }
                   }
            }                    
     }

Manj_SFDCManj_SFDC
while you insert the Account in the test class you need set the PersonLeadSource as 'Holkham'

// Create the data
        Account wyb = new Account();
        wyb.FirstName = 'alex';
        wyb.LastName = 'Test';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';
        insert wyb;

Please mark as solved if this helps you 
Good luck !
Alex SkemprisAlex Skempris
Oh, very good spot Manj! I'm now at 63% coverage. The lines that are NOT getting covered are the four address fields below: 

wyb.BillingStreet = holkhamAccount[0].BillingStreet;
wyb.BillingCity = holkhamAccount[0].BillingCity;
wyb.BillingState = holkhamAccount[0].BillingState;
wyb.BillingCountry = holkhamAccount[0].BillingCountry;

Thanks
Alex
Manj_SFDCManj_SFDC
you need to assign the values to these fields while you insert the account , the way you did for  wyb.PersonLeadSource = 'Holkham';
Manj_SFDCManj_SFDC
        Account wyb = new Account();
        wyb.FirstName = 'alex';
        wyb.LastName = 'Test';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';
        wyb.BillingStreet ='test Street';
        wyb.BillingCity = 'test City';
        wyb.BillingState = 'test State';
        wyb.BillingCountry = 'test Country';

        insert wyb;
Alex SkemprisAlex Skempris
Thanks Manj but that doesn't seem to be it unfortunately, I've inserted the fields as per your suggestion and I'm still at 63%. I've also noticed that if I change the order of the field updates in my trigger it is still only the first line that gets covered.

So it seems that it's only covering one field rather than the DOB__c field specifically. 

Thanks
Alex
 
Manj_SFDCManj_SFDC
can you put a debug after your query in the trigger and check what is the query returning
Manj_SFDCManj_SFDC
and are you landing into exception block
Manj_SFDCManj_SFDC
you need to have an account in your org with first name as alex and last name as Test (as yo mentioned in the test class)
Alex SkemprisAlex Skempris
Hi Manj

You are exactly right. I'm landing in my exception block, I've confirmed the query returns no results. Only thing is I've confirmed there is an account in my sandbox org with those first and last names in and the query still returns nothing. Trying to troubleshoot now. 

Thanks
Alex
Alex SkemprisAlex Skempris
If I put the query alone in the query editor it's finding the account. It is not finding it with wyb.firstname value though
Manj_SFDCManj_SFDC
Your query is trying fetch an existing account please check if it has the same record type as assigned in the test class
or used in your SOQL where clause
Alex SkemprisAlex Skempris
Hi Manj

I have managed to get the coverage up to 100%. Ironically the only thing I was missing was a (seeAllData = True) statement on top of the class. That's why the query was fetching no results. 

I'm attaching the updated test class for anyone bumping on this thread.

@isTest (seeAllData = true)
public class Update_Hol_to_Wyb_FieldsTestClass 
{
    Public string FirstName2{get; set;}

    Static testMethod void myUnitTest() // test for passing the fields over
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'Alex';
        wyb.LastName = 'test2';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';
        
        Account hol = new Account();
        hol.FirstName = 'Alex';
        hol.LastName = 'test2';
        hol.RecordTypeId = '012580000005mIR';
        hol.DOB__c = Date.newInstance(1984, 12, 9);
        hol.BillingStreet = 'test Street';
        hol.BillingCity = 'test City';
        hol.BillingState = 'test state';
        hol.BillingCountry = 'test country';
        insert hol; 
        
        Test.startTest();
        insert wyb;
           test.stopTest();

    }
    
    Static testMethod void myUnitTest2() // Test for the exception - notice the change the wyb.lastname
    {
        // Create the data
        Account wyb = new Account();
        wyb.FirstName = 'Alex';
        wyb.LastName = 'test3';
        wyb.RecordTypeId = '01258000000VBdv';
        wyb.PersonLeadSource = 'Holkham';

        
        Account hol = new Account();
        hol.FirstName = 'Alex';
        hol.LastName = 'test2';
        hol.RecordTypeId = '012580000005mIR';
        hol.DOB__c = Date.newInstance(1984, 12, 9);
        hol.BillingStreet = 'test Street';
        hol.BillingCity = 'test City';
        hol.BillingState = 'test state';
        hol.BillingCountry = 'test country';
        insert hol; 

        
        Test.startTest();
        insert wyb;
           test.stopTest();
        
    }
}
This was selected as the best answer
Alex SkemprisAlex Skempris
Many thanks Manj for all your help on this
Manj_SFDCManj_SFDC
Hello Alex, glad you were able to fix it , however its a not a good practice to use seeAllData = true in the test class, else I would have suggested you at first go , please refer
http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/

good luck !