I'm trying to test a workflow rule made, and I'm trying to insert some Leads, with determined Recordtypes to cover all test scenarios.

In Dev Console/IDE, how to create a Lead with a custom Record type?

Is it possible to create with the RT Name?


1 Answer 1


In Developer Console you can do the following

Lead l = new Lead(Company = 'foo', LastName = 'bar',
                  RecordTypeId = [SELECT Id FROM RecordType
                                    WHERE DeveloperName = 'My_Record_Type' AND
                                          SObjectType = 'Lead'][0].Id);
insert l;

That is, you have to set the value of an ID Lead.RecordTypeId field but you can locate it via SOQL.

Or, you can do without the query using the Schema Describe methods ...

Lead l = new Lead(Company = 'foo', LastName = 'bar',
                  RecordTypeId = :Lead.SobjectType.getDescribe()

insert l;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .