+ Start a Discussion
John NeilanJohn Neilan 

Record Type Name in Test Class

I have a test class that creates records for use in unit tests. One of the components creates an Opportunity record, but I don't know how to add a Record Type to the Opp. I know I can use RecordTypeID, but since I don't want to hardcode that, I am looking to use the Record Type name. Can anyone help me figure out the syntax I need to use? Thanks,
 
public static Opportunity createOppNew (Id acctId){ 
    Opportunity opp1 = new Opportunity();
      opp1.Name = 'Test Renewal Opp';
      opp1.StageName = 'Phase 1';
      opp1.CloseDate = Date.today().addYears(1);
      opp1.Renewal__c = 'Yes';
      opp1.Effective_Date__c = Date.today().addYears(1);
      opp1.RP_Contract_Start__c = Date.today().addDays(1);
      opp1.Term__c = 12;
      opp1.AccountId = acctId;
      opp1.RP_Customer_Engagement__c = 0;
    return opp1;
}

 
Best Answer chosen by John Neilan
yeshabyeshab
Hi John,

you can use 

recordtype rc1 = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' limit 1];
Opportunity oppty = new Opportunity();
oppty.recordtype = rc1.id;

regards,
Yesha

All Answers

yeshabyeshab
Hi John,

you can use 

recordtype rc1 = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' limit 1];
Opportunity oppty = new Opportunity();
oppty.recordtype = rc1.id;

regards,
Yesha
This was selected as the best answer
John NeilanJohn Neilan
Thanks, that did it!