3

I am creating a custom visualforce page as part of an approval process. I have already made it so that based on which team the user chooses, one of two possible dynamic picklists will be rendered with the names of the reps on the specific team. The problem I am having right now is that the working picklist is using the User object to get the name of the rep, not the Request_Company_Share__c (cs) object (cs.Rep__c field) that I need. Is there a way to change the picklist values from the User object to the Request_Company_Share__c (cs) object on submit of the form? I've attached my controller and visualforce page code below. Thanks in advance for any help!

************Controller*****************************

Public with sharing class ShareReferenceExtension {

    public ApexPages.StandardController sc;
    public Share_Reference__c sr { get; set; }
    public Request_Company_Share__c cs { get; set; }
    public String usr { get; set; }
    public Account acct { get; set; }
    public List<SelectOption> options = new List<SelectOption>();


    public ShareReferenceExtension(ApexPages.StandardController sc){
        this.sc = sc;
        acct = [SELECT name FROM Account WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
        sr = new Share_Reference__c();
        cs = new Request_Company_Share__c();
        usr = Userinfo.getName();
    }

    public ApexPages.PageReference SaveBoth(){
        cs.Company__c = acct.Id;
        insert cs;
        return null;
    }

    public List<selectOption> getUSATRepOptions() {
        // New list for holding all of the picklist options
        List<selectOption> options = new List<selectOption>();
        options.add(new selectOption('', '- None -'));

        for (User users : [SELECT Id, name FROM User WHERE (Team__c = 'USAT Integrated Brand Sales' and isActive = True)]){
            options.add(new selectOption(users.id, users.name));
        }

        // Return the picklist options
        return options;

    }

    public List<selectOption> getDigitalRepOptions() {

        List<selectOption> options = new List<selectOption>();
        options.add(new selectOption('', '- None -'));

        for (User users : [SELECT Id, name FROM User WHERE (Team__c = 'Digital Sales' and isActive = True)]){
            options.add(new selectOption(users.id, users.name));
        }

        // Return the picklist options
        return options;
    }
}

********VisualForce Page ********

        <!-- Information Section-->
        <apex:pageBlockSection title="Information">
            <apex:inputField value="{!acct.name}"/>

            <apex:outputPanel >
                <apex:outputText value="Type" style="font-weight: bold; padding-right: 2em; font-size: .9em; color: #424242"></apex:outputText>
                <div class="requiredInput" style="display: inline-block">
                    <div class="requiredBlock"></div>
                    <apex:inputField value="{!account.Type}"/>
                </div>
            </apex:outputPanel>


     <!--
            <apex:outputPanel >
                <apex:outputText value="Rep" style="font-weight: bold; position: relative; margin-left: 30%; padding-right: 2em; font-size: .9em; color: #424242"></apex:outputText>
                <div class="requiredInput" style="display: inline-block">
                    <div class="requiredBlock"></div>
                    <apex:actionRegion >
                        <apex:inputField value="{!cs.Rep__c}" required="true"/>
                        <apex:actionSupport event="onchange" rerender="ST"/>
                    </apex:actionRegion>
                </div>
            </apex:outputPanel>
     -->

            <apex:outputPanel >
                <apex:outputText value="Rep Team" style="font-weight: bold; padding-right: 2em; font-size: .9em; color: #424242"></apex:outputText>
                <div class="requiredInput" style="display: inline-block">
                    <div class="requiredBlock"></div>
                    <apex:actionRegion >
                        <apex:selectList value="{!cs.rep_team1__c}" multiselect="false" size="1" required="true">
                            <apex:selectOption itemValue="None" itemLabel="None"/>
                            <apex:selectOption itemValue="Digital Sales" itemLabel="Digital"/>
                            <apex:selectOption itemValue="USAT Integrated Brand Sales" itemLabel="USAT"/>
                            <apex:actionSupport event="onchange" rerender="ST"/>
                        </apex:selectList>
                    </apex:actionRegion>
                </div>
           </apex:outputPanel>



       </apex:pageBlockSection>

        <!-- Table displaying the Sales Team and Agency/Client, and Team Role based on the Property selection -->
        <apex:pageBlockSection id="ST">

            <apex:outputPanel rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales' || cs.rep_team1__c = 'Digital Sales', true, false)}">
                <apex:outputText value="Rep" style="font-weight: bold; padding-right: 2em; font-size: .9em; color: #424242"></apex:outputText>
                <apex:selectList multiselect="false" size="1" required="true">
                    <apex:selectOptions value="{!USATRepOptions}" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}"/>
                    <apex:selectOptions value="{!DigitalRepOptions}" rendered="{!IF(cs.rep_team1__c = 'Digital Sales', true, false)}"/>
                </apex:selectList> 
           </apex:outputPanel>

            <apex:pageBlockTable value="{!sr}" var="agency" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales' || cs.rep_team1__c = 'Digital Sales', true, false)}">
                <apex:column headerValue="Sales Team" rendered="true">

                    <apex:selectlist id="ST1" value="{!sr.SalesTeam__c}" multiselect="false" size="1" required="true">
                        <apex:selectOption itemValue="USAT Integrated Brand Sales" itemLabel="Integrated Brand Sales" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}"/>
                        <apex:selectOption itemValue="Digital Sales" itemLabel="Digital Sales" rendered="{!IF(cs.rep_team1__c = 'Digital Sales', true, false)}"/>
                    </apex:selectlist>    

                </apex:column>

                <apex:column headerValue="Client" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales' || cs.rep_team1__c = 'Digital Sales', true, false)}">
                    <apex:inputField value="{!sr.Client__c}"></apex:inputField>
                </apex:column>

                <apex:column headerValue="Print Agency" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}">
                    <apex:inputField value="{!sr.Print_Agency__c}"></apex:inputField>
                </apex:column>

                <apex:column headerValue="Digital Agency" rendered="{!IF(cs.rep_team1__c = 'Digital Sales' || cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}">
                    <apex:inputField value="{!sr.Digital_Agency__c}"></apex:inputField>
                </apex:column>
            </apex:pageBlockTable>

            <!-- Team Role which is pre-populated based on the rep's Property selection -->
            <apex:selectList id="TR" value="{!cs.Team_Role__c}" multiselect="false" size="1" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales' || cs.rep_team1__c = 'Digital Sales', true, false)}">
                <apex:selectOption itemValue="Agency & Client Rep" itemLabel="Agency & Client Rep" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}"/>
                <apex:selectOption itemValue="Agency Rep" itemLabel="Agency Rep" rendered="{!IF(cs.rep_team1__c = 'Digital Sales', true, false)}"/>
                <apex:selectOption itemValue="Client Rep" itemLabel="Client Rep" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}"/>
            </apex:selectList>
        </apex:pageBlockSection>

        <!-- Page Section for Reason -->
        <apex:pageBlockSection title="Reason">
            <apex:inputField value="{!cs.Reason__c}"/>
        </apex:pageBlockSection>

        <!-- Save Button -->
        <apex:commandButton value="Submit for Approval" action="{!SaveBoth}" rerender="out, in" status="status" style="position: relative; left: 40%"/>

    </apex:pageBlock>
