+ Start a Discussion

Pre-populate New Lead fields

Hi folks. I have a requirement to pre-populate certain fields (phone, fax, mobile phone), based on the record type, when creating a new Lead. Currently, this is done through an s-control, but I intend to re-implement it using visualforce. Can anyone give me any pointers on how to do this? I can't seem to find any good examples for this particular case.

There's probably a reason you're not doing this but I figured I would ask -- why not a custom link on the originating page?


So if you wanted to populate it from an account, you could add this custom link to the account page




And that would prepopulate a new lead record's phone # field with the Account Phone number.


Record type differentiation might be the issue -- but if there were different page layouts for diff record types, maybe this would be a simple non-code solution.

Message Edited by ReidC on 03-03-2010 12:42 PM

Actually, what I'm trying to do is slightly different. Sorry, I should have put more details in my first post to give everyone a better picture.


Basically, our company has different Lead record types based on country (e.g. AU Lead, HK Lead, SG Lead, etc...). When a user creates a new Lead, the phone, mobile phone and fax fields should be pre-populated with the country code, which is derived from the record type. So, for example, a HK Lead should have "(+852)" already in the phone/mobile phone/fax field when the create Lead page comes out.


What the current S-control does, is that it overrides the New button, then performs some check on the Lead record type to determine what prefix to use. It then generates a URL similar to what you posted, where the phone prefix is included as a parameter in the URL.


I think I can implement the same thing in a visualforce controller by generating a PageReference like so:

PageReference createLeadPage = new PageReference('/00Q/e');
createLeadPage.getParameters().put('nooverride', '1');
createLeadPage.getParameters().put('lea10', countryCode);
createLeadPage.getParameters().put('lea9', countryCode);

...then use that PageReference in the action attribute of the visualforce page.


However, is there any other way to do this without manually creating the URL of the create Lead page? The standard controller has methods to navigate to view and edit, but doesn't seem to have any for create.



Why not use a trigger? 



trigger prepopulateLead on Lead (before Insert){ for (Lead l:trigger.new){ if(l.countryCode=='USA'){ l.phone='011'+l.phone; } } }




Hi, I've the same requirement. I've 2 recordtype for standard Opportunity page. currently it's redirecting to the corresponding pagelayout.But I want to pre-populate some values based on the recordtype. How to check in S-Control and pass the parameters. Any help on this regard is highly appreciated.

Hi XYZ83,


I ended up using a Visualforce page for my Lead pre-populate requirement, though it should be possible to do the same thing with an S-control (I just chose not to use an S-control since they have been phased-out).


Here's what I did:

  1. Overrode the New button with a Visualforce page.
  2. The Visualforce page is empty but the "action" attribute of the page calls a method in a custom controller that returns a PageReference to the standard new Lead page.
  3. The custom controller retrieves the RecordType from the page parameters then determines what the pre-populated values should be based on the RecordType. It then creates a PageReference to the standard new Lead page then inserts the determined values as parameters using the ids of the fields to be pre-populated as the key.


Visualforce page


<apex:page standardController="Lead" extensions="MyLeadController" action="{!prepopulateLeadFields}">    


Custom controller


public class MyLeadController {        

private final RecordType recType;

public MyLeadController(ApexPages.StandardController stdController) {

String recordTypeId = ApexPages.currentPage().getParameters().get('RecordType');

RecordType[] results = [select id, name from RecordType where id = :recordTypeId];

if (results.size() == 1) {
recType = results[0];
} else {
recType = new RecordType(name='Unknown Record Type');



public PageReference prepopulateLeadFields() {
//performs whatever logic needed to determine values to pre-populate
String value = doSomething();
//relative URL for standard create Lead page
PageReference createLeadPage = new PageReference('/00Q/e');
// insert value into the page parameters using the field ids as keys.
// in this example, these are the ids for the fax, mobile and office number fields
createLeadPage.getParameters().put('lea8', EncodingUtil.urlEncode(value, 'UTF-8'));
createLeadPage.getParameters().put('lea9', EncodingUtil.urlEncode(value, 'UTF-8'));
createLeadPage.getParameters().put('lea10', EncodingUtil.urlEncode(value, 'UTF-8'));
createLeadPage.getParameters().put('nooverride', '1');

return createLeadPage;


Still not entirely happy with everything about it (particularly the part where I need to hardcode the field ids), but at least it works. If anybody knows of a better way, I'd appreciate your input.


Hope this helps you XYZ83.








Thanks a lot Ozymandias!!



Do you know of a listing of those lead parameters that begin with lea?