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
Michael MMichael M 

Test code for basic trigger

Would someone be able to help me write test code for this trigger:
trigger ReferralCreateContact on Lead (after insert) {
    if (trigger.isinsert){
        if (trigger.isafter){
    for (lead ld : trigger.new){
        if (ld.nextofkin__c !=null && ld.Emergency_Contact_Phone__c !=null){
            String first;
            String last;
            
            String s = ld.NextofKin__c;
            String [] lines = s.split(' ');
            first = lines[0];
            last = lines[1];
            
            List<Contact> contactToInsert = new List<contact>();
            Contact newContact = new Contact();
            newContact.RecordTypeId='0121N000001HoR7QAK';
            newContact.related_referral__c = ld.id;
            newContact.firstname = first;
            newContact.lastname = last;
            newContact.MobilePhone=ld.Emergency_Contact_Phone__c;
            newContact.recordtypeid= '0122g0000005rSbAAI';
            newContact.ownerid = ld.ownerid;
            
            contactToInsert.add(newContact);
              insert contactToInsert;       
        }
    }
    }
    }

}
Best Answer chosen by Michael M
ayu sharma devayu sharma dev
Hello 

Please try the below code:
 
@isTest
private class testRef{
    static testMethod void refTestMethod(){
        Lead ld = new Lead( FirstName = 'Test', LastName = 'Name',
                            nextofkin__c = 'Test Name', 
                            Emergency_Contact_Phone__c = '12343412',
                            Company = 'Test Company',
                            Status = 'Open'
                            
                         );
        insert ld;
        Contact ct = [ SELECT Id, firstname, LastName, MobilePhone,
                       OwnerId, recordtypeid
                       FROM Contact WHERE related_referral__c =: ld.Id
                       Limit 1 ];
        System.assertEquals( ld.nextofkin__c.split(' ')[0], ct.firstname );
        System.assertEquals( ld.nextofkin__c.split(' ')[1], ct.LastName );
        System.assertEquals( ld.Emergency_Contact_Phone__c, ct.MobilePhone );
        System.assertEquals( ld.OwnerId, ct.OwnerId );
        System.assertEquals( '0122g0000005rSbAAI', ct.recordtypeid );
    }
}



If any problem arises then please let me know.

Thanks and Regards 
Ayush Sharma

All Answers

ayu sharma devayu sharma dev
Hello 

Please try the below code:
 
@isTest
private class testRef{
    static testMethod void refTestMethod(){
        Lead ld = new Lead( FirstName = 'Test', LastName = 'Name',
                            nextofkin__c = 'Test Name', 
                            Emergency_Contact_Phone__c = '12343412',
                            Company = 'Test Company',
                            Status = 'Open'
                            
                         );
        insert ld;
        Contact ct = [ SELECT Id, firstname, LastName, MobilePhone,
                       OwnerId, recordtypeid
                       FROM Contact WHERE related_referral__c =: ld.Id
                       Limit 1 ];
        System.assertEquals( ld.nextofkin__c.split(' ')[0], ct.firstname );
        System.assertEquals( ld.nextofkin__c.split(' ')[1], ct.LastName );
        System.assertEquals( ld.Emergency_Contact_Phone__c, ct.MobilePhone );
        System.assertEquals( ld.OwnerId, ct.OwnerId );
        System.assertEquals( '0122g0000005rSbAAI', ct.recordtypeid );
    }
}



If any problem arises then please let me know.

Thanks and Regards 
Ayush Sharma
This was selected as the best answer
Michael MMichael M
Hi Ayush,

Thank you so much. I ran the test, but it failed and gave this error message:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Referral Process” process failed. Give your Salesforce admin these details. <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow.  Please contact your system administrator for more information.:  [] Class.ReferralCreateContactTest.refTestMethod: line 11, column 1