</apex:form>


<!-- The outputted information that the user has submitted -->
<apex:pageBlock id="out" title="Read View">
    <apex:actionStatus startText="updating..." id="status"/>
    <apex:pageBlockSection >
        <apex:outputField value="{!acct.name}"/>
        <apex:outputField value="{!account.Type}"/>
        <apex:outputField value="{!cs.Rep__c}"/>
        <apex:outputField value="{!cs.rep_team1__c}"/>
        <apex:outputField value="{!cs.Team_Role__c}"/>
        <apex:outputField value="{!cs.Reason__c}"/>


        <apex:pageBlockTable value="{!sr}" var="agency">
            <apex:column headerValue="Sales Team">
                <apex:outputField value="{!sr.SalesTeam__c}"/>
            </apex:column>

            <apex:column headerValue="Client">
                <apex:outputField value="{!sr.Client__c}"/>
            </apex:column>

            <apex:column headerValue="Digital Agency">
                <apex:outputField value="{!sr.Digital_Agency__c}"/>
            </apex:column>

            <apex:column headerValue="Print Agency">
                <apex:outputField value="{!sr.Print_Agency__c}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlockSection>

1 Answer 1

1

What I'm gathering here is that you'd like to save the current value of your picklist to the Request_Company_Share__c record you're inserting in the SaveBoth() method. Since your picklist contains users, I'm going to assume cs.Rep__c is a lookup to a User record.

First off, you'll need to give your selectList a value parameter like the other selectLists have. This value parameter will store the current value of your picklist. You'll also want to declare it with a getter and setter in your controller.

I'm going to call your variable repOption.

Page Code

<apex:selectList multiselect="false" size="1" required="true" value="{!repOption}>
                <apex:selectOptions value="{!USATRepOptions}" rendered="{!IF(cs.rep_team1__c = 'USAT Integrated Brand Sales', true, false)}"/>
                <apex:selectOptions value="{!DigitalRepOptions}" rendered="{!IF(cs.rep_team1__c = 'Digital Sales', true, false)}"/>
</apex:selectList>

Controller Code

public ApexPages.StandardController sc;
public Share_Reference__c sr { get; set; }
public Request_Company_Share__c cs { get; set; }
public String usr { get; set; }
public Account acct { get; set; }
public List<SelectOption> options = new List<SelectOption>();
public String repOption { get; set; }

Once you've done that, in your SaveBoth() method you can do this.

public ApexPages.PageReference SaveBoth(){
    cs.Company__c = acct.Id;
    cs.Rep__c = repOption;
    insert cs;
    return null;
}

Final Note

You might want to look in to a feature called Lookup Filters. I'm not sure how well they work in the context of visualforce pages, but you could probably use them here.

1
  • Wow! It worked. I swear I tried that, but I guess not. Thank you!
    – Stephen
    Apr 13, 2015 at 19:20

You must log in to answer this question.

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