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
Tasia Demuth 4Tasia Demuth 4 

Adding a record type to a test class

Hi all,

I have a test class that I am trying to set a RecordTypeId to (see below). I need the record type id because I have a validation rule in production that will prevent the deployment of this test class without the record id set. 

When I hardcode the recordId into the test class, I get 100% coverage. When I try to dynamically assign the id, however, I get 50% coverage. 

Can anyone help with what I am doing wrong? 

Thanks!

Tasia

@isTest
public class UpdateCaseWhenLeadConvertedTest 
{  
    private static testMethod void LeadConvertedTest()
    {
        
        Lead olead = new Lead(LastName='Test',FirstName='Tester',Status='Known',Company='CD Test');
        insert olead;
        
        Case c = new case (Status ='New', Priority = 'Medium', Origin = 'Email');
        c.Lead__c = olead.Id;
        c.RecordTypeId =  Schema.sObjectType.Case.getRecordTypeInfosByName().get('Customer_Service_Record_Type').getRecordTypeId();
        insert c;

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(olead.id);
        lc.setDoNotCreateOpportunity(true);
        lc.setConvertedStatus('Converted w/out Opp');

        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        
        
        List<Case> lstCase = [Select id ,ContactId, Status, Type from Case where id =:c.id ];
        System.assert(lstCase.size() > 0 );
        System.assert(lstCase[0].ContactId !=null );
        System.assertEquals('Closed', lstCase[0].Status);
        System.assertEquals('Web Account Approved', lstCase[0].Type);
        
    }
}

Best Answer chosen by Tasia Demuth 4
Eduardo Costa 4Eduardo Costa 4
Hi,

The "Schema.sObjectType.Case.getRecordTypeInfosByName().get('Customer_Service_Record_Type').getRecordTypeId();" routine searches for the recordTypeId by Label, check the language that is set by your test user in production and if the label name is the same in sanbox.

Att,
Eduardo B. Costa.

All Answers

Eduardo Costa 4Eduardo Costa 4
Hi,

The "Schema.sObjectType.Case.getRecordTypeInfosByName().get('Customer_Service_Record_Type').getRecordTypeId();" routine searches for the recordTypeId by Label, check the language that is set by your test user in production and if the label name is the same in sanbox.

Att,
Eduardo B. Costa.
This was selected as the best answer
Tasia Demuth 4Tasia Demuth 4
Thank you Eduardo! 

That was it, I was using the Developer Name, not the Label. 

Many thanks, 
Tasia