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
dbush2765dbush2765 

RecordType.Name in Test Methods

So I'm I'm writing a test method for some code that queries RecordType.Name on the Opportunity object. I'm able to grab the RecordType ID and assign it to the Opportunity. System.debug shows that the RecordType I query in the test method has an ID and Name as well.

 

For some reason, it's telling me that Opportunity.RecordType.Name is null, even after I've assigned a RecordType to the Opportunity. Anyone know what's going wrong here? Here's the code:

 

RecordType UpsellRT = [SELECT Id,Name FROM RecordType WHERE sObjectType = 'Opportunity' AND Name = 'UpSell' LIMIT 1];

Opportunity OPP = new Opportunity(
	Name = 'Test Opp',
	RecordTypeId = UpsellRT.Id,
	CloseDate = Date.valueOf('2013-01-01'),
	StageName = 'Closed Won');
insert OPP; 
sfdcfoxsfdcfox
Does your trigger query RecordType.Name? If not, that's where your problem is. There's nothing wrong with your test code.
dbush2765dbush2765

Yeah, it queries RecordType.Name in the actual code being tested.

 

This is what System Debug tells me:

 

System.debug(OPP.RecordTypeId) returns an ID value

System.debug(OPP.RecordType.Name) returns null

 

The record type query is definitely finding a record type and assigning the ID to the Opportunity, but the name field is still null when I try to do anything with it.

Raj_Raj_

have you assigned 

@istest  as @istest(seeAllData = true)

 

BALA_RAMBALA_RAM

here my record type name is RFP in opportunity object.

 

for example below code is for Record type call in Testclass

 

RecordType RFPRectype = [select id,DeveloperName from RecordType where RecordType.DeveloperName ='RFP'];
  CLASSName.opportunity.RecordTypeId = RFPRectype.id;

Raj_Raj_

paste your full code. including @istest .

Rohini AherRohini Aher
Even I am facing the same issue. Not able to get the recrd type name after assigning recordType id in test class. Already tried assigning the record type id through descrbe sobject and query. However not working. Not sure what's going wrong in this case
Sona Kanwal 21Sona Kanwal 21
Did anyone get the solution?