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
msb-appsupport1.3905906418879758E12msb-appsupport1.3905906418879758E12 

How to set recordtype name when creating a new lead

I want to use Apex code and visualforce page to create a new lead.

In web, I need to choose a record type before I create a new lead. And there are three types of leads.
In visualforce page, I want to create a new lead with assigned record type using code.

The confusion for me is that the recordType is related list for the lead. I could extract a list of record type by the following.

for (list<RecordType> rts : [SELECT ID, name FROM RecordType WHERE SObjectType = 'Lead' Order by name])

However, I could not pull out the recordType for a lead by the following, since the recordType is related list.

Lead l = [SELECT RecordType FROM LEAD].

Besides, what I want is create a new lead like below.
Lead l = new Lead(firstname='testInsertLead2', lastname='test',company='Temp Company', RecordType = 'registration type lead');

Could anyone help me with this to access the related list using apex code or visualforce page?

Thanks a lot.


Best Answer chosen by msb-appsupport1.3905906418879758E12
Chidambar ReddyChidambar Reddy
Hi

Try the following it will be user friendly



Map<String,Id> recordtypeMap  = new Map<String,Id>();

for(Record Type leadRT :  [select id, Name from RecordType where SObjectType = 'Lead' ]){

recordtypeMap.put(leadRT.Name,leadRT.Id);
 }

//you can create lead by following 

Lead l = new Lead(firstname='testInsertLead2', lastname='test',company='Temp Company', RecordTypeId = recordtypeMap.get('registration type lead'));

// be sure to enter record type name correctly other wise it would give an error.



Thank you,
Choose it as Best Answer if it resolved your issue.



All Answers

Ramu_SFDCRamu_SFDC
Please review my answer to your question at the link below 
https://developer.salesforce.com/forums/?id=906F00000009n2qIAA
Chidambar ReddyChidambar Reddy
Hi

Try the following it will be user friendly



Map<String,Id> recordtypeMap  = new Map<String,Id>();

for(Record Type leadRT :  [select id, Name from RecordType where SObjectType = 'Lead' ]){

recordtypeMap.put(leadRT.Name,leadRT.Id);
 }

//you can create lead by following 

Lead l = new Lead(firstname='testInsertLead2', lastname='test',company='Temp Company', RecordTypeId = recordtypeMap.get('registration type lead'));

// be sure to enter record type name correctly other wise it would give an error.



Thank you,
Choose it as Best Answer if it resolved your issue.



This was selected as the best answer
karen manekaren mane
Thanks, this helped a lot.
Regards,
Smokekitchen (https://smokekitchen.net/tomodachi-knives-review/)
BitcoinTidingsComBitcoinTidingsCom
I tested the above code many times in my solutions one and it works very well, moreover, what I will convince you for yourself, see on my website about bitcoins https://bitcoin-tidings.com/
Susand AdameSusand Adame
thanks to your solution, I can have another leds. Men log on to my website wanting to see naughty girls (https://naughty-girls.net/naked-girls/). Thank you