0

I am trying to create a visualforce form to create a new opportunity. I want the first action to be selecting the opportunity owner (not the current running user - one user will be creating opportunities for a different user). Based on the owner selection I need a picklist to be populated with the available record types for that owner. The issue I am having is getting the query to refresh and populate the picklist:

Visualforce Page:

<apex:page controller="opp_NewCSOpportunityController" tabStyle="Opportunity" >
    <apex:sectionHeader title="Demo"  subtitle="Custom Lookup" />

    <apex:form id="myForm">  
        <apex:PageBlock id="PageBlock"> 
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}" />
                <apex:commandButton value="Cancel" action="{!Cancel}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2" title="Custom Lookup">
                <apex:inputField id="owner" value="{!Opportunity.OwnerId}">
                    <apex:actionSupport event="onchange" reRender="PageBlock" />
                </apex:inputField>


                        <apex:selectList value="{!rectypes}" multiselect="false" size="1" >
                            <apex:selectOptions value="{!items}"/>
                        </apex:selectList><p/>

                        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>

                    <apex:outputPanel id="out">
                        <apex:actionstatus id="status" startText="testing...">
                            <apex:facet name="stop">
                                <apex:outputPanel>
                                    <p>You have selected:</p>
                                    <apex:dataList value="{!rectypes}" var="c">{!c}</apex:dataList>
                                </apex:outputPanel>
                            </apex:facet>
                        </apex:actionstatus>
                    </apex:outputPanel>

            </apex:pageBlockSection>
        </apex:PageBlock>
    </apex:form>      
</apex:page>

Apex Controller:

public class opp_NewCSOpportunityController {

    Opportunity opportunity;

    //If this is the first time the method is called,
    // it creates an empty record for the variable.

    public Opportunity getOpportunity() {
        if(opportunity == null) opportunity = new Opportunity();
        return opportunity;
    }

    //Select the owner of the new opportunity

    List<User> users = [SELECT Id, Profile.Name FRom User WHERE Id =:ApexPages.currentPage().getParameters().get('owner') LIMIT 1];

    public string profile{
        get{
            for(User u:users){
                profile = u.Profile.Name; 
                system.debug('u.Profile.Name');
            }       
            return profile;
        }
        set;
    }

    String[] rectypes = new String[]{};

        public PageReference test() {
            return null;
        }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        If(profile == 'Q-Sales-EI'){
            options.add(new SelectOption('Employee Insights','Employee Insights'));          
        }
        else If(profile == 'Q-Sales-Enterprise'){
            options.add(new SelectOption('Enterprise','Enterprise'));

        }
        else If(profile == 'Q-Sales-K12'){
            options.add(new SelectOption('K-12','K-12'));

        }
        else If(profile == 'Q-Sales-RS'){
            options.add(new SelectOption('Research Suite Corp','Research Suite Corp'));

        }
        else If(profile == 'Q-Sales-SLG'){
            options.add(new SelectOption('Enterprise','Enterprise'));

        }
        else If(profile == 'Q-Sales-Academic'){
            options.add(new SelectOption('Academic','Academic'));
            options.add(new SelectOption('Enterprise','Enterprise'));
        }          
        return options;
    }

    public String[] getRectypes() {
        return rectypes;
    }

    public void setRectypes(String[] rectypes) {
        this.rectypes = rectypes;
    }

    // This method cancels the wizard, and returns the user to the 
    // Opportunities tab
    public PageReference cancel() {
        PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
        opportunityPage.setRedirect(true);
        return opportunityPage; 
    }

    // This method performs the final save, and
    // then navigates the user to the detail page for the new
    // opportunity.
    public PageReference save() {

        // Create the opportunity. 
        insert opportunity;

        // Finally, send the user to the detail page for 
        // the new opportunity.

        PageReference opptyPage = new ApexPages.StandardController(opportunity).view();
        opptyPage.setRedirect(true);

        return opptyPage;
    }    

}
1

You need to query user everytime your owner change on layout

 public string profile{
        get{
            for(User u: [SELECT Id, Profile.Name FRom User WHERE Id =:Opportunity.OwnerId LIMIT 1]){
                profile = u.Profile.Name; 
                system.debug('u.Profile.Name');
            }       
            return profile;
        }
        set;
    }

You need to update you getItem section as

public List<SelectOption> getItems() {
        for(User u: [SELECT Id, Profile.Name FRom User WHERE Id  =:Opportunity.OwnerId LIMIT 1]){
                profile = u.Profile.Name;

    List<SelectOption> options = new List<SelectOption>();
    If(profile == 'Q-Sales-EI'){
        options.add(new SelectOption('Employee Insights','Employee Insights'));          
    }
    else If(profile == 'Q-Sales-Enterprise'){
        options.add(new SelectOption('Enterprise','Enterprise'));

    }
    else If(profile == 'Q-Sales-K12'){
        options.add(new SelectOption('K-12','K-12'));

    }
    else If(profile == 'Q-Sales-RS'){
        options.add(new SelectOption('Research Suite Corp','Research Suite Corp'));

    }
    else If(profile == 'Q-Sales-SLG'){
        options.add(new SelectOption('Enterprise','Enterprise'));

    }
    else If(profile == 'Q-Sales-Academic'){
        options.add(new SelectOption('Academic','Academic'));
        options.add(new SelectOption('Enterprise','Enterprise'));
    }          
    return options;
}

Also for everything you have created get method.In get method its hard to manage code execution because required or not whenever page rerender they execute again. increase the CPU time and in your case will increase number of SOQL as well.

  • I changed the code but the picklist still isn't populating. – user35075 Sep 26 '16 at 18:46
  • What would you recommend instead of using the get method? – user35075 Sep 26 '16 at 18:46
  • @user35075 create void methods and do processing in them. Also for profile put this code inside getitems method. – Tushar Sharma Sep 27 '16 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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