• CodySechelski
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hi,

 

I have been struggling with this all day. I'm just trying to get the value that was selected in a SelectList, and use it to do some DML stuff. I don't need to do an ajax request and print back to the screen (that seems to be what all of the code snippets out there are doing).

 

Here's the VF page:

 

<apex:page controller="SelectPricebookController" action="{!pageLoad}" showHeader="false" sidebar="false" title="Select Price List">
    <div class="opportunityTab" style="margin:15px;">
        <apex:form id="TheForm">
        <apex:sectionHeader title="Opportunity" subtitle="Select Price List"/>
        <div class="bDescription"></div>
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Price Book" for="ddlPriceBooks"/>
                    <apex:selectList id="ddlPriceList" size="1" multiselect="false" value="{!priceList}" title="Select a Price List">
                    	<apex:selectOptions value="{!lists}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Select" action="{!savePriceList}" immediate="true"/>
            </apex:pageBlockButtons>
        </apex:pageBlock> 
        </apex:form>
    </div>
</apex:page>

 

 

and here's the controller:

 

public with sharing class SelectPricebookController {


    private Opportunity opp;
    private Account acc;
    private Boolean isDirect = true;
    private String[] optionsToAdd;

    public String pList;
    
    
    public String getPriceList() {
        return pList;
    }

   public void setPriceList(String p) {
       System.Debug('================================== Set priceList: ' + p);
        this.pList = p;
    }
    
    public PageReference pageLoad(){
        //Get the Opportunity based on the query param
        String OppID;
        if (ApexPages.currentPage().getParameters().get('OppID') != null){
            OppID = ApexPages.currentPage().getParameters().get('OppID');
            
            opp = [Select Purchase_Channel_Account__c, Name, AccountId, Price_List__c from Opportunity where ID = :OppID Limit 1];
            
            //Gets the correct account. If the Purchase Channel account is null,
            //it will get the primary account.
            //If not, it will get Purchase Channel Account
            if (opp.Purchase_Channel_Account__c != null){
                acc = [Select NAME, Available_Direct_Price_Lists__c, Available_Channel_Price_Lists__c from Account where ID = :opp.Purchase_Channel_Account__c  Limit 1];
                isDirect = false;
            }
            else{
                acc = [Select NAME, Available_Direct_Price_Lists__c, Available_Channel_Price_Lists__c from Account where ID = :opp.AccountId  Limit 1];
            }

            System.debug('Opportunity ID======================================================> ' + opp.id);
            System.debug('Accout ID======================================================> ' + acc.id);
            
            //Gets the available price lists from the account
            String[] lists;
        
            if(isDirect){
                lists = acc.Available_Direct_Price_Lists__c.split(';');
            }
            else{
                lists = acc.Available_Channel_Price_Lists__c.split(';');
            }
                        
            //If there is only one option, just update the opp and redirect
            //Else, show the user a list to pick from
            if(lists.size() == 1){
                optionsToAdd = lists;
                return updateOpp(lists[0]);
            }
            else{
                optionsToAdd = lists;
                return null;
            }
        }
        else{
            //TODO: Handle Exception
            return null;
        }
    }
    
    public List<SelectOption> getLists(){
        List<SelectOption> options = new List<SelectOption>();
        
        for (Integer i = 0; i < optionsToAdd.size(); i++){
            System.debug('Options======================================================> ' + optionsToAdd[i]);
            options.add(new SelectOption(optionsToAdd[i],optionsToAdd[i]));
        }
        
        return options;
    }
    
    public PageReference savePriceList(){
        System.debug('============== pricelist: ' + pList);
        return updateOpp(pList);
    }
    
    private PageReference updateOpp(String p){
        opp.Price_List__c = p;
        System.debug('====================== Updated pricelist to: ' + p);
        update opp;
        
        PageReference pageRef = new PageReference('/' + opp.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }
}

 

 

As you can see, I have a public string variable to hold the value, a getter and a setter. Then I try to use the variable in the savePriceList method. However, it's always null.

 

Thanks for the help!

Hi,

 

I have been struggling with this all day. I'm just trying to get the value that was selected in a SelectList, and use it to do some DML stuff. I don't need to do an ajax request and print back to the screen (that seems to be what all of the code snippets out there are doing).

 

Here's the VF page:

 

<apex:page controller="SelectPricebookController" action="{!pageLoad}" showHeader="false" sidebar="false" title="Select Price List">
    <div class="opportunityTab" style="margin:15px;">
        <apex:form id="TheForm">
        <apex:sectionHeader title="Opportunity" subtitle="Select Price List"/>
        <div class="bDescription"></div>
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Price Book" for="ddlPriceBooks"/>
                    <apex:selectList id="ddlPriceList" size="1" multiselect="false" value="{!priceList}" title="Select a Price List">
                    	<apex:selectOptions value="{!lists}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Select" action="{!savePriceList}" immediate="true"/>
            </apex:pageBlockButtons>
        </apex:pageBlock> 
        </apex:form>
    </div>
</apex:page>

 

 

and here's the controller:

 

public with sharing class SelectPricebookController {


    private Opportunity opp;
    private Account acc;
    private Boolean isDirect = true;
    private String[] optionsToAdd;

    public String pList;
    
    
    public String getPriceList() {
        return pList;
    }

   public void setPriceList(String p) {
       System.Debug('================================== Set priceList: ' + p);
        this.pList = p;
    }
    
    public PageReference pageLoad(){
        //Get the Opportunity based on the query param
        String OppID;
        if (ApexPages.currentPage().getParameters().get('OppID') != null){
            OppID = ApexPages.currentPage().getParameters().get('OppID');
            
            opp = [Select Purchase_Channel_Account__c, Name, AccountId, Price_List__c from Opportunity where ID = :OppID Limit 1];
            
            //Gets the correct account. If the Purchase Channel account is null,
            //it will get the primary account.
            //If not, it will get Purchase Channel Account
            if (opp.Purchase_Channel_Account__c != null){
                acc = [Select NAME, Available_Direct_Price_Lists__c, Available_Channel_Price_Lists__c from Account where ID = :opp.Purchase_Channel_Account__c  Limit 1];
                isDirect = false;
            }
            else{
                acc = [Select NAME, Available_Direct_Price_Lists__c, Available_Channel_Price_Lists__c from Account where ID = :opp.AccountId  Limit 1];
            }

            System.debug('Opportunity ID======================================================> ' + opp.id);
            System.debug('Accout ID======================================================> ' + acc.id);
            
            //Gets the available price lists from the account
            String[] lists;
        
            if(isDirect){
                lists = acc.Available_Direct_Price_Lists__c.split(';');
            }
            else{
                lists = acc.Available_Channel_Price_Lists__c.split(';');
            }
                        
            //If there is only one option, just update the opp and redirect
            //Else, show the user a list to pick from
            if(lists.size() == 1){
                optionsToAdd = lists;
                return updateOpp(lists[0]);
            }
            else{
                optionsToAdd = lists;
                return null;
            }
        }
        else{
            //TODO: Handle Exception
            return null;
        }
    }
    
    public List<SelectOption> getLists(){
        List<SelectOption> options = new List<SelectOption>();
        
        for (Integer i = 0; i < optionsToAdd.size(); i++){
            System.debug('Options======================================================> ' + optionsToAdd[i]);
            options.add(new SelectOption(optionsToAdd[i],optionsToAdd[i]));
        }
        
        return options;
    }
    
    public PageReference savePriceList(){
        System.debug('============== pricelist: ' + pList);
        return updateOpp(pList);
    }
    
    private PageReference updateOpp(String p){
        opp.Price_List__c = p;
        System.debug('====================== Updated pricelist to: ' + p);
        update opp;
        
        PageReference pageRef = new PageReference('/' + opp.Id);
        pageRef.setRedirect(true);
        return pageRef;
    }
}

 

 

As you can see, I have a public string variable to hold the value, a getter and a setter. Then I try to use the variable in the savePriceList method. However, it's always null.

 

Thanks for the help!