[SalesForce] Replace the Lookup with Picklist using VisualForce and Apex

I want to convert a User Lookup to be displayed as dropdown (Picklist) in add screen. We have a Custom Field to display Users in Opportunity Create Page.

I have written a Custom Apex Controller by extending a Base Controller based on the Salesforce Documentation. I created a Visual Force Page to populate the Users in PickList and i can able to see and drag and drop the Visual Force page in (Setup->Customize->Opportunity->Page Layouts) Opportunity page layout.

After saving the layout i can able to see the Picklist in Opportunity View Page but i am not able to see my Visual Force page in Opportunity Create or Edit Screens.
I am pasting my Code Below

<apex:page standardController="Opportunity" extensions="OpportunityControllerExt">
    <apex:form >
        <table>
            <tr>
            <td>
               Manager
            </td>
            <td>
                <apex:selectList value="{!Opportunity.RegionalSalesManager__c}"   multiselect="false" size="1">
                    <apex:selectOptions value="{!users}"/>
                </apex:selectList>
            </td>
            </tr>
        </table>

    </apex:form>
</apex:page>

Controller Class

public class OpportunityControllerExt {
    private Opportunity oppor;
    public OpportunityControllerExt(ApexPages.StandardController stdController) {
        this.oppor = (Opportunity)stdController.getRecord();
    }

     public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Id, name
                      FROM User
                      WHERE isActive = true]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<SelectOption> getUsers() {
        List<SelectOption> options = new List<SelectOption>();
        List<User> userslist=(List<User>) setCon.getRecords();
        if(userslist!=null){
            for (User myUser: userslist) {
              options.add(new SelectOption(myUser.Id,myUser.name));
            }
        }
        return options;
    }
}

Best Answer

You are out of luck because it is not possible to add a visualforce pages to the Edit/New page layout.

However you can override a standard New and/or Edit buttons, create your own layout using a visualforce pages and redirect users to that pages after they click on New or Edit.

Here is some useful information: Override Standard Buttons and Tab Home Pages

Related Topic