You need to sign in to do that
Don't have an account?

record type doesn't get set in Apex
In a test class, I am trying to create an opportunity of a specified record type.
//create opportunity record String opportunityName= 'xxxx'; Opportunity o = new Opportunity( AccountId=a.Id, Name=opportunityName, StageName='Prospecting', CloseDate=Date.today(), Opportunity_Type__c='New', LeadSource='xxx', RecordTypeId= [Select Id From RecordType Where Name = 'xxx' and SobjectType= 'Opportunity'].Id); insert o; system.debug(o.RecordType.Name);
However this returns- null in the debug statement. I'm sure i'm overlooking something simple. Any help?
You will need to explicitly query when you want related object field values..
...
...
insert o;
opportunity opp = [select recordType.name from opportunity where Id = :o.Id];
system.debug(opp.recordType.Name);
Well, if you are writing a testmethod. Then, how about creating a recordtype before creating an opportunity?
Hope that helps.