1

This question already has an answer here:

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;
}    

marked as duplicate by Sergej Utko, Samuel De Rycke Mar 5 '15 at 15:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

See the answer to How to access recordType in Test Methods with seeAllData = false which should provide you with the means to add the RecordType without needing to hard code the ID into your test class. In essence, you can either use a schema call to obtain it or else put it in a static resource.

0

public static schema.describesobjectresult caseRecordType=Case.sobjecttype.getdescribe(); public static map caseRecordTypeMap=caseRecordType.getrecordtypeinfosbyname();

public static id RecordTypeId= caseRecordTypeMap.get('your Record Type Name here').getrecordtypeid();
0

This is a common issue. See here: What would be the best approach to get the recordtype id

for a list of possible solutions. In short, the fastest and simplest way is to query the RecordType object and put 'where DeveloperName = X' in the Where.

Not the answer you're looking for? Browse other questions tagged or ask your own question.